Output of C programs | Set 8

Predict the output of below C programs.
Question 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int x = 5, p = 10;
    printf("%*d", x, p);
  
    getchar();
    return 0;
}

chevron_right


Output:

   10

Explanation:
Please see standard printf function definition

       int printf ( const char * format, ... );

format: String that contains the text to be written to stdout. It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested. The number of arguments following the format parameters should at least be as much as the number of format tags. The format tags follow this prototype:

       %[flags][width][.precision][length]specifier

You can see details of all above parts here http://www.cplusplus.com/reference/clibrary/cstdio/printf/.



The main thing to note is below the line about precision
* (star): The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

So, in the above example 5 is precision and 10 is the actual value to be printed. (Thanks to Ajay Mishra for providing the solution)



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
  char arr[]  = "geeksforgeeks";
  char *ptr  = arr;
  
  while(*ptr != '\0')
      ++*ptr++;
  printf("%s %s", arr, ptr);
  
  getchar();
  return 0;
}

chevron_right


Output:  hffltgpshfflt

Explanation:
The crust of this question lies in expression ++*ptr++.

If one knows the precedence and associativity of the operators then there is nothing much left. Below is the precedence of operators.

   Postfixx ++            left-to-right
   Prefix  ++             right-to-left
   Dereference *          right-to-left

Therefore the expression ++*ptr++ has following effect
Value of *ptr is incremented
Value of ptr is incremented



Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
  signed char i=0;
  for(; i >= 0; i++);
  printf("%d\n", i);
  
  getchar();
  return 0;
}

chevron_right


Output: -128

Explanation:
Here, the first thing to be noticed is the semi-colon in the end of for loop. So basically, there’s no body for the for loop. And printf will print the value of i when control comes out of the for loop. Also, notice that the first statement i.e. initializer in the for loop is not present. But i has been initialized at the declaration time. Since i is a signed character, it can take value from -128 to 127. So in the for loop, i will keep incrementing and the condition is met till roll over happens. At the roll over, i will become -128 after 127, in that case condition is not met and control comes out of the for loop.



Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
void fun(const char **p) { }
int main(int argc, char **argv)
{
   fun(argv);
   getchar();
   return 0;
}

chevron_right


Output: Compiler error.

Explanation:
Parameter passed to fun() and parameter expected in definition are of incompatible types. fun() expects const char** while passed parameter is of type char **.

Now try following program, it will work.

void fun(const char **p) { }
int main(int argc, char **argv)
{
   const char **temp;
   fun(temp);
   getchar();
   return 0;
}


My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.