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 arraystr
and initializes it with the string "hello". -
The
int len = strlen(str);
line declares an integerlen
and initializes it with the length of the stringstr
. Thestrlen
function returns the length of a string (not including the null character at the end), so in this case it will return5
because "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 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++;}
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.