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 3 of 5.
Abi said:
1 decade ago
Hi everyone!
Please explain, how come char ptr gives an integer as result without typecasting?
Please explain, how come char ptr gives an integer as result without typecasting?
Fanzy said:
1 decade ago
Pointers are always incremented by 2 byte. Then how it is possible? can anyone explain this please?
Gunjan said:
1 decade ago
What will be the output char ch=300;?
As it is unsigned it exceeds the range, what is the answer and why please explain.
As it is unsigned it exceeds the range, what is the answer and why please explain.
Satakshi said:
1 decade ago
Please some explain it step by step?
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;
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?
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.
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.
Pankaj tilara said:
10 years ago
Thank you to all.
Nejat said:
10 years ago
How do we know that the string is first initialized to 300?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers