in Programming edited by
1,792 views
5 votes
5 votes

What is the output of the code given below?

# include<stdio.h>
int main()
{
    char name[]="satellites";
    int len;
    int size;
    len= strlen(name);
    size = sizeof(name);
    printf("%d",len*size);
    return 0;
}
  1. $100$
  2. $110$
  3. $40$
  4. $44$
in Programming edited by
by
1.8k views

4 Answers

4 votes
4 votes

Answer: b) 110.

The above char array name can be written as below:

char[] name = {'s','a','t','e','l','l','i','t','e','s', '\0' };

 

strlen gives no of characters in a string without the null in the end = 10.

sizeof  gives the no of characters(no of bytes specifically) in a string including the null at the end = 11 

So 11*10 = 110.

edited by
1 vote
1 vote

 

char[] name = {'s','a','t','e','l','l','i','t','e','s', '\0' };

sizeof=11 (as \0 included and number of bytes)

strlen = 10 (as \0 not included)

len * size = 10*11 = 110

0 votes
0 votes
char[] name = {'s','a','t','e','l','l','i','t','e','s', '\0' };

here size of will give the total size of the array=11

strlen = 10 (as \0 not included)

10*11 = 110
0 votes
0 votes
strlen() => Length of string excluding null character.
sizeof() => No of bytes allocated including those assigned to null character.
so len = 10 and size =11
Output is 11.
Answer:

Related questions