Output of C programs | Set 8

04 Aug, 2021
Predict the output of below C programs. Question 1:
c
#include<stdio.h>int main(){    int x = 5, p = 10;    printf("%*d", x, p);     getchar();    return 0;}
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 width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
In simple words, the value to be printed must have atleast the number of characters corresponding to the width. Here the width is 5, the value to printed is 10. Since it just has two characters, in order to make it 5, three blank spaces will be printed preceding 10.
So 10 gets printed after 3 spaces!!Question 2 
c
int main(){  char arr[]  = "geeksforgeeks";  char *ptr  = arr;   while(*ptr != '\0')      ++*ptr++;  printf("%s %s", arr, ptr);   getchar();  return 0;}
Output:  hffltgpshffltExplanation: 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 incrementedQuestion 3 
c
int main(){  signed char i=0;  for(; i >= 0; i++);  printf("%d\n", i);   getchar();  return 0;}
Output: -128Explanation: 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 
c
#include <stdio.h>void fun(const char **p) { }int main(int argc, char **argv){   fun(argv);   getchar();   return 0;}
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;
}
getthemm
sheltonsharonj
anikaseth98
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Different ways to copy a string in C/C++
Output of C++ Program | Set 1
Output of Java Program | Set 3
Output of C++ programs | Set 47 (Pointers)
Output of Java Programs | Set 12
Output of Java Program | Set 7
Output of C programs | Set 59 (Loops and Control Statements)
unsigned specifier (%u) in C with Examples
How to show full column content in a PySpark Dataframe ?