1.What is the output of the following code?char str[] = "hello";printf("%c\n", str[1]);
Question
What is the output of the following code?
char str[] = "hello";
printf("%c\n", str[1]);
Solution
The output of the code will be 'e'.
Here's the step by step explanation:
-
The code declares a character array
str[]
and initializes it with the string "hello". -
In C and C++, strings are actually one-dimensional array of characters terminated by a null character '\0'. So the string "hello" is stored in the array like this: 'h', 'e', 'l', 'l', 'o', '\0'.
-
Array indexing in C and C++ starts from 0. So,
str[0]
is 'h',str[1]
is 'e',str[2]
is 'l', and so on. -
The
printf
function is used to print the character at index 1 of the array, which is 'e'. So, 'e' will be the output of the code.
Similar Questions
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
What is the output of the following C code?char *ptr;char mystring[] = "abcdefg";ptr = myString;ptr += 5;
Find output of the below program:int main(){ char str[]={'e','a','r','t','h'}; printf("%s", str); return 0;}
What is the output of the following code?int arr[5] = {1, 2, 3};printf("%d\n", arr[4]);
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
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.