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:
58 comments Page 2 of 6.

Siva said:   1 decade 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)

Hann said:   2 decades ago
Its like
char str[]="good";
printf("%s",str);
its jst like replacing str with "good"
it becomes printf("%s","good");
output will be:
good
(1)

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

Mayur said:   1 decade ago
#include<stdio.h>

int main()
{
char *str;
str = "sv";
printf("%s",str,"k");//here str="sv" is printed...
return 0;
}
other prograam:
int main()
{
char *str;
str = "sv";
printf("%s","k");//here , present therfor first %s ignore due to not avaliable of variable stringis printed...
return 0;
}

Sunny said:   1 decade ago
@akshit
bcoz str is a pointer it stores first address of the string.

Armaan said:   1 decade ago
Kavyashree is just correct

Arvindh said:   1 decade ago
Kartik : I had the same doubt. However both work. You don't need the type as prefix in printf statement if you're directly printing something, but if you are printing a value stored in a variable it needs the type of the variable.

Omkar said:   1 decade ago
@Ravi.

Your code is working, its working because printf function needs one fixed argument and n variable arguments.

Kaushlenda singh parihar said:   1 decade ago
Hoping this example will help you understand the concept used in above question.

#include<stdio.h>
int main()
{char *str = "%d";
int i=10;
printf(str,i);
return 0;
}

Output:10.

Akshit said:   1 decade ago
How can Kavyashree be correct?

Str = Address of first character of string, isn't it?


Post your comments here:

Your comments will be displayed after verification.