CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is the output of
void main()
{
int a = (1,2,3);
printf("%d",a);
}

with reason.


Posted by: Kulminder Singh on 12/12/2007

Contact Kulminder Singh  Contact Kulminder Singh
Category C Interview Questions
Rating (4.8) By 5 users
Added on 12/12/2007
Views 3453
Rate it!
Answers:

The answer is 3

The value which is last in the bracket is assigned to a(the variable).If it would have been any other value except 3,then it would have been assigned to 'a'.



 Posted by: Suprit    

Contact Suprit  Contact Suprit

Ans : 3

This is because of the comma(,) operator. Whenever any value is assigned using the comma operator, then the last value is assigned to the variable.

Here a = (1,2,3);
it is similar to
a=1;
a=2;
a=3;

if i print value of a after all the three statements, then it will print 3.

Let me take another example:

void main()
{
int a=(2,3,4);
int b= (a,10);
int c = a+b;
printf("Value of c = %d",c);
getch();
}

Here a will contain the value 4 and b will contain the value 10. Hence the value of c will be 14.



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed

Ans : 3

This is because of the comma(,) operator. Whenever any value is assigned using the comma operator, then the last value is assigned to the variable.

Here a = (1,2,3);
it is similar to
a=1;
a=2;
a=3;

if i print value of a after all the three statements, then it will print 3.

Let me take another example:

void main()
{
int a=(2,3,4);
int b= (a,10);
int c = a+b;
printf("Value of c = %d",c);
getch();
}

Here a will contain the value 4 and b will contain the value 10. Hence the value of c will be 14.



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed

3
First a is assigned to 1 then it is replaced by 2 and then 3.
which gives the final output to be 3.



 Posted by: pavan    

Contact pavan  Contact pavan

Ans is : 3
if it is int a = (1,2,3,4);
the answer will be 4.



 Posted by: vipin    

Contact vipin  Contact vipin

The OutPut Will be 3
because the precedence of braces() is right to left
so 3 will be assigned to int a.



 Posted by: Gurupreet    

Contact Gurupreet  Contact Gurupreet

output will be 3 because compiler
read from right firstly he read 3
and assign a=3 and then print.



 Posted by: krishan kumar agrawal    

Contact krishan kumar agrawal  Contact krishan kumar agrawal

Compile time error will be generated as the integer a is not correctly initialized.



 Posted by: sandip    

Contact sandip  Contact sandip

syntax error, a is not an array.



 Posted by: ranjit pise    

Contact ranjit pise  Contact ranjit pise

The answer will be: 3
Reason: The comma operator has associativity from left to right like assignment operator. So the right most value i.e. 3 will be assigned to a.



 Posted by: sanjay    

Contact sanjay  Contact sanjay

output is 3 becuse 3 is assigned to a and it get printed.



 Posted by: Pranjal    

Contact Pranjal  Contact Pranjal

3



 Posted by: prashant kumar jha    

Contact prashant kumar jha  Contact prashant kumar jha

answer will be
3
value of a will updated two times and last value which is 3 will display



 Posted by: abhiroop    

Contact abhiroop  Contact abhiroop

3
whatever the integer values assigned as in the program given, the final integer will be the answer for example if it is as below
i-(2,3,4)
answer will be 4.



 Posted by: prudhvi    

Contact prudhvi  Contact prudhvi

It will take the last value that is 3 since it is the latest value assigned to it.



 Posted by: kumar    

Contact kumar  Contact kumar

3
whatever the last one in bracket is the output.because the last answer is overwritten on the previous ans.



 Posted by: ruchi    

Contact ruchi  Contact ruchi

First right hand side of the = is solved i.e (1,2,3)
It has 3 expression separated by coma, like (exp1,exp2,exp3)
it is evaluated from left to right
so exp1 next exp2 finally exp3 is evaluated so the final evaluated result will be assigned to a
so the answer is 3



 Posted by: shantha kumara.K.R    

Contact shantha kumara.K.R  Contact shantha kumara.K.R

Output is 3,
because in first a became 1 then it changes to 2 ofter that it changes 3,
Now a value is 3 then we print a value , so the output is 3.
a->1,a=(1)

a->2;a=(1,2)

a->3, a=(1,2,3)
so now a vaule is 3...



 Posted by: rookmini    

Contact rookmini  Contact rookmini

This will give u compilation error..That
Declaration is not allowed here....



 Posted by: Vandana    

Contact Vandana  Contact Vandana

int a = (1,2,3);

translates into
a = 1;
a = 2;
a = 3;

