-4

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 be shown. Basically I am interested in knowing how printf works. Does it have a list of variables in a table sort of things. If a table is there from where it will take the value of &a

SKD
  • 464
  • 1
  • 4
  • 16
SouthArcot
  • 21
  • 1
  • 3
  • 2
    What did your compiler say about that. If the answer is "nothing", then you need to increase the warning level, or get a better compiler. Your code invokes undefined behavior since the argument list doesn't match the format string. – user3386109 Nov 19 '15 at 05:52
  • Have you tried to run that code? What do you get when you do? The second half of your question doesn't seem to make any sense. Not sure what a table of variables has to do with `printf`. It takes the value of `&a` from what was passed into its arguments. – kaylum Nov 19 '15 at 05:52
  • why didn't you use `int a = 10;` ? – SKD Nov 19 '15 at 05:54

3 Answers3

3

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).

user23573
  • 2,479
  • 1
  • 17
  • 36
2

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.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
-1

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

john elemans
  • 2,578
  • 2
  • 15
  • 26
  • This is the correct answer! The above code is not "undefined behavior" it's more like "unintended behavior". – user23573 Nov 19 '15 at 06:36
  • 5
    @BogdanWilli The C specification disagrees with you. §7.21.6/9 says, *"If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined."* Also, many 64-bit systems have 64-bit pointers, and 32-bit `ints`, so printing a pointer as an `int` would only print half of the pointer. – user3386109 Nov 19 '15 at 06:58
  • I already see you are going to downvote my answer. But both, Visual Studio 12.0 and GCC 4.6.3 agree with me. And please read [this page](https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx) from microsoft where it states that int is always 32 bit on VS2015. – user23573 Nov 19 '15 at 07:18
  • 2
    @BogdanWilli 1) it's true that under Microsoft compilers `int` is always 32bit, but it may be not true on other environnments. 2) even if `int` is 32 bits, a _pointer_ to `int` is definitely 64 bit under a 64 bit system and if you use the `%d` format specifier, it will actually only print the first 32 bits of the address. – Jabberwocky Nov 19 '15 at 11:02