C chapters

Chapter 16 of 17

C Syntax Cheatsheet

Try this in our C Compiler →

Everything in one place

Use this chapter as a quick reference once you've worked through the rest of the course. It collects every core construct of C into runnable, copy-pasteable snippets you can scan whenever you forget the exact syntax.

Hello world & variables

```c
#include <stdio.h>

int main(void) { printf("Hello, World!\n"); return 0; }

int age = 25; char name[] = "Aman"; float pi = 3.14f; printf("%s is %d\n", name, age); ```

Data types & operators

```c
int n = 42;
float f = 3.14f;
double d = 2.71828;
char c = 'A';
int arr[3] = {1, 2, 3};

int a = 10, b = 3; printf("%d %d %d %d\n", a+b, a-b, a*b, a/b); printf("%d\n", a % b); printf("%d\n", (a > 5) && (b < 5)); ```

Conditionals & loops

```c
int score = 72;
if (score >= 90) printf("A\n");
else if (score >= 60) printf("Pass\n");
else printf("Fail\n");

for (int i = 0; i < 5; i++) { printf("%d\n", i); }

int n = 0; while (n < 3) n++; ```

Functions & arrays

```c
int add(int a, int b) {
    return a + b;
}

int main(void) { printf("%d\n", add(2, 3)); return 0; }

int nums[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) { printf("%d\n", nums[i]); } ```

Strings & objects

```c
#include <string.h>
char name[] = "PlayLearn";
printf("%lu\n", strlen(name));
char greet[64];
sprintf(greet, "Hello, %s!", name);

struct User { char name[32]; int age; };

struct User u = {"Aman", 20}; printf("%s is %d\n", u.name, u.age); ```

Errors, modules & I/O

```c
#include <errno.h>
#include <stdio.h>
FILE *f = fopen("missing.txt", "r");
if (f == NULL) {
    perror("open failed");
}

// math.h int add(int a, int b);

// math.c #include "math.h" int add(int a, int b) { return a + b; }

// main.c #include "math.h" int main(void) { return add(2, 3); }

char name[64]; printf("Your name? "); scanf("%63s", name); printf("Hello, %s\n", name); ```

How to use this page

Bookmark it. When you start a new project, open this cheatsheet alongside your editor and use it as a syntax lookup. Once a construct becomes second nature you'll stop needing the reference for it — and that's exactly when you know you've mastered that part of C.

Try it yourself

C Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.