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 2 of 6.

Megha said:   1 decade ago
int main()
{
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}

Here printf(str,"K\n");
str prints the base address?
str would he having the address of string "%s"
And when we just say str its address is supposed to be printed?
(1)

Assasin said:   1 decade ago
#include<stdio.h>
#include<conio.h>
int main()
{
printf("hi","bye");
}

please explain this output: hi.

Mitali said:   10 years ago
@Shahin.

How it will give garbage value as an output?

Singh is king said:   1 decade ago
Based on the total var we need to specify scope identifiers.

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.

Omkar said:   1 decade ago
@Ravi.

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

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.

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.

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.