# basic C questions

Page 1 of 1

## 12 Replies - 2218 Views - Last Post: 23 May 2009 - 07:08 AMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=105490&amp;s=938063ae8835eb4e400220ba21e014cd&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 31
• Joined: 20-May 07

# basic C questions

Posted 16 May 2009 - 12:13 PM

```int main(void)
{ int a=3,b=4,c,c1,c2,c3,c4;
c=a++ + ++b;
c1=a+++b;
c2=a+++ +b;
c3=a+ +b;
c4=a+ -b;
cout<<c<<endl<<c1<<endl<<c2<<endl<<c3<<endl<<c4<<endl;
getchar();
return 0;
}
```

output:
8
9
10
11
1

*Mod Edit: Fixed code tags:

This post has been edited by NickDMax: 16 May 2009 - 01:11 PM

Is This A Good Question/Topic? 0

## Replies To: basic C questions

### #2 NickDMax

Reputation: 2235
• Posts: 9,216
• Joined: 18-February 07

## Re: basic C questions

Posted 16 May 2009 - 01:17 PM

Start here: Operator Precedence which tells you the order in which operators will be executed.

c = a++ + ++b -> c = a + (b += 1), a += 1
c1 = a+++b -> c1 = a + b, a += 1
c2 = a+++ +b -> c2 = a + ( +b ), a += 1
etc.etc.

### #4 gabehabe

• GabehabeSwamp

Reputation: 1375
• Posts: 10,951
• Joined: 06-February 08

## Re: basic C questions

Posted 16 May 2009 - 01:21 PM

Your question isn't really very specific, but let's take a look.

This is a pretty horrible example of pre/post incrementation operators, I don't think I've ever seen any actual code in that form, but I guess it's a good way to understand how they work.

I've commented each line on the how. Hopefully you'll understand from this.

```#include <iostream>

using namespace std;

int main(void)
{ int a=3,b=4,c,c1,c2,c3,c4;
c=a++ + ++b; // 3 + 5 (a will become 4 AFTER this equation, b become 5 BEFORE the equation)
c1=a++ +b; // 4 + 5 (a will become 5 AFTER this equation, b remains the same)
c2=a+++ +b; // 5 + 5 (a will become 6 after this equation, b remains the same)
c3=a+ +b; // 6 + 5 (a does not change, +b simple says "positive b")
c4=a+ -b; // 6 + -5 (a remains the same, plus negative b )
// adding a negative number turns it into a subtraction
// so the final equation gives 1
cout<<c<<endl<<c1<<endl<<c2<<endl<<c3<<endl<<c4<<endl;
cin.get();
return 0;
}
```

This post has been edited by gabehabe: 16 May 2009 - 01:22 PM

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 17 May 2009 - 04:50 AM

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 17 May 2009 - 06:35 AM

My First question is being solved.I am adding question in continuity not to create many different topics.I hope my above good friends will help me solved my queries in future time. Here's the Question

int x=10,y;
y=--x--;
cout<<x<<endl<<y<<endl;

compile time error:non l-value decreament

I want to know the reason behind this. I have mentioned my thoughts below.

let me tell you what I analysed:
the associativity of -- operator is from Left to Right.
so,
first x--(it will be evaluated in the next step as being a post increament.)
y=--x,x-=1

it will be like y=9--

As we cann't apply decreament operator on a constant so it's showing this error.

This post has been edited by JackOfAllTrades: 17 May 2009 - 07:11 AM

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 17 May 2009 - 06:55 AM

My next question :::::

/*int i=8;
i+++;
cout<<i; */

//expected primary-expression before ';' token

Why is this error.I could not make out any defined answer.

This post has been edited by JackOfAllTrades: 17 May 2009 - 07:10 AM

• Saucy!

Reputation: 5864
• Posts: 22,990
• Joined: 23-August 08

## Re: basic C questions

Posted 17 May 2009 - 07:13 AM

No more huge colored text. Be considerate.

Simple. First, there are rules to programming in C, and you're violating those rules.

Then, what do you expect this to do?
```int i=8;
i+++;
cout<<i;
```

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 17 May 2009 - 07:19 AM

[quote name='JackOfAllTrades' date='17 May, 2009 - 06:13 AM' post='644823']
No more huge colored text. Be considerate.

Simple. First, there are rules to programming in C, and you're violating those rules.

Then, what do you expect this to do?
[code]int i=8;
i+++;
cout<<i;

Well I won't put coloured text.

@Jackofalltrades ,You didn't mention the reason of above two questions.

### #10 NickDMax

Reputation: 2235
• Posts: 9,216
• Joined: 18-February 07

## Re: basic C questions

Posted 17 May 2009 - 10:40 AM

well the second one i+++ is kind of nonsence since you have the + operator without an associated piece: i++ + ? -- the compiler is looking for the other piece of the puzzle. It "expects" to find an expression and instead it found a semi-colon. Basically you made a syntax error.

Next --x--, it does seem as though this should evaluate as --x, x+=1 and in fact it looks as though the compiler you are using tried to do that very thing. This is why it gave you the error "no L-value".

