i have these two c programming code .They are identical except for one step and because of that their output is totally different please help me why is this happening
*p=0; //only change is here rest is same
and output is
For ease of explanation, let's consider we're sticking to ASCII values all over.
In the first case,
puts the ASCII value of
48) into the memory pointed by
In second case,
*p = 0;
puts the ASCII value
0) itself into the memory pointed by
Hence, in the first case, for the string supplied as the argument to
%s the value at the given index (4) is
48, which makes it print the literal 0 and continue until it finds a null-terminator.
Now, in the second case, because the ASCII
0 indicates the null-character at that given index,
%s finds the end of the string and stops there.