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 1 of 3.

Mayur said:   4 years ago
#include<stdio.h>

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

This Gives O/P - Kagpur, agpur.

So We cannot just assign a value directly to the base address, We have to dereference to do that.

Utkarsh said:   6 years ago
"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.

Murphy said:   8 years ago
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.

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).

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:   9 years ago
Please not understanding. Can you please explain in detail?

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.

Sneha said:   1 decade ago
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:   1 decade ago
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.

Pratyay said:   1 decade ago
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..
(1)


Post your comments here:

Your comments will be displayed after verification.