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

# G.S. said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
What is mean by lvalue required error?

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

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?

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.

Sushma said:   10 years ago
Please not understanding. Can you please explain in detail?

Pranali said:   8 years ago
An, Array is internally considered as Pointer. And the name of an array is internally a base address.


Post your comments here:

Your comments will be displayed after verification.