Steven Elcarim
Steven Elcarim

Reputation: 69

printf("%d") doesn't display what I input

My code:

printf("Enter a number : ");

scanf("%d", &number);

printf("%d is what I entered\n", &number);

I input 2,

Expected output : 2 is what I entered

Actual output : 2293324 is what I entered

What's the problem here?

Upvotes: 8

Views: 1602

Answers (6)

Jack
Jack

Reputation: 722

#include <stdio.h>

int main(void) {
        printf("Enter a number: ");
        int number;
        scanf("%d", &number);
        printf("The number is: %d\n", number);

        return 0;
}

This should solve your problem.

In scanf() first you have to specify what the input item will be(int, float, char, char*, etc...) for your example "%d" is for integer.

After that, you have to get the address of your item. That's why you have to use the & operator before the variable.

Your error occurred due to the fact that you were using the & operator on the printf() as well. This basically means that you print the address in the stack where you stored the integer. But without the & you print the actual value.

Hope this helps.

Upvotes: 2

Ali Yeşilkanat
Ali Yeşilkanat

Reputation: 607

That & operand gives the address of variable "number". You should not use it.
Solution:

printf( "%d is what I entered\n", number);

Upvotes: 2

ninja
ninja

Reputation: 118

u can try removing the & from number and just simply write number

printf("%d is what I entered\n", number);

Its actually pointing and displaying the memory location of your input ie 2

Upvotes: 0

Thorsten Scherf
Thorsten Scherf

Reputation: 1011

If you would have used -Wall -g compiler option, then you should have seen the error right during compile time:

# cc -Wall -g ex.c -o ex
ex.c: In function ‘main’:
ex.c:9:10: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
   printf("%d is what I entered\n", &number);
      ^

Upvotes: 4

Spikatrix
Spikatrix

Reputation: 20244

printf("%d is what I entered\n", &number);

is wrong because %d(in the printf) expects an argument of type int, not int*. This invokes Undefined Behavior as seen in the draft (n1570) of the C11 standard (emphasis mine):

7.21.6.1 The fprintf function

[...]

  1. If a conversion specification is invalid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

Fix it by using

printf("%d is what I entered\n", number);

Then, Why does scanf require & before the variable name?

Keep in mind that when you use number, you pass the value of the variable number and when you use &number, you pass the address of number(& is the address-of operator).

So, scanf does not need to know the value of number. It needs the address of it (an int* in this case) in order to write the input into it.

printf, on the other hand, does not require the address. It just needs to know the value (int, in this case) to be printed. This is why you don't use & before the variable name in printf.

Upvotes: 11

songyuanyao
songyuanyao

Reputation: 172864

You're using operator& on number, means take address of it, so you're not printing the value of number, but the address of it, you should:

printf("%d is what I entered\n", number);

Upvotes: 7

Related Questions