I know that strings are just an array of chars with adjacent memory addresses. So when you have a character array:
char s[5];
s[0] = '1';
s[1] = '2';
s[2] = '3';
s[3] = '4';
s[4] = '5';
and change character array at s[1] to "5" then printing such an array should return "15345". Now my question is about scanf and strtol functions. When I insert values into the array s using scanf twice using different sized strings, Why is it that the strtol function does not convert the ENTIRE array?
Here is my code as example:
#include <stdio.h>
#include <stdlib.h>
int main(){
char bytes[5];
printf("enter size 1: ");
scanf("%s", bytes);
printf("the size is: %ld\n", strtol(bytes, NULL, 10));
printf("enter size 2: ");
scanf("%s", bytes);
printf("the size is: %ld\n", strtol(bytes, NULL, 10));
return 0;
}
So imagine these user inputs:
10000
the program would then print out "the size is 10000"
then the user inputs:
100
the program then prints "the size is 100"
why doesn't it print out "the size is 1000" again? I only stored 100 into bytes, shouldn't the remaining array elements of bytes from the first input be unchanged and strtol should convert the rest of the array right?
In my mind, when the program stores the first input of 10000 into the array bytes, it looks like this at that moment
bytes = {1,0,0,0,0}
then when the user inputs 100, the array looks the same since it only changed the values of the first 3 elements and the rest of the array should remain the same:
bytes = {1,0,0,0,0}
with that strtol would convert the entire array to 10000 right?
Does scanf essentially "empty" out the rest of the array when storing values into the same memory address?