954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic Concept

# include<stdio.h>
void main()
{  int p;
    p = (1,2,2,100,1);
    printf("%d",p);
}


I dont know what will be the output . Can anyone please tell me what exactly second line implies ?? :rolleyes:

harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
 

comma ' , ' operator has left to right associativity

# include<stdio.h>
void main()
{  int p;
    p = 1,2;
    printf("%d",p);
}

In this case output will be 1

But if we apply ( )
then last value is assigned to p and output will be 2

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
# include<stdio.h>
void main()
{  int p;
    p = (1,2,2,100,1);
    printf("%d",p);
}

I dont know what will be the output . Can anyone please tell me what exactly second line implies ?? :rolleyes:

though p should be declare as an array int p[]

keevin
Newbie Poster
3 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

if p were an array, then the initializer list would have to be in braces, not parentheses, like this

int p[] = {1,2,2,100};


But the above is entirely different than the code you originally posted.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

># include
>void main()
>{ int p;
> p = 1,2;
> printf("%d",p);
>}
>In this case output will be 1

what will be the output, if i make a bit of change in the existing code, i mean:::

# include
void main()
{ int p = 1,2;
printf("%d",p);
}

i got error in this case, i dont know , why its producing error in this case, when it looks similar to the old one?? :?:

Rajendra Bansal
Newbie Poster
1 post since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

parentheses are required as shown in previous posts.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The same..
Paranthesis will do the work for getting p=(1,2)
so o/p will be 2.

akshatvig19
Newbie Poster
2 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You