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);
Solution
The output of the code will be 5.
Here's the step by step explanation:
-
The
char str[] = "hello";line declares a character arraystrand initializes it with the string "hello". -
The
int len = strlen(str);line declares an integerlenand initializes it with the length of the stringstr. Thestrlenfunction returns the length of a string (not including the null character at the end), so in this case it will return5because "hello" has 5 characters. -
The
printf("%d\n", len);line prints the value oflen, which is5, followed by a newline character. So the output of the code will be5.
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;}
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.