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.
Siya said:
9 years ago
Can we declare print statement like printf(str-2, 300);?
MY question is where is "%c" or " "%s"in printf statement. If it is true then how it works?
MY question is where is "%c" or " "%s"in printf statement. If it is true then how it works?
(1)
Amol said:
9 years ago
Nice explanation.
Yashwanth P said:
10 years 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:
10 years ago
How do we know that the string is first initialized to 300?
Pankaj tilara said:
10 years 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;
Satakshi said:
1 decade ago
Please some explain it step by step?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers