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:
57 comments Page 3 of 6.
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.
#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.
Your code is working, its working because printf function needs one fixed argument and n variable arguments.
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)
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;
}
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.
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?
Str = Address of first character of string, isn't it?
Armaan said:
1 decade ago
Kavyashree is just correct
Ravindra bagale said:
1 decade ago
@Everyone.
Very simple logic. Just replaces the string, nothing is else. Just assigns "%s" to str. If you uses %d then it will gives some integer result, if %c then some character. Don't take tension, just chill guys.
Very simple logic. Just replaces the string, nothing is else. Just assigns "%s" to str. If you uses %d then it will gives some integer result, if %c then some character. Don't take tension, just chill guys.
Rishi said:
1 decade ago
@Ravi.
Your code is working properly. O/P will be goog.
Your code is working properly. O/P will be goog.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers