The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
150 views
Q) What is the output of the following C Program fragment

#include<stdio.h>

int main()

{

int a = 4, b = 3;

printf("%d",a+++++b);

return 0;

}

 

A) 7      B) 8             C) 9               D) Error
asked in Programming by Loyal (9.5k points) | 150 views

2 Answers

0 votes

If there is no space between the post increment and preincrement operators, then there will be an error. Else it will run as usual 

answered by Active (3.4k points)
0

Please see the source of question

0
@Sayan Bose,While executing the code the error which popped up was
"lvalue" required.
0
Yes if all the + are written one after the other(without any space) then compilation error is "Lvalue required" but I am not able to understand what a+++++b implies i.e. whether it has any significant meaning in programming.
+1
@Sayan Bose,A slight modification. The code will work fine in the following manner:-

printf("%d",(a++)+(++b));

Parentheses set a priority. Executing this way the answer is 8.
0 votes
OPTION: B is correct.

The output of this program will be 8 as the unary increment operator works right to left so in the statement a+++++b it will first increment b to 4 and will then perform addition and after that will update a.

 

Initially, a=4, b=3;

in a++ + ++b, b will be 4 and a will remain 4, so addition will be 4+4=8. So the output will be 8.

After printf the value of a will become 5.
answered by (13 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

40,845 questions
47,507 answers
145,771 comments
62,262 users