So, it will print 3.



 Posted by: Priyabrata Das    

Contact Priyabrata Das  Contact Priyabrata Das

The answer will be 3. because in c language the assignment of values for the variables will be done from right to left.so right most 3 will be assigned to the variable 'a'



 Posted by: p.karthikeyan    

Contact p.karthikeyan  Contact p.karthikeyan

Output is 3,Because the first(1)
value is override by second(2) and second(2) value overide by third(3).



 Posted by: Rathindra Nath Singha    

Contact Rathindra Nath Singha  Contact Rathindra Nath Singha

output:
3
reason:we are giving only oone variable for group pf values



 Posted by: prashant    

Contact prashant  Contact prashant

3



 Posted by: shivendra kumar    

Contact shivendra kumar  Contact shivendra kumar

65
because the variable assigned is not assigned properly so its return the garbage values.



 Posted by: Navanee    

Contact Navanee  Contact Navanee

When we are using the printf statement the right most value will be accessed by the variable.That's why 3 is printed.



 Posted by: cnu    

Contact cnu  Contact cnu

output: 3
Since printf statement will take up the last value specified in the list of arguments separated by comma.



 Posted by: priya    

Contact priya  Contact priya

Answer is 3 because in initialization final value is taken.



 Posted by: vikas    

Contact vikas  Contact vikas

3
Because the last assigned value will be taken into account other values(1,2) has no effect.



 Posted by: gayathri    

Contact gayathri  Contact gayathri

Ans is a = 3
because as a is single variable so it can hold only one value.First 1 is assigned to a then 1 is overwritten by 2 and then by three so current value is 3 .



 Posted by: Anish Rana    

Contact Anish Rana  Contact Anish Rana

ANS : 3.
Reason :U are giving three i/ps.
When executing the result it will print 3 because FIFO



 Posted by: Ibu    

Contact Ibu  Contact Ibu

3
Because first the value of a will be 1 after that it will overwrite by 2 and finally value of a will be 3.



 Posted by: rakesh chaudhary    

Contact rakesh chaudhary  Contact rakesh chaudhary

The answer is 3.

Because,since the ',' operation value is the right operand,3 will be assigned to a.



 Posted by: rangaswamy reddy    

Contact rangaswamy reddy  Contact rangaswamy reddy

Output is 3.

'a' is assigned to the last number when braces are used otherwise '1' would have been the o/p.

i.e, a=1,2,3;
printf("%d",a);

would print '1'.



 Posted by: Chethan    

Contact Chethan  Contact Chethan

Here a=3

Reason being that comma has lower precedence, so it will take into account the latest value of variable a.



 Posted by: vishwa mohan    

Contact vishwa mohan  Contact vishwa mohan

printf the address of 1
becoz by default array name holds the base address or the address of first element of array.



 Posted by: malaram kumhar    

Contact malaram kumhar  Contact malaram kumhar

printf the address of 1
becoz by default array name holds the base address or the address of first element of array.



 Posted by: malaram kumhar    

Contact malaram kumhar  Contact malaram kumhar

Answer is 3 because we had using the comma operator. It take last value of intialized variable. Hence 3 was assined to variable of 'a'. So, output will be 3.

I hope useful to u



 Posted by: Ramanadhan K    

Contact Ramanadhan K  Contact Ramanadhan K

ANS: 3

because only last assignment it will take . we have assigned 3 as last ,so ans is 3.



 Posted by: jpavan    

Contact jpavan  Contact jpavan

Answer 3.
In this case, during the initialisation of variable "a", values 1, 2 and 3 are assigned to "a" in the order 1->2->3.

So finally 3 will be stored in a.



 Posted by: Aneeb    

Contact Aneeb  Contact Aneeb

Answer will 3.Because first it is taking 1 after that 2 and then 3.This is equal to this:
a=1;
a=2;
a=3;



 Posted by: sonu    

Contact sonu  Contact sonu

output is 3.it take last value of intilized variable.hance 3 was assigned to "a".



 Posted by: Mohit Giri    

Contact Mohit Giri  Contact Mohit Giri


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Write Addition of two numbers using Bitwise operators.
View Answer
How much memory does a static variable takes?
View Answer
What is file pointer and its working method?
View Answer
How to get string in files and print the string in the reverse order?
View Answer
How I can add two numbers in c language without using Arithmetic operators?
View Answer
What is pointers and its uses?
View Answer
What is the difference between structure and union?
View Answer
What is pointer?
View Answer
Can we use functions within a structure?
View Answer
void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12588/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.7