Character arraysWhat is the output of this C code?#include<stdio.h>void main() { char str[] = "Code\0Tantra\0India"; printf("%s\n", str);}OptionsCode\0Tantra\0IndiaCodeCompilation errorCode Tantra India
Question
Character arraysWhat is the output of this C code?#include<stdio.h>void main() { char str[] = "Code\0Tantra\0India"; printf("%s\n", str);}OptionsCode\0Tantra\0IndiaCodeCompilation errorCode Tantra India
Solution
The output of the given C code will be "Code".
Here's the step by step explanation:
-
The given C code declares a character array
str[]and initializes it with the string "Code\0Tantra\0India". -
In C, strings are actually one-dimensional array of characters terminated by a null character '\0'. So the '\0' character is used to mark the end of a string.
-
When the
printffunction is called with "%s\n" as the format specifier andstras the argument, it starts printing the characters from the beginning of thestrarray and stops when it encounters the first '\0' character. -
Therefore, only "Code" is printed and the rest of the string after the first '\0' character is ignored.
So, the correct option is "Code".
Similar Questions
Character arraysWhat is the output of this C code?#include<stdio.h>void main() { char str[] = "Code\0Tantra\0India"; printf("%s\n", str);}OptionsCode\0Tantra\0IndiaCodeCompilation errorCode Tantra India
1.What is the output of the following code?char str[] = "hello";printf("%c\n", str[1]);
What is the output of the following C code?char *ptr;char mystring[] = "abcdefg";ptr = myString;ptr += 5;
What will be the output of the following C code?
What will be the output of the following C code?const char str1[]="ABCDEF1234567";const char str2[] = "269";len = strcspn(str1, str2);printf("First matching character is at %d\n", len + 1);
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.