Result of Printf (“%d”, &a)

后端 未结 3 1254
长情又很酷
长情又很酷 2020-12-22 14:17

I have a code in C .

    int a; 
    a =10;
    printf (\"%d\", &a); 

I want to know if some garbage will be printed or error msg will

相关标签:
3条回答
  • 2020-12-22 14:37

    This is a beginners question and deserves to be answered properly. (I'm startled by the downvotes and the non-constructive comments)

    Let me give a walk-throu line by line:

    int a; 
    

    The first line declares a variable with the name a. The variable is of type integer and with normal compilers (Microsoft Visual C++ on Windows, GCC on linux, Clang on Mac) this usually 32 bits wide. The integer variable is signed because you did not specify unsigned. This means it can represent values ranging from –2,147,483,648 to 2,147,483,647.

    a =10;
    

    The second line assigns the value 10 to that variable

    printf ("%d", &a); 
    

    The third line is where you get the surprising result. "%d" is the "format string" it defines how the variables, given as further arguments are formatted and subsequently printed. The format string comprises of normal text (which will be printed normally) and control-sequences. the control sequences start with the character % and end with a letter. The letter specifies the argument type that is expected. d in the above case expects a integer value.

    The problem with your code is that you do not specify an itenger value, you give the address of an integer value (with the address-of & operator). The correct statement would be:

    printf ("%d", a);
    

    Simple.

    I recommend that you have a read on a good C book. I recommend "The C programming language" which is from the original authors of the language. You find this book on amazon, but you find it also online.

    You can find the same answers reading in the standard. But to be honest, these documents are not easy reading. You can find a draft of the C11 standard here. The description of the formatting options starts on page 309. (drafts are usually good enough for programming purposes, and are usually available for free).

    0 讨论(0)
  • 2020-12-22 14:44

    It will print the address of the variable 'a'. Remember that the & operator returns the address of the operand.

    0 讨论(0)
  • 2020-12-22 14:55

    This is undefined behaviour.

    If you are new to C, this may be a surprise. However the C specification defines the behaviour of certain programs (called "correct programs"). If you do not follow the rules in the specification, then your program is not correct, and literally anything may happen. The above link goes into more detail.

    Your program doesn't follow the rules because the %d format specifier for printf must have a corresponding argument of type int (after the default argument promotions), but instead you pass an argument of type int *.

    Since it is undefined behaviour, the output is meaningless and it is generally not worthwhile to investigate.

    0 讨论(0)
提交回复
热议问题