+ 1

What output this code and why?

#include <stdio.h> int main() { int a = 10, *b; b = &a; printf ("%d", *b++); return 0; }

23rd Jan 2019, 7:45 PM
Mihai C
Mihai C - avatar
3 Answers
+ 4
may I say it again? it won't print 11. there is nothing stored in the memory when you increment the pointer. the pointer points to an address of the memory, and that memory has to be formatted as int. it points to a, when you increment b the value printed won't be 11, will be the value of the address written in decimal number if you write it in code playground, if you do it in a terminal it might show it to you in hexadecimal.
24th Jan 2019, 4:32 AM
notqueued
notqueued - avatar
+ 4
it will print 10 to the screen and then the pointer b, after being incremented, will point to an address of the memory that has nothing stored so if you try to print it again it will show you an address.
23rd Jan 2019, 8:39 PM
notqueued
notqueued - avatar
+ 1
Output is 10 Because b holds the address of variable a ..... and *b prints the value of address b . b++ is increment operator in which the value is printed and then after it will increase by 1 . So the answer is 10 not 11. If. Printf("%d", *++b ); \\ output is 11
23rd Jan 2019, 8:59 PM
Rakesh Singh
Rakesh Singh - avatar