What will be the output of the following C program?#include<stdio.h>int main(){ char str[] = "Nagpur"; str[0]='K'; printf("%s, ", str); str = "Kanpur"; printf("%s", str+1); return 0;}Select one:Nagpur, KanpurErrorKagpur, KanpurKagpur, anpur
Question
What will be the output of the following C program?#include<stdio.h>int main(){ char str[] = "Nagpur"; str[0]='K'; printf("%s, ", str); str = "Kanpur"; printf("%s", str+1); return 0;}Select one:Nagpur, KanpurErrorKagpur, KanpurKagpur, anpur
Solution
The output of the given C program will be an error.
Here's the step by step explanation:
-
The program starts with including the standard input-output library using
#include<stdio.h>. -
The
main()function is declared which is the entry point of the program. -
Inside the
main()function, a character arraystr[]is declared and initialized with the string "Nagpur". -
The first character of the string,
str[0], is then changed from 'N' to 'K', making the string "Kagpur". -
The program then prints this modified string using
printf("%s, ", str);, so "Kagpur, " is printed. -
The next line
str = "Kanpur";attempts to change the entire string from "Kagpur" to "Kanpur". However, in C, you cannot change the entire string stored in a character array like this after it has been declared. This line will cause a compilation error.
So, the correct answer is: Error.
Similar Questions
What is the output of the C Program?
What will be the output of the following program?
What will be the output of the C program? ans.09119011 Previous Marked for Review Next
What would be the output of the following program?#include <stdio.h>int main( ){int j=1;while (j <= 255){printf ( "%c %d\n ", j, j );j++;}return 0;}ans.
What will be the output of the following program?Note: This question helps in clearing AMCAT exam.12345678910#include <stdio.h>int main() { int c = 0, e = 0; for (e = 0;e < 5; e++) { c++; if (e == 3) break; } printf("%d",c);}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.