C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 7)
7.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str;
    str = "%s";
    printf(str, "K\n");
    return 0;
}
Error
No output
K
%s
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
57 comments Page 6 of 6.

Abdullah said:   9 years ago
I think that "%s", "%d", . Etc are pointers to string, integer, etc respectively themselves. That is why we use them in printf as (pointer to the variable of the type of its content, then the variable name).

So when we write str="%s" we are assigning a pointer to another pointer.
(6)

Satyam said:   9 years ago
Thanks to everyone. Really useful discussion.

Zdd said:   7 years ago
Thank you @Hann.

Sudheer said:   7 years ago
Thank you @Sandeep.

Shradha said:   3 years ago
Your explanation is good for understanding. Thank you @Megha.
(2)

Bhavesh said:   1 year ago
str has "%s" stored in it so the printf will work like,
printf(Str,"K\n"); => printf("%s","K\n"); => will print K as it's string and \n is for newline.
(6)

Prakash said:   5 months ago
There is no format specifier in print statement. So it doesn't print the structure value it only prints the K.


Post your comments here:

Your comments will be displayed after verification.