Tips for C Programmers
What is Output of following c snippet?
int main(){
char *s="Abhas";
printf("%s",s+ 2);
getch();
}
int main(){
char *s="Abhas";
printf("%s",s+ 2);
getch();
}
Replies
-
kingdavidhas
Explanation: In the above program role of %s is to display the string whose address is passed as an argument. This is how a standard printf statement works in c language. Now since we have passed s + 2 as an argument therefore first value of this expression is evaluated. Here ‘s’ would refer to address of first character in string ‘s’. Now printf would get address of third character (address of first character + 2) as argument so it will display the string starting from third position. Hence output would be ‘has’. -
Vishal Sharma
Ummm... don't you think there must be a * in front of s+2 to get the output "has" ? What i actually mean is, to get the output "has", it must be *(s+2)kingdavidhas
Explanation: In the above program role of %s is to display the string whose address is passed as an argument. This is how a standard printf statement works in c language. Now since we have passed s + 2 as an argument therefore first value of this expression is evaluated. Here ‘s’ would refer to address of first character in string ‘s’. Now printf would get address of third character (address of first character + 2) as argument so it will display the string starting from third position. Hence output would be ‘has’. -
Neeraj Sharma
*(s+2) will give you s[2] i.e. h..Vishal0203Ummm... don't you think there must be a * in front of s+2 to get the output "has" ? What i actually mean is, to get the output "has", it must be *(s+2)
has is correct
You are reading an archived discussion.
Related Posts
During our childhood we used to play with remote cars..can u say me how it operated?
Hello, I am Satty from Vishakhapatnam. Can someone tell me about the dielectrics in capacitors? What is their role? How do they function?
I want to join the M.tech course, but I don't have information whether I can get admission now. Please suggest me some colleges in Delhi. How it would be to...
I (and the chairman of the group) resisted getting a cell phone for very long. Even now, I have the most basic of phones which does only the normal phone...
can we increase the RPM of the motor ...... ?????? or the RPM is fixed ???? for induction motors