The C programming book says that my string must be null terminated to print it using printf, but still the following program prints the string despite it being non-null terminated!
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
char str[10];
for(i = 0; i < 10; i++) {
str[i] = (char)(i+97);
}
printf("%s", str);
}
I am using the Code::Blocks IDE.
It’s undefined behavior to read beyond the bounds of an array. You were actually unlucky it didn’t crash. If you run it enough times or call it in a function, it may (or may not) crash.
You should always terminate the string, or use a width specifier: