student@ubuntu:~$
c 1/5 20 XP

C Program Structure

0%

Quick Reference

Minimal C program:

#include <stdio.h>          // 1. Headers

#define MAX 100              // 2. Constants

void greet(void);            // 3. Prototypes

int main(void)               // 4. Entry point
{
    greet();
    return 0;                // 0 = success
}

void greet(void)             // 5. Definitions
{
    printf("Hello!\n");
}

Common headers:

Header Provides
<stdio.h> printf, scanf, fopen, fgets
<stdlib.h> calloc, malloc, free, exit, atoi
<string.h> strlen, strcmp, strcpy, memcpy
<ctype.h> isalpha, isdigit, toupper, tolower
<math.h> sqrt, pow, sin (link with -lm)

Format specifiers:

Specifier Type printf scanf
%d int %d %d
%f double %f %lf (!!)
%c char %c %c
%s string %s %s
%zu size_t %zu

Java → C translation:

Java C
System.out.println(x) printf("%d\n", x);
Scanner sc = new Scanner(System.in) scanf("%d", &x);
public static void main(String[] args) int main(void)
import java.util.* #include <stdio.h>

Common Pitfalls

  • Forgetting \n — printf doesn’t add newlines. You must include \n explicitly.
  • Wrong format specifier%d for int, %f for double in printf but %lf in scanf. Mismatch = garbage.
  • Missing & in scanfscanf("%d", x) crashes. Use scanf("%d", &x).
  • () vs (void) — Empty () means unspecified args in C. Use (void) for no parameters.
  • Forgetting headers — Each function needs its header. printf needs <stdio.h>, malloc needs <stdlib.h>.

Unlocks

Complete this skill to see what it unlocks.