void main()
{
int a=1;
printf("%d %d %d",a,++a,a++);
}
the output is supposed to be 1 2 2....but it is 3 3 1
this is due to calling conventions of C. if anyone can
explain me how it happens?
Answers were Sorted based on User's Feedback
Answer / sumant
In C the parameters are pushed on the stack from right to
left. So
1> it will push a=1 on the stack and do a++ making a=2
2> it will porform ++a making a = 3 and push value 3
3> it will push a on the stack which is 3
so the stack will have values 1 3 3 and it will POP in
the reverse order and thus printf will display 3 3 1
| Is This Answer Correct ? | 52 Yes | 11 No |
Answer / vishnu
first calculations will be done from right to left and then
prints accroding to the parameters passed.
| Is This Answer Correct ? | 29 Yes | 8 No |
Answer / sathish
execution does from right to left and while printing it goes from left to right.
| Is This Answer Correct ? | 18 Yes | 3 No |
Output:3 3 1 This
is because,C's calling convention is from right to left.That
is ,firstly 1 is passed through the expression a++ and then
a is incremented to 2.Then result of ++a is passed.That is,a
is incremented to 3 and then passed.Finally,latest value of
a,i.e. 3,is passed.Thus in right to left order,1 ,3, 3 get
passed.Once printf() collects them,it prints them in the
order in which we have asked it to get them printed(and not
the order in which they were passes).thus 3 3 1 gets
printed.
| Is This Answer Correct ? | 8 Yes | 2 No |
Answer / hemanth
All,
output of above code is compiler depended i.e the order of
evalulation.
| Is This Answer Correct ? | 8 Yes | 5 No |
Answer / keerthi
while printing the output it starts from right hand
side ..so first 'a++' value is printed then '++a' value and
last it prints 'a' value
| Is This Answer Correct ? | 12 Yes | 16 No |
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
how to find sum of digits in C?
What are static variables, and where are they stored?
what is Array?
write a c program in such a way that if we enter the today date the output should be next day's date.
Sir,please help me out with the code of this question. Write an interactive C program that will encode or decode multiple lines of text. Store the encoded text within a data file, so that it can be retrieved and decoded at any time. The program should include the following features: (a) Enter text from the keyboard, encode the text and store the encoded text in a data file. (b) Retrieve the encoded text and display it in its encoded form. (c) Retrieve the encoded text, decode it and then display the decoded text. (d) End the computation. Test the program using several lines of text of your choice.
pgm to find number of words starting with capital letters in a file(additional memory usage not allowed)(if a word starting with capital also next letter in word is capital cann't be counted twice)
Explain how do you print an address?
What is a loop?
How can you be sure that a program follows the ANSI C standard?
What is operator precedence?
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.