An L-Value is a value that has an address in memory. Therefore it may be assigned a value. The operator precedence states that post-increment happens before pre-increment. So, hypothetically, first the compiler takes x and puts its current value into temporary storage (a register more than likey), then it increments x (done with the post-increment). Now it goes to do the pre-increment, only finds that the temporary value has no address to store the result in.

so it is like you did this: --(x--) the compile does not know how to increment an expression, only a variable. I.e. it can't increment an intermediate value unless that value is an L-Value (think pointers).

Of course I don't know of anyway to ask a compiler to show what it "tried to do". There is no "give me your best guess" compiler option. So, I really don't know if this is exactly what happened but I can tell you that using pre-and post-increment of a value in the same expression has, "undefined behavior"

for example most compilers will compile the following
```#include <iostream>

int main() {
int y, x = 5;
y=x++ + x++;
std::cout << "y = " << y << std::endl;
std::cout << "x = " << x << std::endl;
return 0;
}
```

Compilers compile it, but the result is not guaranteed because the standard does not clearly define how the compiler would deal with this.

Even worse is something like this:
```#include <cstdio>

int main() {
x = 0;
printf("%d %d %d", x++, x++, ++x);
return 0;
}
```
Where the first code example actually gets handled in a pretty standard way, this example is even worse because it combines 2 undefined behaviors. C/C++ does not defined the order that the compiler has to evaluate the expressions in a function call, so it may work left to right, or right to left.

big picture: don't use multiple inc and dec of the same variable in the same expression.

### #11 jcmaster2

Reputation: 2
• Posts: 183
• Joined: 27-April 09

## Re: basic C questions

Posted 17 May 2009 - 05:39 PM

NickDMax, on 17 May, 2009 - 09:40 AM, said:

well the second one i+++ is kind of nonsence since you have the + operator without an associated piece: i++ + ? -- the compiler is looking for the other piece of the puzzle. It "expects" to find an expression and instead it found a semi-colon. Basically you made a syntax error.

Next --x--, it does seem as though this should evaluate as --x, x+=1 and in fact it looks as though the compiler you are using tried to do that very thing. This is why it gave you the error "no L-value".

An L-Value is a value that has an address in memory. Therefore it may be assigned a value. The operator precedence states that post-increment happens before pre-increment. So, hypothetically, first the compiler takes x and puts its current value into temporary storage (a register more than likey), then it increments x (done with the post-increment). Now it goes to do the pre-increment, only finds that the temporary value has no address to store the result in.

so it is like you did this: --(x--) the compile does not know how to increment an expression, only a variable. I.e. it can't increment an intermediate value unless that value is an L-Value (think pointers).

Of course I don't know of anyway to ask a compiler to show what it "tried to do". There is no "give me your best guess" compiler option. So, I really don't know if this is exactly what happened but I can tell you that using pre-and post-increment of a value in the same expression has, "undefined behavior"

for example most compilers will compile the following
```#include <iostream>

int main() {
int y, x = 5;
y=x++ + x++;
std::cout << "y = " << y << std::endl;
std::cout << "x = " << x << std::endl;
return 0;
}
```

Compilers compile it, but the result is not guaranteed because the standard does not clearly define how the compiler would deal with this.

Even worse is something like this:
```#include <cstdio>

int main() {
x = 0;
printf("%d %d %d", x++, x++, ++x);
return 0;
}
```
Where the first code example actually gets handled in a pretty standard way, this example is even worse because it combines 2 undefined behaviors. C/C++ does not defined the order that the compiler has to evaluate the expressions in a function call, so it may work left to right, or right to left.

big picture: don't use multiple inc and dec of the same variable in the same expression.

OR just say this is undefined in Standard C...

### #12 NickDMax

Reputation: 2235
• Posts: 9,216
• Joined: 18-February 07

## Re: basic C questions

Posted 17 May 2009 - 06:46 PM

Quote

NickDMax ....it's always nice to get your reply..your explanation is great.but i would like to get one good lesson of L-value from your choice.

see here.

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 23 May 2009 - 06:48 AM

Why does global variable is taken as static .Why the value is intialised to being zero.

I would like your thought on value of i getting zero.
``` int i;
int main(void)
{
for(;i;)
printf("\n Wipro Infotech");
getch();
return 0;
}

```

output: nothing

Reputation: 0
• Posts: 31
• Joined: 20-May 07

## Re: basic C questions

Posted 23 May 2009 - 07:08 AM

I tried this two question.I have some basic doubts on it.

```int main(

int a=5,b=10;
if(a==b,a++)
printf("Good\n");
else
printf("%d",a);
getch();
return 0;
}

```

output:good
6

I know that in case of comma operator operation will be done fromleft to right.
so 1st a==b will be performed
2nd a,a=a+1
Output is right, but still i need your thoughts on this.
priority of operator
--------------------
increament operator
equality operator
comma operator

I am just little confused.

The second question:

```main(){
int k=35;
printf("\n %d %d %d ",k==35,k=50,k>40);
}

```

How is the output is 0 50 0

This post has been edited by pradosh: 23 May 2009 - 07:54 AM