edited by
3,844 views
6 votes
6 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$
edited by

5 Answers

5 votes
5 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 votes
1 votes

 

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

2 votes
2 votes
3 answers
2
Satbir asked Jan 13, 2020
1,825 views
Consider the following recursive C function that takes two argumentsunsigned int rer(unsigned int n, unsigned int r){ if(n>0)return(n%r + rer(n/r,r)); else retturn 0; }Wh...
6 votes
6 votes
1 answer
3