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 2 of 6.
Rajesh Panse said:
1 decade ago
@kavyashree.
You are absolutely right.
You are absolutely right.
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
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
Arpan V Mohokar said:
1 decade ago
Kavyashree is correct
Prateek said:
1 decade ago
Kavyashree is correct
Taniya said:
1 decade ago
Thanx kavyashree.
Suchita said:
1 decade ago
Can we assign int type value to char type pointer? Please give example.
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;
}
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;
}
Nagraj said:
1 decade ago
@Alim
printf("%c\n", str);
in this statement why it prints "$"
printf("%c\n", str);
in this statement why it prints "$"
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.
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.
Rajat said:
1 decade ago
It shows error in turbo c compiler. "declaration syntax error".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers