C is a statically typed language that compiles to native machine code (or to an object file the linker turns into a program). There is no JVM, no bytecode verifier, and no garbage collector. What the compiler accepts can still be undefined at run time.
Mental model: cc -c compiles a .c file to a .o. The linker combines object files and libraries into an executable. A missing #include is a compile problem. A missing .o or library is a link problem. They look different.
| Step | Typical command | Produces |
|---|---|---|
| Preprocess + compile | cc -std=c17 -Wall -Wextra -c hello.c | hello.o |
| Link | cc hello.o -o hello | executable hello |
| One shot | cc -std=c17 -Wall -Wextra hello.c -o hello | same result |
cc on this machine is usually GCC or Clang. The ISO language does not name a vendor.-std=c17 pins the language. Without it you may get GNU C by default.-Wall -Wextra are not ISO; they are how professionals catch mistakes the standard leaves as warnings.cc -std=c17 -Wall -Wextra hello.c -o hello ./hello
#include <stdio.h>
int main(void) {
puts("hello");
return 0;
}
main is the hosted-environment entry point. Returning 0 means success to the operating system.int main(void). With arguments: int main(int argc, char *argv[]).void main() is not standard hosted C. Do not write it.main without return is defined to return 0 (C99+). Still write return 0; in teaching code.#include <stdio.h> is a preprocessor directive, not a Java import. See Preprocessor and Headers.Wrong main: void main(), or main() with an empty parameter list in new code (that is an old “unspecified arguments” form, not void).
The lexer splits source into tokens. You must be able to name each kind.
| Kind | Examples | Rule |
|---|---|---|
| Identifier | count, size_t | Letter or _, then letters/digits/_. Cannot be a keyword. |
| Keyword | int, if, return, sizeof | Reserved. sizeof is an operator spelled as a keyword. |
| Literal | 42, 3.14, 'A', "hi" | Integer, floating, character, string. There is no true keyword in C (use <stdbool.h>). |
| Punctuator | ; ( ) { } [ ] * & -> | Structure and operators. |
int if they fit, otherwise a wider type. Suffix U, L, LL as needed.1st does not compile._Foo.// end of line (C99) /* block — may span lines */
/* outer /* inner */ still-code */ is a classic trap.// — that is not a comment.// inside a directive line still comments out the rest of that line.| Thing | Common style | Example |
|---|---|---|
| Function, variable | snake_case | item_count, read_line |
| Type (typedef / struct tag) | snake_case or PascalCase in a project | node, Node |
| Macro constant | UPPER_SNAKE | MAX_BUFFER |
| Header guard | UPPER_SNAKE unique | UTIL_STRINGS_H |
The compiler does not enforce style. Pick one and keep it. POSIX and the C library use lowercase with underscores.
.c file is typically one translation unit (after includes are expanded)..h. Definitions (the actual function body, the actual object) go in one .c.hello.c.-o, not the source name./* Incorrect — definition in a header included twice → multiple definition at link */ /* util.h */ int n = 1; /* Correct — declaration in the header, definition in one .c */ /* util.h */ extern int n; /* util.c */ int n = 1;
-Wall is telling you about implicit declarations and bad printf conversions.undefined reference to main or to your function).void main() because an old book did.#include is text inclusion, not a module system.-std=c17 -Wall -Wextra. Fix warnings.int main(void) and return 0._ + uppercase.~ Good luck with your revision! ~