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 forprintf.- In
int add(int a, int b), the firstintis the return type,addis the name, andint a, int bis the argument list. int result = a + b;declaresresultand initializes it in one statement.mainreturns anint. 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:
| Expression | Reasoning | Value |
|---|---|---|
a * b | 3 * 8 | 24 |
b % a | 8 divided by 3 leaves remainder 2 | 2 |
a > b | 3 is not greater than 8 | 0 |
a != b | 3 and 8 are different | 1 |
a && b | Both values are nonzero | 1 |
a || 0 | At least one value is nonzero | 1 |
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
| Concept | C form |
|---|---|
| Declare and initialize | int count = 4; |
| Arithmetic | + - * / % |
| Compare | < > <= >= == != |
| Logical operators | && means AND; || means OR |
| Assignment | = |
| Conditional | if (condition) { ... } else { ... } |
| Loop | while (condition) { ... } |
| Decimal output | printf("%d", value); |
| Character output | printf("%c", value); |
| Typical type sizes | char: 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.
- In
int square(int n) { return n * n; }, identify the return type, function name, argument list, and body. - If
int a = 4;andint b = 9;, what are the values ofb % a,a > b, anda && b? - What does
printf("%c %d", 65, 'C');print? - Find three errors in
if (x = 3) { printf("x is %c\n", x) }if the goal is to testxand print its decimal value. - What does this loop print?
int i = 1; while (i < 5) { printf("%d ", i); i = i + 2; }
Answers
Show the answers
- The return type is
int, the name issquare, the argument list isint n, and the body is{ return n * n; }. b % ais 1,a > bis 0, anda && bis 1 because both values are nonzero.A 67. ASCII code 65 representsA, and the code for'C'is 67.- Use
==for the comparison, use%dfor decimal output, and add a semicolon after theprintfcall. 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.