Click to play video
In Python you'd do something like this:
python slow_program.py
The Python interpreter then executes that file top-to-bottom. If you have a print() at the top level, then it will print something.
The entire file is interpreted line by line, but that's not how C works.
The simplest C program is essentially:
int main() {
return 0;
}
But a lot is happening here...
main is always the entry point to a C program (unlike Python, which enters at the top of the file).int is the return type of the function and is short for "integer". Because this is the main function, the return value is the exit code of the program. 0 means success, anything else means failure.
{ is the start of the function's body (C ignores whitespace, so indentation is just for style, not for syntax)return 0 returns the 0 value (an integer) from the function. Again, this is the exit code because it's the main function.
0 represents "nothing bad happened" as a return value.; at the end of return 0; is required in C to terminate statements.} denotes the end of the function's body.It feels very different coming from Python, but printing in C is done with a function called printf from the stdio.h (standard input/output) library with a lot of weird formatting rules. To use it, you need an #include at the top of your file:
#include <stdio.h>
Then you can use printf from inside a function:
printf("Hello, world!\n");
Notice the \n: it's required to print a newline character (and flush the buffer in the browser), which print() in Python does automatically.
In case you're wondering, the f in printf stands for "print formatted".
Write a small C program that prints:
Program in C!