CodeBlanks Logo Icon CodeBlanks

Summary - C basics

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 main and prints with printf
  • Comments use // and /* */
  • printf formats 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

main.c
// write two-line Hello World

Sample solution

main.c
#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

main.c
// define two ints and print Sum: 7

Sample solution

main.c
#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

Question
printf("Result: ____", 5);
Answer
printf("Result: %d", 5);

Explanation: Integers are printed with %d.

All lessons in Beginner