C Programming - Strings - Discussion

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;
}

[A]. Kagpur, Kanpur
[B]. Nagpur, Kanpur
[C]. Kagpur, anpur
[D]. Error

Answer: Option D

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"


Suci said: (Jan 29, 2011)  
Still not understanding. Give some other example.

Nagarjun said: (Mar 10, 2011)  
Please give the explanation cleary.

Why it produce anpur?

Darkrai said: (Jun 15, 2011)  
Plz give some other example !

Naveen said: (Jul 7, 2011)  
Base address of an arry string can not be changed.it is constant pointer pointing to a non constant string.

Kiran said: (Jul 26, 2011)  
It is not understanding. Please explain it in other ways.

Raj said: (Sep 4, 2011)  
Please give some other example !

Honey said: (Sep 22, 2011)  
Am not understanding, please explain it in easy way.

Ravi said: (Nov 3, 2011)  
I don not understand, please give me another example.

Nsk said: (Jan 15, 2012)  
initially str[]=Nanpur...but when strcpy(str, "Kanpur"),the content of str[]=Kanpur will overwrite Nagpur.Since str represent base address str+1 will point to second character a & print the character starting from a to r i.e anpur

Sarita said: (Dec 24, 2012)  
Str="kanpur" str is consider as base address and we can not change value of base address hence error.

# G.S. said: (Jan 16, 2013)  
To copy a string we cant do it directly . For this we having the inbuilt function strcpy(,).

So here , If we want to put the "Kanpur" to str, we have to do like this strcpy(str, "Kanpur");

Otherwise its a ERROR.

Dheeraj Pandey said: (Aug 21, 2013)  
If we use strcpy(str,"kanpur")like this... then what will the output.

#include<stdio.h>

int main()
{
char str[] = "Nagpur";
str[0]='K';
printf("%s, ", str);
str = strcpy(str,"Kanpur");
printf("%s", str+1);
return 0;
}

Akhilesh said: (Sep 2, 2013)  
char str[] = "Nagpur";

str = "Kanpur";

Here str is a constant pointer and it is pointing to a string "Nagpur" (that is an array of char type).

In the second line we are trying to override the array which is not possible. we can assign it character by character. There is a inbuilt function strcpy(str,"Kanpur") that can be used for direct copying(overriding).

Krti said: (Oct 14, 2013)  
What is mean by lvalue required error?

Pratyay said: (Jan 18, 2014)  
lvalue means location value.
This question has two parts.

<1> The Erroneous One:

For example: int a = 7;

Here, a = lvalue because it is modifiable. We can update "a" according to our wish.

And anywhere a = 8; is acceptable.

Now suppose we write: 7 = 8;

Can that be done?
Never! Because 7 is not a variable/lvalue.

Lets come back to the question. Here, "str" means a pointer pointing to the base of the array "str".

So, It's a constant. How can you assign a string to a constant? So, we are getting an error.


<2> The Correct One:

After using strcpy, we actually copied everything of the second string to the first one. It's just an analogy of
x = y; (where x and y are two integers).

Here is the working process of the correct code.

>> First, we had "Nagpur".

>> Then, we just changed str[0] or "N" with "K".

>> It leaves "Kagpur".

>> We print that.

>> Next, we update "Kagpur" to "Kanpur".

>> Now we have to understand how strings get printed.

printf("%s, ", str) --> This signifies that everything from address str is printed until '\0' is encountered.

>> So printf("%s ", str+1) just increments the pointer to point to the next character i.e "a" and prints "anpur".

Hope I can make it clear..

Sneha said: (Nov 22, 2014)  
But I have an doubt that how str becomes an pointer? Its just an character array.

How can you do like str+1? Only we can do like str[0+1]. Anyone clear my doubt.

Sneha said: (Nov 22, 2014)  
But I have an doubt that how str becomes an pointer. Its just an character array.

How can you do like str+1? Only we can do like str[0+1]. Anyone clear my doubt?

Anamika said: (Dec 25, 2014)  
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.

Sushma said: (Feb 20, 2016)  
Please not understanding. Can you please explain in detail?

Pranali said: (Apr 1, 2017)  
An, Array is internally considered as Pointer. And the name of an array is internally a base address.

Vijayakumar said: (Jul 25, 2017)  
No, it won't produces the error, the string can be assigned directly using = during the initialization. So the answer will be C).

Murphy said: (Jul 29, 2017)  
Guys, the answer D is correct but the official explanation is horribly wrong!

This line is problematic:

"str[0]='K';"
The reason is, a literal string like "Nagpur" is stored in static memory area, READ ONLY. You can not change the value.

There is no problem to assign to str a different literal string because it is a char pointer, that is what a point is born to do.

Try run the code yourself, comment all lines after "str[0]='K';" and see what you find out.

Utkarsh said: (May 31, 2019)  
"String" is not a data type in C (unlike python, Java etc.) so you cannot simply assign:

char name[20];
name = "market"; // wrong
name[0] = "cricket"; // wrong again

"cricket" is called a string literal whereas name is a char array, i.e {'c','r','i','c','k','e','t','\0'}. They are not automatically converted to each other - you have to write your own function to copy char-by-char or use the library function strcpy().

Then how is;
char name[20] = "This is me";
valid code? Well, this is just a C short-cut for writing
char name[20] = {'T','h','i','s',' ','i','s',' ','m','e','u','\0'};

It does not work anywhere else.

Please, anyone, explain clearly.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.