

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Memory Management
incomplete
2: C Program Structure
incomplete
3: Interpreted Quiz
incomplete
4: C Is Compiled
incomplete
5: Comments
incomplete
6: Basic Types
incomplete
7: Strings
incomplete
8: Printing Variables
incomplete
9: Compilation: Types
incomplete
10: Variables
incomplete
11: Constants
incomplete
12: Functions
incomplete
13: Void
incomplete
14: Unit Tests
incomplete
15: Math Operators
incomplete
16: If Statements
incomplete
17: Logical Operators
incomplete
18: Ternary
incomplete
19: Type Sizes
incomplete
20: Sizeof
incomplete
21: For Loop
incomplete
22: While Loop
incomplete
23: Do While Loop
incomplete
24: Pragma Once and Header Guards
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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!