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;
}
Discussion:
58 comments Page 1 of 6.
Bhavesh said:
2 years 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.
printf(Str,"K\n"); => printf("%s","K\n"); => will print K as it's string and \n is for newline.
(7)
Abdullah said:
10 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.
So when we write str="%s" we are assigning a pointer to another pointer.
(6)
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.
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)
Mohit said:
1 decade ago
Change %s with %c, %x. You will see that str is replace by different output.
(2)
Shradha said:
4 years ago
Your explanation is good for understanding. Thank you @Megha.
(2)
Abu zaid said:
1 decade ago
%s is the format specifier type of string.
We can declare it anywhere in the local scope.
We can declare it anywhere in the local scope.
(2)
Sachin said:
1 decade 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.
It is only taking printf(" ");
Whatever you write inside the quotes is only printing that's it.
(2)
Pranay Pradhan said:
6 months ago
@all.
We get an equivalent output from the following:
#include<stdio.h>
int main()
{
char str[3] = "%s";
printf(str, "K\n");
return 0;
}
We get an equivalent output from the following:
#include<stdio.h>
int main()
{
char str[3] = "%s";
printf(str, "K\n");
return 0;
}
(1)
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?
{
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)
Manjeet said:
1 decade ago
So why this not possible when I replace char data type with int?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers