When you declare char temp[100]
without initialising it to anything, it just takes uninitialised memory. This memory can be anything. For example, the following program will write out the initial contents of that, as integers:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char temp[100];
int i;
for(i = 0; i < 100 ; i++)
{
fprintf(stdout, "%d ", temp[i]);
}
return 0;
}
This prints consistently different output for me, though by some fluke it keeps printing sections of zeros. e.g.:
88 -70 43 81 -1 127 0 0 88 -70 43 81 -1 127 0 0 1 0 0 0 0 0 0 0 112 -70 43 81 -1 127 0 0 0 64 -108 14 1 0 0 0 72 50 -13 110 -1 127 0 0 -128 -70 43 81 -1 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96 -70 43 81
88 90 72 88 -1 127 0 0 88 90 72 88 -1 127 0 0 1 0 0 0 0 0 0 0 112 90 72 88 -1 127 0 0 0 -96 119 7 1 0 0 0 72 18 72 105 -1 127 0 0 -128 90 72 88 -1 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96 90 72 88
88 -6 -79 87 -1 127 0 0 88 -6 -79 87 -1 127 0 0 1 0 0 0 0 0 0 0 112 -6 -79 87 -1 127 0 0 0 0 14 8 1 0 0 0 72 34 57 104 -1 127 0 0 -128 -6 -79 87 -1 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96 -6 -79 87
What's most likely happening is that your non-null-terminated string is being accidentally null-terminated by virtue of the fact that temp[strlen(str)]
is, by a fluke, \0
.
NULL
is a macro with a null pointer constant. This is irrelevant here. You mean the ASCIINUL
ornul
character with the integer value0
. – Impellerstrcpy
. – Ogawastrcpy
doesn't help bring about that understanding. – Mdputs
found aNUL
. This behaviour is NOT guaranteed, and (as the answers are saying) is undefined behaviour. – Mdstrcpy
), or if this is a student whom is expected to run into (a) certain problem(s) - i.e. to prepare for some assignment or lecture, etc. – Md