C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 14)
14.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
Discussion:
43 comments Page 2 of 5.
Shubham said:
1 decade ago
At the starting str will points to the first character of the string which is %.
After executing two times increment operator on str pointer will skip the first two characters of the string which is %d and points to the \.
Inside the printf statement, str-2 make str point to the starting of the string .
So, the whole string will appear in the printf statement.
printf("%d\n",300).
Therefore, the output will be 300.
After executing two times increment operator on str pointer will skip the first two characters of the string which is %d and points to the \.
Inside the printf statement, str-2 make str point to the starting of the string .
So, the whole string will appear in the printf statement.
printf("%d\n",300).
Therefore, the output will be 300.
Fanzy said:
1 decade ago
Pointers are always incremented by 2 byte. Then how it is possible? can anyone explain this please?
Amol said:
1 decade ago
Nice explanation.
Yashwanth P said:
1 decade ago
@Nejat.
It's just an assumption. The memory location is allocated randomly by the CPU. It can be any number.
It's just an assumption. The memory location is allocated randomly by the CPU. It can be any number.
Nejat said:
1 decade ago
How do we know that the string is first initialized to 300?
Pankaj tilara said:
1 decade ago
Thank you to all.
Krishna mohan said:
1 decade ago
Here str is char type & how we can assign it to integer type i.e: %d without any conversion.
Vijay said:
1 decade ago
Step->1 char*str;
Here str is the char pointer.
Str has own address. Assume i.e. 500.
Step->2 str = "%d\n";
That mean str has at 500 = "%d\n".
Step->3 str++;
Address incremented by 1 because str is char pointer i.e new address will be 501(500+1).
Step->4 str++; Similarly again increment by 1.
Address will be 502 (501+1).
Step->5 printf(str-2, 300);
Here 1st will be evaluate str-2 then print.
Str-2 means decrements two times. Pointer will be at starting location, i.e 500.
Now printf will be like this printf ("%d\n", 300). Then finally print 300.
Step->6 return 0; means program execute successfully.
Here str is the char pointer.
Str has own address. Assume i.e. 500.
Step->2 str = "%d\n";
That mean str has at 500 = "%d\n".
Step->3 str++;
Address incremented by 1 because str is char pointer i.e new address will be 501(500+1).
Step->4 str++; Similarly again increment by 1.
Address will be 502 (501+1).
Step->5 printf(str-2, 300);
Here 1st will be evaluate str-2 then print.
Str-2 means decrements two times. Pointer will be at starting location, i.e 500.
Now printf will be like this printf ("%d\n", 300). Then finally print 300.
Step->6 return 0; means program execute successfully.
Ashlesha said:
1 decade ago
Are you want to say in last step that if str-1 it will pt to 102 and again to 100. That means here compiler will take str-2 is for twice time instead of 2 bytes. , right?
Tgrlaltn said:
1 decade ago
char*str; --> a pointer char points to anywhere, Ex. 100.
str = "%d\n"; --> now points to "%d\n" address of char array. Here there is a address of first element "%d\n".
Let's say that a char length is 2 byte.
So :
% --> address --> 100.
D --> address --> 102.
\ --> address --> 104.
N --> address --> 106.
str++; --> for be increased length of char (2 byte) up now our char array starts 102.
str++; --> for be increased length of char (2 byte) up now our char array starts 104.
printf (str-2, 300) ; for now str is decreased 2 times (2 byte + 2 byte) and it was 100 again. So it points to our char array again.
Return 0;
str = "%d\n"; --> now points to "%d\n" address of char array. Here there is a address of first element "%d\n".
Let's say that a char length is 2 byte.
So :
% --> address --> 100.
D --> address --> 102.
\ --> address --> 104.
N --> address --> 106.
str++; --> for be increased length of char (2 byte) up now our char array starts 102.
str++; --> for be increased length of char (2 byte) up now our char array starts 104.
printf (str-2, 300) ; for now str is decreased 2 times (2 byte + 2 byte) and it was 100 again. So it points to our char array again.
Return 0;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers