Machine-translated by Google. Questions are authored in English.
Top

Discussion

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: D

Correct answer: Error

The first part is fine: str is a modifiable array, so str[0] = 'K' works. The failure is str = "Kanpur"; - an array name is not a modifiable lvalue, so it cannot be reassigned. Copying into it would need strcpy(str, "Kanpur"), and even then only if there were room.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.