CSSE 132 Mini-reading

C Programming Basics in 10 Minutes

Review C program structure, variables, functions, operators, basic control flow, and formatted output with printf.

Why This Matters

CSSE 132 uses C to connect source code with memory and ARM64 assembly. Before making that connection, you need to read a small C program, trace its operators, and recognize the syntax that tells the compiler what you mean. These are the skills practiced in Worksheet 06.

Core Idea

A C program is made from functions. A function has a return type, a name, an input argument list, and a body enclosed in braces. The body contains statements, and most statements end with a semicolon.

A complete small program

#include <stdio.h>

int add(int a, int b) {
    int result = a + b;
    return result;
}

int main(void) {
    int x = 2;
    int y = 5;
    int total = add(x, y);
    printf("total = %d\n", total);
    return 0;
}
  • #include <stdio.h> provides the declaration for printf.
  • In int add(int a, int b), the first int is the return type, add is the name, and int a, int b is the argument list.
  • int result = a + b; declares result and initializes it in one statement.
  • main returns an int. A return value of 0 reports successful completion.

Worked Examples

Example 1: Trace arithmetic and logical operators

Suppose int a = 3; and int b = 8;. Evaluate the arithmetic first, then any comparison:

ExpressionReasoningValue
a * b3 * 824
b % a8 divided by 3 leaves remainder 22
a > b3 is not greater than 80
a != b3 and 8 are different1
a && bBoth values are nonzero1
a || 0At least one value is nonzero1

C uses 0 for false and treats a nonzero value as true. Comparison and logical operators produce either 0 or 1.

Example 2: Match values with printf specifiers

int code = 68;
char letter = 'b';
printf("code = %c, letter = %d\n", code, letter);

%c prints a value as an ASCII character, so 68 prints as D. %d prints a decimal integer, so 'b' prints as 98. The output is:

code = D, letter = 98

A char occupies one byte and stores a numeric code. Single quotes such as 'b' create a character value; the computer still stores a number.

Example 3: Use character codes in arithmetic

The digit characters appear in order in ASCII. Subtracting the code for '0' converts a digit character to its numeric value:

'7' - '0'
= 55 - 48
= 7

Therefore, if char digit = '7';, then digit - '0' + 4 evaluates to 11. This works for the digit characters '0' through '9'.

Example 4: Trace control flow

int i = 0;
while (i < 3) {
    if (i == 1) {
        printf("middle\n");
    } else {
        printf("edge\n");
    }
    i = i + 1;
}

The loop runs with i equal to 0, 1, and 2. Only 1 satisfies i == 1, so the output is edge, then middle, then edge. The assignment i = i + 1 changes i; the comparison i == 1 only tests it.

Common Mistakes

Using = for comparison

= assigns a value. == tests whether two values are equal. Write if (x == 3) when you mean to compare.

Using Java or Python syntax

C uses else if, not elif. A one-line comment begins with //, and statements such as declarations need semicolons.

Mismatching a format specifier

%d prints a decimal integer, while %c prints the character associated with a numeric code. Choose the specifier for the output you want.

Assuming every type has one fixed size

char is one byte, but some other sizes depend on the platform. For example, long is commonly 8 bytes in an ARM64 Linux environment. Use sizeof(type) when the actual size matters.

Quick Reference

ConceptC form
Declare and initializeint count = 4;
Arithmetic+ - * / %
Compare< > <= >= == !=
Logical operators&& means AND; || means OR
Assignment=
Conditionalif (condition) { ... } else { ... }
Loopwhile (condition) { ... }
Decimal outputprintf("%d", value);
Character outputprintf("%c", value);
Typical type sizeschar: 1 byte, short: 2, int: 4, long long: 8, float: 4, double: 8

Check the platform: C guarantees ordering and minimum ranges for integer types, but not one universal byte size for every type. sizeof reports the size used by the compiler.

Check Yourself

Try these before opening the answers. The values differ from Worksheet 06.

  1. In int square(int n) { return n * n; }, identify the return type, function name, argument list, and body.
  2. If int a = 4; and int b = 9;, what are the values of b % a, a > b, and a && b?
  3. What does printf("%c %d", 65, 'C'); print?
  4. Find three errors in if (x = 3) { printf("x is %c\n", x) } if the goal is to test x and print its decimal value.
  5. What does this loop print?
    int i = 1;
    while (i < 5) {
        printf("%d ", i);
        i = i + 2;
    }

Answers

Show the answers
  1. The return type is int, the name is square, the argument list is int n, and the body is { return n * n; }.
  2. b % a is 1, a > b is 0, and a && b is 1 because both values are nonzero.
  3. A 67. ASCII code 65 represents A, and the code for 'C' is 67.
  4. Use == for the comparison, use %d for decimal output, and add a semicolon after the printf call.
  5. 1 3 . The loop prints 1, adds 2, prints 3, adds 2 again, and stops because 5 is not less than 5.

Next Step

Complete Worksheet 06, then use the C Reference Card to review syntax and operators while writing your next small C program.