I have two programs. Both initialize an array from a string literal. In one case the array size is exactly the number of characters I want to place in the array.
I wonder why the output of the size returned by strlen()
differs for both prorgams. Is it because of the terminating null character is missing? If so, then why is the output 16?
#include<stdio.h>
#include<string.h>
main()
{
char str[5] = "ankit";
printf("size of = %d
",sizeof(str));
int len = strlen(str);
printf("length = %d
",len);
}
output :- size of = 5 , length = 16
#include<stdio.h>
#include<string.h>
main()
{
char str[] = "ankit";
printf("size of = %d
",sizeof(str));
int len = strlen(str);
printf("length = %d
",len);
}
output :- size of = 6 , length = 5
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…