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.

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)

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

int main()
{
char *str;
str = "hi %s hello";
printf(str,"hh");
return 0;
}

Here printf function take the address of str it is pointing to the "hi %s hello"

it's print hi hh hello
%s will take address of hh

printf("%s")
it will print null

printf("%s","hi");
it's print hi

It takes address of string. Hope you are understand.

Raghu said:   1 decade ago
The printf() is a function which receives a pointer to a format string.
printf(str, "K\n"); --> works because it passes the address of format string -> "%s"

It is as good as specifying
printf("%s", "K\n") which again passes pointer to string "%s".

So, there is not replacement of string is happening here ( as mentioned by Kavyashree) just a pointer which is passed across.

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;
}

Amir Khan said:   1 decade ago
Can anyone explain me this:

If,
int *str="abcd"
Then first character will be 'a' not '"'(Double quotes).

Then, if we initialize as,
str = "%s";
printf(str, "K\n");

Then printf requires "--"(double quotes).

And if we replace str value in printf function, then it will write as,
printf(%s,"K\n");

Got my point everybody? Please reply me.
(2)

Alim Ul Karim said:   1 decade ago
#include<stdio.h>

int main()
{
char *str;
str = "%s";
printf(str, "K\n");
printf("%s\n", str); // prints "%s"
printf("%c\n", str); // prints the address and converted it to char , which is '$'
printf("%c\n", *str); // prints '%'
return 0;
}

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)

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)

Ravi said:   1 decade ago
@Kavya and others:Your concept is correct but then also it somehow depends on the arguments of printf() function ,if its just replacing the string then how come the following does not work ?

int main()
{
char s[]="goog";
printf(s);
return 0;
}

MERIL said:   1 decade ago
@kavyashree

u r correct
if v replace d code like this
#include<stdio.h>

int main()
{
char *str;
int i=2
str = "%d";
printf(str,i);
return 0;
}

THE O/P WILL BE: 2
***So ur explanation is correct


Post your comments here:

Your comments will be displayed after verification.