C Program Structure
Challenge Gallery
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\nexplicitly. - Wrong format specifier —
%dfor int,%ffor double in printf but%lfin scanf. Mismatch = garbage. - Missing
&in scanf —scanf("%d", x)crashes. Usescanf("%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>.