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:
56 comments Page 1 of 6.

Bhavesh said:   10 months 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.
(3)

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

Sudheer said:   7 years ago
Thank you @Sandeep.

Zdd said:   7 years ago
Thank you @Hann.

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

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)

Siva said:   9 years ago
int i;
int main()
{
int i;
i=78;
void *vptr;
vptr = &i;
printf("%d",& *vptr);

\\as this gives garbage value because type casting should be done.(*(int*)vptr)); and in calling function we have given **q.we know that *q=&vptr and **q=value of vptr so vptr value is 0 because it is initialised with void data type.and size to store 0 we get the size to store 0 because any pointer by default gets int. So we get the size and 0 are stored.
(1)

Sachin said:   9 years ago
If we try to print only str it will show (null) as output and in the code, there is no need of str.
It is only taking printf(" ");
Whatever you write inside the quotes is only printing that's it.
(2)

Manjeet said:   9 years ago
So why this not possible when I replace char data type with int?
(1)

Abu zaid said:   9 years ago
%s is the format specifier type of string.

We can declare it anywhere in the local scope.
(2)


Post your comments here:

Your comments will be displayed after verification.