1.What is the output of the following code?char str[] = "hello";printf("%c\n", str[1]);
Question
1.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
printffunction 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 does the following code print?#include <stdio.h>int main() { char str[] = "Hello, World!"; char *ptr = str + 7; printf("%s\n", ptr); return 0;}
What will be the output of the following code?#include <stdio.h>int main() { char str[] = "ABCDE"; printf("%c\n", str[2] + 3); return 0;}
What will be the output of the following code?#include <stdio.h>int main() { char str[] = "Hello"; str[0] = 'M'; printf("%s\n", str); return 0;}
What will be the output of the following C code? #include <stdio.h> #include <string.h> int main() { char *str = "hello, world"; char str1[9]; strncpy(str1, str, 9); printf("%s %d", str1, strlen(str1)); }
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.