Character Array MCQ 4 : String Length and Pointer
Character Array MCQ 4 : String Length and Pointer
When I was Selected for Technical Interview of Accenture then Technical Person Asked me -
Predict the Output of the Following Program ?
#include<stdio.h> int main() { char str[]="c4learn"; printf("%d",*(str+strlen(str))); return(0); }
Options :
- 0
- NULL Character
- Garbage
- None Of these
Output:
Switch to String MCQ Home : Click Here
How and Why ?
Consider -
*( str + strlen(str)) = *( str + 7) = *( Base Address of str + 7)
- Base Address Means Address of str[0].
- str[0] + 7 means ‘NULL Character‘
- Length of Array is 7 -
str[0] = 'c' str[1] = '4' str[2] = 'l' str[3] = 'e' str[4] = 'a' str[5] = 'r' str[6] = 'n' str[7] = '\0'
So
*(str + strlen(str)) Returns NULL Character '\0'
Look at Printf Statement Once Again -
- %d is Written inside Printf
- So ASCII Value of ‘\0’ will be printed.
- ASCII Value of ‘\0’ is 0.