R R
R R

Reputation: 313

Strange result when trying to print

After execution, result was very strange:

#include <stdio.h>
int main(){
  int a,b; 
  printf("enter two numbers :");
  scanf("%d%d",&a,&b);
  if(a>b){
    printf("maximum number is %d",&a);
  } 
  else{
    printf("maximum number is %d",&b);  
  }
  return 0; 
}

After enter two numbers in console result was:

maximum number is 2686696

2686696 very strange for me if I enter two numbers such as 5, 3 must shows me 5 but shows 2686696 !!!

Can anyone guide me?

Upvotes: 0

Views: 87

Answers (4)

haccks
haccks

Reputation: 106012

Remove & operator from the argument of printf if you are interested in printing the numbers a and b else change the format specifier %d to %p if you are trying to print the address of a and b.

printf("maximum number is %p", (void *)&a);

Upvotes: 0

user2493976
user2493976

Reputation:

The & in printf prints the address of the variables instead of the value.

To print a value use :

if(a>b){
   printf("maximum number is %d",a);
}
else{
    printf("maximum number is %d",b);  
}

Upvotes: 0

Sadique
Sadique

Reputation: 22821

You are trying to print the address of int not its value. Do this:

if(a>b){
    printf("maximum number is %d",a);
}
else{
    printf("maximum number is %d",b);  
}

& operator returns the address of a or b.

Upvotes: 5

Maroun
Maroun

Reputation: 95968

Remove & from printf to print the value, now you're printing the address. Should be:

printf("maximum number is %d",a);

Upvotes: 0

Related Questions