C Program Structure
Program Structure in clang
In Python you'd do something like this:
python3 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.
Simplest C Program
The simplest C program is essentially:
int main() {
return 0;
}
But a lot is happening here...
-
A function named
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 themain
function, the return value is the exit code of the program.0
means success, anything else means failure.- You'll find a lot of abbreviations in C because 1) programmers are lazy, and 2) it used to matter how many bytes your source code was.
-
The opening bracket,
{
is the start of the function's body (C ignores whitespace, so indentation is just for style, not for syntax) -
return 0
returns the0
value (an integer) from the function. Again, this is the exit code because it's themain
function.0
represents "nothing bad happened" as a return value.
-
The pesky
;
at the end ofreturn 0;
is required in C to terminate statements. -
The closing bracket,
}
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");
Write and execute c
Escirbir un archivo en c que imprime "Hola Mundo", compilalo y correlo
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}