C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 23)
23.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[] = "Nagpur";
str[0]='K';
printf("%s, ", str);
str = "Kanpur";
printf("%s", str+1);
return 0;
}
Answer: Option
Explanation:
The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.
To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");
The program prints the string "anpur"
Discussion:
24 comments Page 2 of 3.
Anamika said:
1 decade ago
str holds the address of first character in an array. Its a pointer.
If you give str[0+1]->means str[1]-> it will prints "a" alone.
If you give str[0+1]->means str[1]-> it will prints "a" alone.
Vijayakumar said:
8 years ago
No, it won't produces the error, the string can be assigned directly using = during the initialization. So the answer will be C).
Naveen said:
1 decade ago
Base address of an arry string can not be changed.it is constant pointer pointing to a non constant string.
Sarita said:
1 decade ago
Str="kanpur" str is consider as base address and we can not change value of base address hence error.
Pranali said:
8 years ago
An, Array is internally considered as Pointer. And the name of an array is internally a base address.
Sushma said:
10 years ago
Please not understanding. Can you please explain in detail?
Nagarjun said:
1 decade ago
Please give the explanation cleary.
Why it produce anpur?
Why it produce anpur?
Kiran said:
1 decade ago
It is not understanding. Please explain it in other ways.
Ravi said:
1 decade ago
I don not understand, please give me another example.
Honey said:
1 decade ago
Am not understanding, please explain it in easy way.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers