Guess The Output Series 7

Explanation of guess the output questions posted on our Instagram Page.
Click here to view Guess The Output series 6.

1) strlen() vs sizeof()

Guess the output

Output of above C Program is D) 5 8 .

Why ­čĄö ?

Firstly you should know how strlen() function and sizeof() operator works.
Now let us understand how strlen() function works.
The strlen() function counts the characters till the terminating null character (excluding) i.e (‘\0’)
Now take a look at the given problem,

strlen() function searches for that NULL character and counts the number of memory address passed.

so , strlen() counts the chars upto str[4] .
i.e count=5

The working of sizeof() operator is pretty clear!
It returns actual amount of memory allocated for the operand passed to it.
What content present inside that block is none of sizeof() operator’s business ­čśÄ!

Hence, correct output is 5 8 !!

Click on Execute button to Run above code:

The above explanation is given by Akash Patil.

For more programming related questions and fun,
Follow us on Instagram.

Click here to view Guess The Output series 6.


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top