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;
}
Kagpur, Kanpur
Nagpur, Kanpur
Kagpur, anpur
Error
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 3 of 3.

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.

Darkrai said:   1 decade ago
Plz give some other example !

Nagarjun said:   1 decade ago
Please give the explanation cleary.

Why it produce anpur?

Suci said:   1 decade ago
Still not understanding. Give some other example.


Post your comments here:

Your comments will be displayed after verification.