Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

This C syntax quick reference covers the core patterns students need to read, write, and debug C programs. It helps students remember how declarations, expressions, control structures, functions, arrays, pointers, structs, and input/output fit together. A cheat sheet is useful because C is strict about punctuation, types, and memory, so small syntax errors can stop a program from compiling.

The most important ideas are declaring variables with a type, ending statements with semicolons, using braces to group code, and choosing the correct control structure. Functions organize code with a return type, name, parameter list, and body. Arrays store sequences of same-type values, pointers store memory addresses, and structs group related fields into one custom data type.

Key Facts

  • A variable declaration has the form type name; or type name = value;, such as int count = 0;.
  • Every simple C statement must end with a semicolon, such as total = total + 1;.
  • An if statement has the form if (condition) { statements } else { statements }, and the condition must be inside parentheses.
  • A for loop commonly has the form for (int i = 0; i < n; i++) { statements }, where initialization, condition, and update are separated by semicolons.
  • A function definition has the form return_type function_name(parameter_list) { statements; return value; }.
  • An array declaration has the form type name[size];, and valid indexes run from 0 to size - 1.
  • A pointer stores an address, so int *p = &x; makes p hold the address of x, and *p accesses the value stored there.
  • Formatted input and output use printf("format", values); and scanf("format", &variables);, such as scanf("%d", &age);.

Vocabulary

Declaration
A statement that tells the compiler the type and name of a variable, function, array, pointer, or struct.
Data Type
A category that determines what kind of value a variable can store and how much memory it uses.
Function
A named block of code that can receive parameters, perform a task, and optionally return a value.
Array
A fixed-size sequence of values of the same type stored in consecutive memory locations.
Pointer
A variable that stores the memory address of another value.
Struct
A user-defined type that groups related variables, called fields or members, under one name.

Common Mistakes to Avoid

  • Forgetting a semicolon after a statement is wrong because C uses semicolons to mark the end of most statements.
  • Using = instead of == in a condition is wrong because = assigns a value, while == compares two values.
  • Accessing array[index] where index equals the array size is wrong because C arrays start at 0 and end at size - 1.
  • Passing a variable to scanf without & is wrong for basic variables because scanf needs the variable’s memory address to store input.
  • Using an uninitialized variable is wrong because its value is unpredictable and may cause incorrect results.

Practice Questions

  1. 1 Write a C declaration for an integer variable named score and initialize it to 95.
  2. 2 What values of i are printed by this loop: for (int i = 0; i < 4; i++) printf("%d ", i);?
  3. 3 Given int x = 10; int *p = &x; *p = 25;, what is the value of x after these statements run?
  4. 4 Explain why C requires both a type and a name when declaring a variable.

Understanding C Syntax Quick Reference

C programs pass through several stages before they run. The compiler checks whether the code follows the language rules and converts each source file into machine code. The linker then joins your code with library code, such as the code that supports printing text.

A compiler error means translation could not finish. A warning means the program may still build, but the compiler noticed a risky pattern. Treat warnings seriously.

They often reveal an uninitialized variable, a wrong type, or a mistaken comparison. C allows some conversions automatically, which can hide errors.

For example, dividing one integer by another integer drops any fractional part. Storing a large value in a smaller type can lose data.

Variable lifetime and scope explain many confusing bugs. A local variable exists only while its function is running. It can be used only inside the block where it was declared.

A variable declared inside a loop body is created again on each pass through that block. A global variable exists for the whole program, but using many globals makes programs harder to test because distant parts of the code can change the same data. Initialize variables before reading them.

Reading an uninitialized local variable has undefined behavior. That means C does not promise a sensible result. The program might appear to work on one computer and fail after a small unrelated change.

Arrays and pointers are closely connected, but they are not identical. In many expressions, an array name becomes the address of its first element. This is why a function can receive an array through a pointer parameter.

The function does not automatically know the array length, so pass the length separately when needed. C does not check array boundaries for you. Accessing one position before the start or after the end can overwrite other memory.

Text strings need special care. A C string is a character array ending with a zero character.

If that ending character is missing, printing the string may continue into unrelated memory. This matters in real programs that process names, files, sensor readings, and messages.

Functions make code easier to reason about when each one has one clear job. Ordinary arguments are passed by value, so changing a parameter does not change the caller's variable. To let a function update a caller's value, pass its address and use a pointer parameter.

Structs are useful when several values describe one thing, such as a student record with an identification number, score, and name. Use a dot to access fields in a struct variable, while a pointer to a struct uses an arrow. For input, check the return value from scanf because input can fail or have the wrong format.

For full lines of text, fgets is often safer because it limits how many characters are stored. Build programs in small steps, compile often, test boundary values, and use a debugger or print statements to inspect values when the result does not match your expectation.