Why does this print 1, I thought it should print 0 since there is nothing in it and strlen dosn't count the \0 at the end of the string.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char a[50];
int b = strlen(a);
printf("\nlength:%d\n",b);
return(0);
}
Now here
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int b = strlen(a);
printf("\nlength:%d\n",b);
a[0]=fgetc(stdin);
printf("\n--%c\n",a[0]);
b = strlen(a);
printf("\nlength:%d\n",b);
a[0]=0;
printf("\n--%c\n",a[0]);
b = strlen(a);
printf("\nlength:%d\n",b);
}
but not looking at this part
Code:
int main()
{
char a[50];
int b = strlen(a);
printf("\nlength:%d\n",b);
I give the char a[0] a character, and then I say that the value of a[0] is [NULL].
And then it dosnt see anything there.
So for example if I had a string and use fgets and stdin to write in it, and then for every character of the char string I say it equals to 0, that is [NULL].
So then it would show me strlen 0, and it wold look as if the string is empty.
Why is that so btw?
Oh and another question my friend told me if I do this
char text[500] = {0};
That then basicly every char should be = 0;
Is that really how it works?