Sunday, 18 August 2013

Difference between printf("%d") and printf("%d",x)


1. When we write printf("%d",x); the compiler will print the
value of x. But where there is nothing after %d so compiler will show  garbage value.

2. When we use %d the compiler internally uses it to access the
argument in the stack called argument stack. Compiler determines
the offset of the variable depending on the format specification
string. Now when we write printf("%d",x) then compiler first accesses the top most element in the argument stack of the printf which is %d and depending on the format string it calculated to offset to the actual data variable in the memory which is to be printed. Now when only %d will be present in the printf then compiler will calculate the correct offset (which will be the offset to access the integer variable) but as the actual data object is to be printed is not present at that memory location so it will print what ever will be the contents of that memory location.

3. Some compilers check the format string and will generate an error.

Related Posts:

  • Decision Making Structure In C Decision making structures allows the programmer to specify one or more conditions to be evaluated or tested by the compiler or , along with a statement or set of statements to be executed if the condition is  … Read More
  • Storage Classes in c A storage class in C defines the scope (visibility) and life-time of a variables and/or a functions .These specifiers precede the type that they modify. There are some storage classes, which are  used in a C Pr… Read More
  • Loops in C  This is an attempt at explaining the loops in C .It is not complete, just the basics. C Programming Loop is  a construct which execute a set of statements certain number of times… Read More
  • Operators in C An operator is a symbol that instruct  the compiler to perform specific mathematical or logical operation. C language  has following type of operators: C Programming •  Arithmetic Operators •  … Read More
  • Constant and Literals in c The constants refer to fixed values that the program may not change during its execution. These values are also called literals. C Programming Constants can be of any of the  data types like an integer … Read More

0 comments:

Post a Comment