Summary - C basics
- C course
- Beginner
- Lesson 6/116
- 5 min
Combine main, printf and comments to write your first C program.
Summary - C basics
In this group we covered the building blocks of C:
- C is a compiled systems language; you compile it with
gcc - Every program starts from
mainand prints withprintf - Comments use
//and/* */ printfformats output with specifiers like%d,%f
Now solidify what you learned with two small combined tasks.
Practice Write a two-line program
Write a C program with #include <stdio.h> and int main(void). Use \n in a printf call to print Hello and World on two lines.
Starter code
// write two-line Hello WorldSample solution
#include <stdio.h>
int main(void) {
printf("Hello\nWorld");
return 0;
}Expected output
Hello World
Practice Print the sum
Define two int variables (for example 3 and 4) and print Sum: 7 with printf. Use the %d specifier and the addition operator.
Starter code
// define two ints and print Sum: 7Sample solution
#include <stdio.h>
int main(void) {
int a = 3;
int b = 4;
printf("Sum: %d", a + b);
return 0;
}Expected output
Sum: 7
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the core concepts
| Term | Meaning |
|---|---|
main |
Entry point |
printf |
Output |
gcc |
Compiler |
// |
Comment |
2 Complete the specifier
printf("Result: ____", 5);
printf("Result: %d", 5);
Explanation: Integers are printed with %d.