C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 21)
21.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
printf("%s\n", &str+2);
return 0;
}
Answer: Option
Explanation:
Step 1: char str[25] = "IndiaBIX"; The variable str is declared as an array of characteres and initialized with a string "IndiaBIX".
Step 2: printf("%s\n", &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string "IndiaBIX". Therefore &str+2 is another memory location.
Hence it prints the Garbage value.
Discussion:
10 comments Page 1 of 1.
JagP said:
4 years ago
Scale factor of this string is 9. So &str+2 will get you to str+18. If you want to access str[2] you should use str+2.
Balaji said:
9 years ago
But actually it takes address.
&str+2=(str)+(2*sizeof(str))
&str+2=(str)+(2*sizeof(str))
Balaji said:
9 years ago
Thank you very much @Deepak.
Vidushi said:
9 years ago
@Deepak.
When &str+2 is performed, then we do not reach to str[2] location, the location which is attained is (address of str) + 2 *(length of whole str string).
When &str+2 is performed, then we do not reach to str[2] location, the location which is attained is (address of str) + 2 *(length of whole str string).
Deepak sharma said:
9 years ago
Can anybody explain this more clearly?
Because I think & str will give base addr then &str +2 will take us two location ahead in the string. How its printing garbage value?
Because I think & str will give base addr then &str +2 will take us two location ahead in the string. How its printing garbage value?
Anonymous said:
9 years ago
@Akshay.
You are absolutely right my friend but what we are trying to print is address of str+2 which points to 'd', of course, but we are using %s for an operation which require %d and gets garbage value.
You are absolutely right my friend but what we are trying to print is address of str+2 which points to 'd', of course, but we are using %s for an operation which require %d and gets garbage value.
Akshay said:
10 years ago
I have a doubt that. str is a array and str will points to the beginning of the Indiabix.
i.e "I" it will then sequentially store the every other preceding alphabets. So if str will point to I. Then str+2 should have to point to "d".
i.e "I" it will then sequentially store the every other preceding alphabets. So if str will point to I. Then str+2 should have to point to "d".
Paras Vanika said:
1 decade ago
No Simple..!!
The meaning of &str + 2 is that suppose in memory "indiaBix"
Is stored at any location then &str try to access that location .
suppose it store at 5000 address then &str =5000.
&str+2=5002
Which is another address in a memory which does not have anything to print...that's why output is Garbage value.
The meaning of &str + 2 is that suppose in memory "indiaBix"
Is stored at any location then &str try to access that location .
suppose it store at 5000 address then &str =5000.
&str+2=5002
Which is another address in a memory which does not have anything to print...that's why output is Garbage value.
Jatin said:
1 decade ago
char str[25] = "IndiaBIX";
printf("%s\n", &str[2]);
print"diaBix"
printf("%s\n", &str[2]);
print"diaBix"
Dimple kamboj said:
1 decade ago
Can we add the memory with string as numeric as you did?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers