ADVERTISEMENT
ADVERTISEMENT

C - Program to compare two integer numbers using pointers.


IncludeHelp 20 September 2016

In this code snippet/program/example we will learn how to compare two integer values using pointers in c programming language?

This program will read two integer values in integer pointer variables and compares them, if they are equal program will print “Integers are equal” and if they are not equal program will print “Integers are not equal”.

We will implement this program by using two methods. First simple integer pointer declarations and comparison in main () and second by using user define function – User define function to compare two integers will take two integer pointers and compares them inside the function. Function will return 0 if they are equal otherwise it will return -1.

C Code Snippet/ Program - Compare Two Integer Numbers using Integer Pointers in C programming language

/*C - Program to compare two integer numbers 
using pointers.*/

#include <stdio.h>
int main(){
	int a,b;
	int *pa,*pb;
	
	pa=&a; pb=&b;
	
	printf("Enter first integer: ");
	scanf("%d",pa);
	printf("Enter second integer: ");
	scanf("%d",pb);
	
	//compare 
	if(*pa==*pb)
		printf("Integers are equal.\n");
	else
		printf("Integers are not equal.\n");
	
	return 0;
}

Using user define function

#include <stdio.h>

char compareIntegers(int a,int b){
	if(a==b)
		return 0;
	else
		return -1;
}
int main(){
	int a,b;
	int *pa,*pb;
	
	pa=&a; pb=&b;
	
	printf("Enter first integer: ");
	scanf("%d",pa);
	printf("Enter second integer: ");
	scanf("%d",pb);
	
	//compare 
	if(compareIntegers(*pa,*pb)==0)
		printf("Integers are equal.\n");
	else
		printf("Integers are not equal.\n");
	
	return 0;
}
    Enter first integer: 100
    Enter second integer: 100 
    Integers are equal.

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.