944,147 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1949
  • C RSS
Sep 29th, 2005
0

Basic Concept

Expand Post »
  1. # include<stdio.h>
  2. void main()
  3. { int p;
  4. p = (1,2,2,100,1);
  5. printf("%d",p);
  6. }

I dont know what will be the output . Can anyone please tell me what exactly second line implies ?? :rolleyes:
Similar Threads
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Sep 29th, 2005
0

Re: Basic Concept

comma ' , ' operator has left to right associativity

  1. # include<stdio.h>
  2. void main()
  3. { int p;
  4. p = 1,2;
  5. printf("%d",p);
  6. }
In this case output will be 1

But if we apply ( )
then last value is assigned to p and output will be 2
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 30th, 2005
0

Re: Basic Concept

Quote originally posted by harshchandra ...
  1. # include<stdio.h>
  2. void main()
  3. { int p;
  4. p = (1,2,2,100,1);
  5. printf("%d",p);
  6. }

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[]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keevin is offline Offline
3 posts
since Sep 2005
Sep 30th, 2005
0

Re: Basic Concept

if p were an array, then the initializer list would have to be in braces, not parentheses, like this
  1. int p[] = {1,2,2,100};

But the above is entirely different than the code you originally posted.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 10th, 2005
0

Re: Basic Concept

># include<stdio.h>
>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<stdio.h>
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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rajendra Bansal is offline Offline
1 posts
since Sep 2005
Oct 10th, 2005
0

Re: Basic Concept

parentheses are required as shown in previous posts.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 13th, 2005
0

Re: Basic Concept

The same..
Paranthesis will do the work for getting p=(1,2)
so o/p will be 2.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akshatvig19 is offline Offline
2 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Fibonacci number
Next Thread in C Forum Timeline: strtok() function questions <C programming>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC