Knowee
Questions
Features
Study Tools

What is the output of the following code?char str[] = "hello";int len = strlen(str);printf("%d\n", len);

Question

What is the output of the following code?

char str[] = "hello";
int len = strlen(str);
printf("%d\n", len);
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be 5.

Here's the step by step explanation:

  1. The char str[] = "hello"; line declares a character array str and initializes it with the string "hello".

  2. The int len = strlen(str); line declares an integer len and initializes it with the length of the string str. The strlen function returns the length of a string (not including the null character at the end), so in this case it will return 5 because "hello" has 5 characters.

  3. The printf("%d\n", len); line prints the value of len, which is 5, followed by a newline character. So the output of the code will be 5.

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 is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}

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.