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 str[] = "world";printf("%s\n", str + 2);

What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);

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

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

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.