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)

Shobika said:   8 years ago
How char pointer stores the "%d\n"?
(2)

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?
(1)

Swaroopa said:   9 years ago
Thanks for your answers and explanation.
(1)

Rani said:   9 years ago
How we can assign are str= %d/n?

Is strcpy not necessary here?
(1)

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)

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.

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.

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;


Post your comments here:

Your comments will be displayed after verification.