[Solved] C program not printing


Your code is buggy! you don’t allocate memory for the bus[] array, and are trying to access values at garbage location as e.g bus[i] = 0; — Undefined Behavior in C standard, Undefined means you can’t predict how your code will behave at runtime.

This code compiled because syntax-wise the code is correct but at runtime the OS will detect illegal memory access and can terminate your code. (interesting to note: as OS detects memory right violation by a process — An invalid access to valid memory gives: SIGSEGV And access to an invalid address gives: SIGBUS). In the worst case your program may seem execute without any failure, producing garbage results.

To simply correct it define bus array as char bus[N]; else allocated memory dynamically using void* malloc (size_t size);

Additionally, suggestion to your from @Lochemage and @Ran Eldan:

You need to declare bus with a specific size, like char bus[12]. It has to be at least large enough to fit 12 chars because your for loop at the end is iterating through that many (and you can check your code working with this suggestion @codepade).

Also there is no return type to main() in your code, its should be int main(void).

6

solved C program not printing