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

Roshan zameer said:   2 decades ago
Str doesnot return.. so the value(k )inside brace isdisplayed..

Sandeep(MCA) said:   1 decade ago
It will print k because we don't add the %c with str. So it will print only massage which is in "".

Kavyashree said:   1 decade ago
Its because

The statement printf(str, "K\n"); is replaced with printf("%s" , "K\n");
// since str = "%s";

So it will print K.

Ranjan said:   1 decade ago
@Kavyashree
your assumption is wrong, as it will ask a variable to print the data type, by the way the code offered from you shows error.

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

Pranoy said:   1 decade ago
@KavyaShree@Hann

Dont worry Kavya your assumption is absolutely.

The code what You have written is true.

And, Hann it is not necassary to ask a variable to print the data type, since it takes as null if not assigned

Harsh said:   1 decade ago
@Kavyashree.

You are absolutely correct.

Lavanya said:   1 decade ago
@Kavyashree.

Your ans is exactly correct.

Nirlepsingh said:   1 decade ago
how str="%s" is true
because str is a pointer which store adress of characters

Rahul said:   1 decade ago
str will store address of variable if and only if variable is preceded by &
otherwise it will store value of variable.


Post your comments here:

Your comments will be displayed after verification.