C programming language cheat sheet with syntax and code examples

K1=c Cheat Sheet: Essential Syntax and Examples

If you need a fast, practical guide to C programming essentials, this c cheat sheet brings together crucial syntax, keywords, operators, and sample code for instant reference. Whether you’re learning C for the first time or revisiting its fundamentals, this page covers the basics you need to write, understand, and debug simple C programs efficiently.

Basic syntax and structure in C

Every C program starts with key structural elements: function declarations, the main() function, statement terminators, and braces for code blocks. The file extension for a C source file is .c. C is case-sensitive, uses semicolons to end statements, and relies on curly braces to define the start and end of functions or control blocks.

Here’s the minimal structure of a C program:

#include <stdio.h>

int main() {
    // Your code here
    return 0;
}

Key points to remember:

  • #include <stdio.h> is required for input/output functions like printf and scanf.
  • main() is the program entry point. It returns an integer, usually 0.
  • Each statement ends with a ;
  • Code blocks are enclosed in { }
  • Use // for single-line comments or /* … */ for multi-line comments.

Common data types and variables

C has simple but strict data typing. Declare variables before using them, and choose the type that matches your data to optimize memory use and catch errors early. Here are the most common data types for quick reference:

  • int: Integer, typically 4 bytes (int x = 5;)
  • float: Single-precision float, typically 4 bytes (float y = 3.14f;)
  • double: Double-precision float, typically 8 bytes (double z = 2.71828;)
  • char: Single character, 1 byte (char c = ‘A’;)
  • unsigned: For non-negative values only (unsigned int u = 42;)

To declare and initialize multiple variables at once, separate them with commas:

int a = 1, b = 2, c = 3;

Constant values are defined with #define for preprocessor constants or const for typed constants:

#define PI 3.14159
const int DAYS_IN_WEEK = 7;

When using scanf for input, always use & before variable names (except for strings).

Control structures and operators

C uses familiar control structures for decision-making and repetition. The core constructs are:

  • if, else if, else: Conditional branching
  • switch: Multi-way branching on integer/char values
  • for, while, do-while: Looping

Example syntax:

if (x > 0) {
    printf("Positive\n");
} else {
    printf("Not positive\n");
}

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

Operators in C include:

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: && (and), || (or), ! (not)
  • Assignment: =, +=, -=, *=, /=
  • Increment/Decrement: ++, —

Combine these operators to form complex expressions, especially in loop and condition statements.

Essential code examples and tips

To quickly accomplish basic tasks in C, use these ready-to-copy examples as your c cheat sheet reference:

  • Print to console:
    printf("Hello, World!\n");
  • Read user input:
    int n;
    scanf("%d", &n);
    
  • Declare and use an array:
    int numbers[5] = {1, 2, 3, 4, 5};
    printf("%d", numbers[2]); // prints 3
    
  • Create a simple function:
    int add(int a, int b) {
        return a + b;
    }
    
  • Loop through an array:
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    

Tips: Always initialize variables before use to avoid unpredictable results. Use descriptive variable names for clarity. For larger projects, split code into multiple functions to keep main() simple and readable. When in doubt, refer back to this c cheat sheet for syntax reminders and sample snippets.