Explain the output of these short C programs

1.
main()
{
static int var = 4;
printf("%d",var--);
if(var)
  main();
}
The output is given as 4321

2.
main()
{
static char n [5][20]={"Amar","Akbar","Anthony","Vijay","Amit"};
int i;
char*t;
t=n[3];
n[3]=n[4];
n[4]=t;
for(i=0;i<=4;i++)
    printf("%s",n[i]);
}
This will generate a compiler error due to a property that array names can not be modified or something like that. Please explain it in detail.

3.
void main()
{
int i=5;
printf("%d",i+++++i);
}
They say this will be parsed like i++ ++ +i which is illegal therefore there will be compiler error. Why is it not parsed like the following: i++ + ++i?

4.
main()
{
int i=0;
for(;i++;printf("%d",i));
printf("%d",i);
}
The output of this program is given as 1. But I think it should be 11. What do you think? Explain the program flow.

5.
main()
{
int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
printf("%d %d %d",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
}
The output is given as 3 6 1. How? How to process a[1]-a[0] and a[1][0]-a[0][0]?

Just explain the concepts which you know exactly. Need not have to answer all the programs if you don't know. Just give number for which you are answering.😀

Replies

  • Vishal Sharma
    Vishal Sharma
    1.

    You've declared var as static. Hence, it doesn't change its value at each recurrence. So, at each recurrence, it prints the value of var which is decremented at line 2 of the block. when var becomes 0, if condition is false hence program gives the output 4321

    2.

    You've used 2 dimensional array for writing the names. but, at the time of manipulation you're using single dimension array. Nothing more to explain over there.

    3.

    printf("%d",i+++++i);
    the i+++++i is the wrong use of operator, which causes ambiguity to the compiler. Different compilers may produce different outputs for that! The increment and decrement are generally used in loops, not like this. to know more go to#-Link-Snipped-#.

    4.

    #include
    main()
      {
    1. int i=0;
    2. for(;i++;printf("%d",i)); 
    3. printf("%d",i);
      }
    If you have a look at the for loop syntax, it is something like this.

    for( initialization ; condition ; increment/decrement)
    {
    [for statements]
    }
    and it flows like this...
    1st : initialization
    2nd: condition check
    3rd: getting into the [for block]
    4th: increment/decrement, the re-checking the condition.

    But, in your code the for loop has increment at the condition check position. So, value of i changes to 1. then the code gets into for block and prints the value of i. At the place of increment, your code has print statement. for loop doesn't execute the print statements and since there is no change in the value of i, the loop breaks.
  • rahul69
    rahul69
    1.
    Simple example of use of Static variables and Recursion principle, u call main() until var>0 and keep printing values.

    2.
    Well I don't agree with #-Link-Snipped-# on this one, the problem lies in the lines
    n[3]=n[4];
    n[4]=t;
    and the reason is that u cannot copy values of arrays directly to another array (the reason being that arrays are of fixed sized, so there are chances of buffer overflow if the array being copied is larger than the first one). Same is the case with n[4]=t; but u can make pointer point to array and hence t=n[4]; will have no problem.

    3.
    printf("%d",i+++++i);
    It happens due to ambiguity and for proper parsing u can use parenthesis and it will work. i.e:

    printf("%d",(i++)+(++i));
    will work as it clearly defines the operations intended.

    4.
    for(;i++;printf("%d",i));
    As theory has been explained by Vishal, I will explain the output:
    Initialization: Skipped by ';'
    Checking i++; Since post increment follows use then change sequence so first value
    i is used (which is 0)so if(0) results false and checking fails and it comes out of
    loop and then i has been changed (i++ => i=i+1 => i=1) and hence when printed, it
    prints 1. Note that the for loop never runs even once (ie Suppose u had some statements
    inside loop they will not execute!)
    Hope it helps!
  • Mr.Don
    Mr.Don
    #-Link-Snipped-# 2. Well, for this problem, use *n[5] = {"v","w","x","y","z"} <- This declaration will solve all your issues and also will get the desired task completed.

    There your using [][] to copy string which had created the error/warning.
  • rahul69
    rahul69
    5.
    Here comes the final
    main()
    {
    int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
    printf("%d %d %d",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
    }
    To explain this output:3,6,1, I will explain from rightmost output since it is easiest to explain:
    a[1][0][0]-a[0][0][0];
    Here a[1][0][0]=2; a[0][0][0] =1, hence a[1][0][0]-a[0][0][0]=2-1=1

    Now an important concept, next a[0][0], this is not a value, but just a pointer to some address, and when we subtract two pointers what will we get? Well the answer is the "no of elements between those two pointers"
    So now between a[1][0] and a[0][0] (assuming row major😉 ) the elements are:9,8,3,7,2,2 ie 6
    now for a[1] and a[0] this (9,8) acts a single element. So a[1]-a[0] the elements are (9,8),(3,7),(2,2)
    i.e 3 so thats how u got 3,6,1😎
  • TheCSGuy
    TheCSGuy
    1. Haven't understood the first program yet. When the program calls main() for the first time from if with var value = 3, what about the first line in the main at that time? Is it just ignored? How?
  • TheCSGuy
    TheCSGuy
    rahul69
    5.
    Now an important concept, next a[0][0], this is not a value, but just a pointer to some address, and when we subtract two pointers what will we get? Well the answer is the "no of elements between those two pointers"
    So now between a[1][0] and a[0][0] (assuming row major😉 ) the elements are:9,8,3,7,2,2 ie 6
    now for a[1] and a[0] this (9,8) acts a single element. So a[1]-a[0] the elements are (9,8),(3,7),(2,2)
    i.e 3 so thats how u got 3,6,1😎
    Still not understood this. Do the following representation is correct?
    1 2
    9 8
    3 7

    and the other one is

    2 2
    1 4
    5 4
    Now a[0][0] means for the 0th(1st) matrix, 0th row right?
    Now just explain the following line.
    rahul69
    5.
    So now between a[1][0] and a[0][0] (assuming row major😉 ) the elements are:9,8,3,7,2,2 ie 6
    now for a[1] and a[0] this (9,8) acts a single element. So a[1]-a[0] the elements are (9,8),(3,7),(2,2)
    i.e 3 so thats how u got 3,6,1😎
  • rahul69
    rahul69
    Yeah, the representation is similar, it is actually a cubic structure (conceptually) and each cell is represented by (i,j,k).
    Now a[0][0] represents the memory location, i.e address of location of element accessed by a[0][0][0].
    To understand this question, u can practice 2D Arrays first and perform operations which will clear ur basics.Good Luck!!
  • rahul69
    rahul69
    TheCSGuy
    1. Haven't understood the first program yet. When the program calls main() for the first time from if with var value = 3, what about the first line in the main at that time? Is it just ignored? How?
    Did u mean this line:
    static int var = 4;
    This is a static variable and it retains its value between function calls.
    U need to study about this so that u understand its function. Go through the following
    link, it will help: The Static Keyword in C++ - Cprogramming.com

You are reading an archived discussion.

Related Posts

Dear Sir/Madam, i have researched about lightning harvesting before three months and without having proper guidance and funds . i am publish my unfinished project into you for make it...
It's an open secret that after disrupting the music player, mobile and tablet markets, Apple's eyeing disruption of the television market. WSJ today posted that the officials who're working with...
Hiiii friends... I have completed my B.tech from ECE branch..and i want tomake my career in Telecom Sector. Please suggest me what to do?????/
hi buddies pls need info on security systems with motion sensor and camera projects ,cos i want to use that as my final year project.i'd be glad to get help
hey guys i have to wrote C-CAT exam on 16 dec and admit cards are available to download but whenever i click on the "print the admit card" link the...