1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <stdlib.h>//in C++, this would've been cstdlib (NO .h)
#include <stdio.h>//in C++, this would've been cstdio (NO .h)
#include <string.h>//in C++, this would've been string (NO .h) I think
char first[] = "=First=";
char second[] = "=Second=";
char third[] = "=Last=";
int main() {
printf("%s\n%s\n%s\n",first,second,third);//EXPECTED: =First= \n =Second= \n =Last= \n
printf("\n");
strcpy(second,"012345678");//var second[] now holds: 012345678
printf("%s\n%s\n%s\n",first,second,third);//EXPECTED: =First= \n 012345678 \n =Last= \n
// WRONG EXPECTED: b/c of null character in 012345678'\0' that overwrote first char in var third
strncpy(second,"012345678", sizeof(second));
printf("%s\n%s\n%s\n",first,second,third);
printf("%s",third);//NB: it's gone now...
return 0;
}
|