Knowee
Questions
Features
Study Tools

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]);
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be 'e'.

Here's the step by step explanation:

  1. The code declares a character array str[] and initializes it with the string "hello".

  2. 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'.

  3. 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.

  4. 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.

This problem has been solved

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;}

1/3

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.