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;
}
No output
30
3
300
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
43 comments Page 1 of 5.

Sandesh H said:   7 years ago
@All.

This may help you.

*str ultimately is a variable like any other variable that store value, since it has * it becomes pointer variable which we used to store the address of other variables to store the address of another variable say m, we "&" symbol i.e str=&m;
but here str is declared as char and str="%d\n" means in variable str %d\n is stored instead of the address.

Since str is also a variable it will also have an address.
Let's assume str is at an address 200 ( let's assume char takes 1 bytes memory since it is char it will take 1 byte only as I think).

str++ means here address increment since str is pointer variable i.e 200+1=201.
again str++ means 201+1=202.( now str is 202)

The printf(str, 300); is same as printf("%d\n",300); as %d\n is stored in str.
but here str is now 202 as it is incremented twice, therefore, they have written str-2 in printf statement.
i.e 202-2=200 i.e str which as "%d\n".
(8)

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;

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.

Shubham said:   9 years 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.

Srk said:   1 decade ago
Consider str is pointing to 1002.

If we increment it by one 1002+sizeof(char)*1 (size of chat is 2 bytes so it will Point to next location (1004) that is explicitly casted to store char,

( str = (char *)(str+1*sizeof(char)) )

To 1004 contains a garbage value and similarly 1006.

but in printf statement we will make the pointer to point 1002.

Coderx said:   8 years ago
Here, str is a pointer variable. It points to the address of the first position. Next the str variable is incremented twice. That means the str variable now points at position 2 instead of position 0. But when we perform str-2, the str variable goes back to the position 0. At position 0 i.e. str= %d, so 300 is printed.
(1)

Sohan lal mits said:   1 decade ago
We know that when we increament pointer wihh n values like then pointer point to nth value address and whereas pointer decrease with nth value so pointer locate same address as previus so according question
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
output:300

PAVIforlove said:   1 decade ago
Assume ch address is 100.

The ch pointer variable store the %d.

In memory location 100.

After increment it would be 101 then 102.

While printing ch-2 means 102-2 then it again pointing 100th location in 100 location it contains "%d" then it will print 300.

Archana said:   2 decades ago
Twice str is incremented n then when we do str-2, it is again pointing to starting position. So str contains "%d\n".

So printf statement would be like printf("%d\n",300);

Hence output will be 300.

Vallabh said:   1 decade ago
Because it will take it as :

printf("%d",300); and print 300.

But in case like

char *str;
str="abc";
printf(str,"mno\n");

The output will be "abc".


Post your comments here:

Your comments will be displayed after verification.