I want to know why this code is giving 15 as output as according to me 14 should be answer?

#include<stdio.h>
int main()
{
int i;
i=6;
printf("%d",++i+i++);
return 0;
}

1 Like

it came in exam today …lol. i just want to know is there any property of printf which is generating 15 as output

As far as I can understand, it is going like this- first i=6, when we do ++i, i becomes 7. Now the value of i changes to 7. And we are doing i++, so value of i now becomes 8. So 7+8=15

1 Like

its i++ post increment so value of i should remain 7 according to me
. the value should change after executing printf.

You are mistaken here. Consider this
ll i=1;
ll x=i++;
cout<<i<<endl;
cout<<x<<endl;
ll y=++i;
cout<<i<<endl;
cout<<y<<endl;

Output-
2
1
3
3
The value of i after all operations is 3 and not 2. It is the value which is storing i is different, that is value of x and y.

1 Like

what i am trying to say that
int i;
i=6;
printf("%d",++i+i++);

here first ‘++i’ is executed and value of ‘i’ is changed to 7 after that ‘i++’ executed so now its value should be 7 and it will be incremented to 8 after executing printf.
ll i=1;
ll x=i++;
cout<<i<<endl;
cout<<x<<endl;
here in this code we can see that value of x is 1 not 2 . first statement is executed and then increment .

1 Like

…cracking knuckles…

Let’s fire up our trusty GNU Compiler Collection and dive into some x86 assembly(AT&T)!

NOTE:
The following demo is done by storing your code in a file called prog.c because that’s literally the most creative name I could come up with at this moment.

Step 1 (creating assembly file)

Let’s create an assembly file of prog.c.

gcc prog.c -S

Step 2 (validation)

Let’s make sure gcc isn’t mucking around.

ls
prog.c prog.s

Step 3 (being amazed)

Look! A file ending with a .s extension! It’s an assembly file.

Step 4 (opening it)

I feel privileged because I have the Sublime 3 text editor and you don’t. I feel sorry for you. Go download it from this very suspicious link.

…assuming that you’re back…

subl prog.s

Step 5 (look at that ass.)

		.file	"prog.c"
		.text
		.def	___main;	.scl	2;	.type	32;	.endef
		.section .rdata,"dr"
	LC0:
		.ascii "%d\0"
		.text
		.globl	_main
		.def	_main;	.scl	2;	.type	32;	.endef
	_main:
	LFB12:
		.cfi_startproc
		pushl	%ebp
		.cfi_def_cfa_offset 8
		.cfi_offset 5, -8
		movl	%esp, %ebp
		.cfi_def_cfa_register 5
		andl	$-16, %esp
		subl	$32, %esp
		call	___main
		movl	$6, 28(%esp)
		addl	$1, 28(%esp)
		movl	28(%esp), %eax
		leal	1(%eax), %edx
		movl	%edx, 28(%esp)
		movl	28(%esp), %edx
		addl	%edx, %eax
		movl	%eax, 4(%esp)
		movl	$LC0, (%esp)
		call	_printf
		movl	$0, %eax
		leave
		.cfi_restore 5
		.cfi_def_cfa 4, 4
		ret
		.cfi_endproc
	LFE12:
		.ident	"GCC: (GNU) 7.4.0"
		.def	_printf;	.scl	2;	.type	32;	.endef

You Know Nothing, Jon Snow!

Step 6 (did you know that I skipped step 5???)

You went up to check XD. Let’s take a look at the part of the assembly (only lines 21 - 30) that actually caters to what you’ve asked for. Believe me, it isn’t very scary. Note that, ;s are used instead of //s for comments in this assembly. I’ve explained every line below in the form of comments. Do go through it and if you have any doubt, feel free to ask.

	movl	$6, 28(%esp)	;	assigns `6` to `i`(28 + `esp`(stack pointer register))			;	i ← 6
	addl	$1, 28(%esp)	;	adds `1` to `i`													;	i ← 7
	movl	28(%esp), %eax	;	stores `i` in `eax`(accumulator register)						;	eax ← 7, i ← 7
	leal	1(%eax), %edx	;	stores `eax` + `1` to `edx`(data register)						;	edx ← 8, eax ← 7, i ← 7
	movl	%edx, 28(%esp)	;	stores `edx` content at `i`										;	i ← 8, edx ← 8, eax ← 7
	movl	28(%esp), %edx	;	loads `i` into `edx`											;	edx ← 8, i ← 8, eax ← 7
	addl	%edx, %eax		;	adds content of `edx` to `eax`									;	eax ← 15, edx ← 8, i ← 8
	movl	%eax, 4(%esp)	;	stores content at `eax` at `esp` + `4` as argument for _printf 	;	esp + 4 ← 15, eax ← 15, edx ← 8, i ← 8
	movl	$LC0, (%esp)	;	adds format string "%d\0" onto stack as argument for _printf	;	esp ← "%d\0", esp + 4 ← 15, eax ← 15, edx ← 8, i ← 8
	call	_printf			;	calls _printf to write to stdout								;	stdout ← "15", esp ← "%d\0", esp + 4 ← 15, eax ← 15, edx ← 8, i ← 8

Step 7 (Conclusion)

That’s why you’re getting 15 instead of 14 as output. @ssjgz is absolutely correct. This is yet another example of undefined behaviour. This program produces unpredictable results. Never write such horrendous code. If you’d like to know more about x86 assembly, I’m gonna share the most valuable link you’ll ever find.

You’re welcome. :slightly_smiling_face:

3 Likes

i got the answer but assembly laguage is much hard to understand .most of it had gone over my head . i am really not interested in writting codes like this . i was excited to know about the result as it came in my exam .thanks a lot buddy.