Confusion with ++ operator


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Confusion with ++ operator
# 1  
Old 08-23-2014
Code Confusion with ++ operator

Can anyone guide me whats happening in this program given below. I got the Output 7 7 12 49... i was expecting 5 16 9 25.
First is simple (3+1)*(3+1)
Second is again 3*3; i =4 now
Third i =5 then 5*5;
i don't know where i am going wrong!
Code:
#include<stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
	int i=3,j,k,l;
	j=PRODUCT(i+1);
	k=PRODUCT(i++);
	l=PRODUCT(++i);
	printf("%d %d %d %d\n",i,j,k,l);	
	return 0;
	
}

# 2  
Old 08-23-2014
Quote:
Originally Posted by Abhishek_kumar
Can anyone guide me whats happening in this program given below. I got the Output 7 7 12 49... i was expecting 5 16 9 25.
First is simple (3+1)*(3+1)
Second is again 3*3; i =4 now
Third i =5 then 5*5;
i don't know where i am going wrong!
Code:
#include<stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
	int i=3,j,k,l;
	j=PRODUCT(i+1);
	k=PRODUCT(i++);
	l=PRODUCT(++i);
	printf("%d %d %d %d\n",i,j,k,l);	
	return 0;
	
}

There is a big difference between a macro expansion and a function call. Your assignments to j, k, and l expand to:
Code:
	j=i+1*i+1; /* 3 + 3 + 1 -> 7 */
	k=i++*i++; /* 3 * 4 -> 12 leaving i set to 5 */
	l=++i*++i; /* 6 * 7 -> 42 leaving i set to 7 */

Are you sure you got 49 instead of 42 as the last number printed?
# 3  
Old 08-23-2014
I get 7 7 9 49

Apparently using incremental ops multiple times in the same statement is undefined in C.
# 4  
Old 08-23-2014
Quote:
Originally Posted by neutronscott
I get 7 7 9 49

Apparently using incremental ops multiple times in the same statement is undefined in C.
Yes, the behavior in those cases is unspecified (except that i should be 7 at the end). But with compilers I've used in the past, the results I quoted was what you would usually get. I had seen compilers that would give one of:
Code:
7 7 9 36
7 7 9 49
7 7 16 36
7 7 16 49

before (both with or without increments in all cases), but I had never seen one that did:
Code:
7 7 12 49

with immediate increments on i++ and using both values (3 * 4) on one, but doing both ++i operations and using the final value (7 * 7) on the other. Oh, well.
# 5  
Old 08-23-2014
My understanding is that * and ++expr operators have the same precedence in the C language but that ++exprs are always evaluated before any *.

Thus
Code:
i=5
(++i) * (++i)

gives 49 with both gcc and g++
# 6  
Old 08-24-2014
Quote:
Originally Posted by fpmurphy
My understanding is that * and ++expr operators have the same precedence in the C language but that ++exprs are always evaluated before any *.

Thus
Code:
i=5
(++i) * (++i)

gives 49 with both gcc and g++
My understanding is that the actual increment of i can occur at any time before the next sequence point. In the statements:
Code:
	k=i++ * i++;
	l= ++i * ++i;

the ; is a sequence point; the * is not a sequence point.

The submitter hasn't specified which compiler is being used in this example. Using the compiler on OS X 10.9.4 to build and run the sample program provided in the 1st message in this thread yields:
Code:
$ make t
cc     t.c   -o t
t.c:7:13: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
        k=PRODUCT(i++);
                   ^~
t.c:2:21: note: expanded from macro 'PRODUCT'
#define PRODUCT(x) (x*x)
                    ^
t.c:8:12: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
        l=PRODUCT(++i);
                  ^~
t.c:2:21: note: expanded from macro 'PRODUCT'
#define PRODUCT(x) (x*x)
                    ^
2 warnings generated.
$ ./t
7 7 12 42

It is perfectly legal for a compiler to perform the increments before the multiplication, but the C Standard doesn't require it to behave that way. And, if the compiler the submitter uses did behave that way, the output would be:
Code:
7 7 9 49
    or
7 7 16 49

rather than:
Code:
7 7 12 49

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If then statement confusion

#!/bin/bash PH=(KD 6S TC 3D) #playerhand TCIP=(AH) #topcard in play A=( "${TCIP::1}" ) # A B=( "${TCIP:1}" ) # H C=8 for e in ${PH}; do if ]; then echo "$e " >> /home/cogiz/validcards.txt else echo... (1 Reply)
Discussion started by: cogiz
1 Replies

2. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies

3. Shell Programming and Scripting

Confusion in hash

Hi folks, If a declare a direct hash , then the hash element works fine. my %test = ("test",1); print %test; print "\n"; Here in the above, the name of the hash is predeclared... Suppose now I need to create the hash elements dynamically in the for loop. $test="hash"; my... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

5. Programming

const_cast confusion

See code below. It appears that i and j inhabit the same address yet hold different values. Can anyone shed light on this? int main() { const int i= 3; int* j = const_cast<int*>(&i); *j = 5; cout << j << endl << &i << endl; cout << *j << endl << i; } (4 Replies)
Discussion started by: StuartH
4 Replies

6. UNIX for Dummies Questions & Answers

crontab confusion

I come across an entry in cron which is in such: 0 * * * * What is the first 0 indicating? 0 minute? meaning a script cron as such will run every minute? :confused: (2 Replies)
Discussion started by: user50210
2 Replies

7. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

8. Shell Programming and Scripting

Sed confusion

Hello all, I am trying to delete all the lines in a particular file having a pattern. The problem is that it has special characters and for some reason is not doing the job. For eg. src_file /home/test/filelist.txt :xxxx:ogog /home/test/RCH/ogogogg /home/test/RYHUJ/HHHH... (3 Replies)
Discussion started by: alfredo123
3 Replies

9. UNIX for Dummies Questions & Answers

wc command confusion

Can somebody explain it to me that why wc gives more chars suppose Ab.txt have two lines qwer qasd then wc -c ab.txt will give 10.why not 8.okay may be it is taking count one for each line just in case but why echo "qwer"|wc -C gives 5. Ok with \c it is returning 4. :) (6 Replies)
Discussion started by: Dhruva
6 Replies

10. UNIX for Dummies Questions & Answers

ftp confusion

I'm an intern at a company that recently bought out another business. In doing so, they inherited a unix system that contains files which they need to retrieve. No one in the company, including myself, really understands or knows unix so please respond with the true assumption that I'm a unix... (1 Reply)
Discussion started by: intern
1 Replies
Login or Register to Ask a Question