studyhubProgramming 1

Syntax sheet

Programming 1 on one page, module by module. The middle column is what the syntax does; the right-hand column is the mistake that particular line invites.

The basic structure

Module 3a · in this order

/* … */documentation section: the header comment — what, who, whenComments are never compiled; a program runs the same without them.
#include <stdio.h>link section: headers whose functions the program usesstdio.h declares printf and scanf.
#define PI 3.14159definition section: symbolic constants (macros)No = and no ; — the preprocessor pastes 3.14159 wherever PI appears.
int total = 0;global declaration section: usable in every functionDeclared outside every function, above main.
int add(int a, int b);function prototype declaration sectionA prototype ends with ; — a definition has a body instead.
int main() { … }the main function: a declaration part, then an execution partEvery statement in both parts ends with ;.
int add(int a, int b) { … }user-defined function definitions, after mainCalled from main; the prototype above lets main call it first.

From prog.c to the screen

Module 3a · execution flow

prog.c → preprocessor → prog.i#include and #define are expandedThe preprocessor runs before the compiler ever sees the code.
prog.i → compiler → prog.sC becomes assembly codeSyntax errors stop the program here — nothing runs.
prog.s → assembler → prog.objassembly becomes object code
prog.obj → linker → prog.exelinked with the library functions
prog.exe → loader → memoryloaded and run; output goes to the console

Output

Module 3b · printf

printf("text");prints exactly the text — nothing moreNo newline unless the string holds \n.
printf("%s\n", name);the usual way to print a lineThe \n is yours to write — printf adds none.
puts("text"); putchar('A');side notes: a string plus a newline, and one characterNeither takes placeholders. Use printf everywhere.
return 0;main ends; 0 tells the system it succeededExecution starts at main, wherever main is written.

Escape sequences

Module 3b · the execution character set

\nnewline\n is one character, not two.
\ttab — to the next stop, every 8 columnsName:\tJuan lines up at column 9, not 4 spaces on.
\\ \" \'a backslash, a double quote, a single quote"\" never ends: its closing quote is escaped.
\0the null character, code 0 — ends every stringPrinting stops at the first \0.
%%a percent sign, inside printf's format% is not an escape; a lone % starts a placeholder.

Format specifiers

Module 3b · printf and scanf

%d %iint%d given a double prints garbage — printf never converts.
%ffloat or double, 6 decimals by default%f given an int prints garbage. Cast: (double) n.
%.2f2 decimals, rounded%2f is width 2, not 2 decimals.
%cchar — the character with that code%d of a char prints its code: 'A' is 65.
%sstring — chars up to the \0%s given a char crashes. Use %c.
%5d %-5d %05dwidth 5: right-aligned, left-aligned, zero-paddedWidth is a minimum; longer values are never cut.
%.3sat most 3 characters of the stringPrecision on %s truncates; width on %s pads.
%e %g %x %uscientific, shortest, hexadecimal, unsigned%g drops trailing zeros: 2.50 prints as 2.5.

Input

Module 3b · scanf

scanf("%d", &n);reads an int into nThe & is required: scanf needs where n lives.
scanf("%f", &x);reads a floatFor a double, scanf needs %lf. (printf is fine with %f.)
scanf("%s", name);reads one word into a char arrayNo & for an array. Stops at the first space.
scanf(" %c", &c);skips spaces and newlines, then reads a charWithout the space, %c reads the newline Enter left behind.
scanf("%[^\n]", line);reads the rest of the line, spaces includedStops before the newline — which is still waiting.
fgets(line, 30, stdin);side note: the other way to read a line, at most 29 charsUnlike scanf, it keeps the \n at the end.
scanf's returnhow many values it stored; EOF when input ran outwhile (scanf(…) == 1) loops until input fails.

Variables and data types

Module 3b

int %d4 bytes, a whole number-2,147,483,648 to 2,147,483,647.
float %f double %lf4 and 8 bytes, with a fractional partscanf needs %lf for a double; printf takes %f or %lf.
char %c1 byte, one character'A' is a char constant; "A" is a string constant.
local variabledeclared inside a function or block; lives there onlyHolds garbage until it is given a value.
global variabledeclared outside every functionEvery function can read and change it.
static int y = 10;keeps its value between callsInitialized once, not on every call.
identifiersletters, digits, _ ; not starting with a digitNo spaces, no # or $, no keyword. Case-sensitive: Total ≠ total.

Functions

Module 4

int calcSum(int x, int y);declaration (prototype): name, parameter types, return typeEnds with ; and comes before main.
total = calcSum(a, b);call: a and b are the arguments (actual parameters)The call's arguments must match the parameter list in type, order and number.
int calcSum(int x, int y) { … }definition: the header, then the body; x and y are formal parametersA non-void function must return a value of its return type.
void f(void)receives nothing, returns nothingCalled as f(); on its own line — there is no value to store.
void f(int a) · int f() · int f(int a)the other three designsOnly a non-void function's call can sit on the right of =.
pass-by-copythe argument's value is copied into the parameterChanging the parameter never changes the caller's variable.

Selection and jumps

Module 5a

if (c) …runs when c is non-zeroif (x = 0) stores 0 and is always false.
if (c); …the ; is the whole if — the next line always runsNever put ; straight after if (…).
if … else if … elsethe first true branch runs, then the chain endsCheck the highest range first: 90, then 80, then 75.
elsebelongs to the nearest if that has noneIndentation doesn't decide it. Use braces.
switch (x) { case 1: … break; }jumps to the matching case, runs until breakNo break: it falls into the next case's code.
case 1: case 7:stacked cases share codecase labels are constants — no ranges, no variables.
switch (expression)the expression must be an integer typeA char works — it is a small integer. A double or a string doesn't.
c ? a : bthe ternary operator: a if c is true, else bFrom the exam coverage list. It is an expression — it gives a value.
goto label;jumps to label: anywhere in the functionThe slides say to avoid it; a loop or an if does the job.
return expr;ends the function, handing expr to the callerNothing after a return in the same block runs.

Loops

Module 5b

while (c) { … }checks first — may run zero timesUpdate the loop variable, or it never ends.
do { … } while (c);runs once, then checksNeeds the ; after while (c).
for (init; c; update)init once, check, body, update, check…for (…); with a ; runs an empty body.
i < n vs i <= nn rounds from 0; n rounds from 1The off-by-one: count the rounds for a tiny n.
for ( ; ; )an infinite loop: no condition means always trueSomething inside must break out, or it never ends.
break (Module 5a)leaves the innermost loop or switchIn nested loops, only the inner one ends.
continue (Module 5a)skips to the next roundIn a for, the update still runs; in a while, an update below it is skipped.
after the loopthe variable keeps the value that failed the testfor (i = 0; i < 5; i++) leaves i at 5.

Pointers

Module 7

int *p;declares p, a pointer to an intThe * here declares; it doesn't follow anything yet.
p = &x;referencing: p holds x's address&x is the address of x, not its value.
y = *p;dereferencing: the value at the address p holdsThe pointer and what it points to must be the same type.
*p = 20;changes x itself, through pp = 20 would store 20 as the address instead — gcc warns, and the program breaks.
printf("%p", p);prints an addressDifferent on every run; never the same as the value.

Functions, part 2

Module 8

void swap(int *a, int *b)pass-by-reference: the parameters hold addressesCalled as swap(&d, &e) — the & is required.
tmp = *a; *a = *b; *b = tmp;swaps the caller's variablesswap(int a, int b) swaps its own copies and changes nothing outside.
recursiona function that calls itselfFrom the exam coverage list: it needs a base case that doesn't recurse.
if (n <= 1) return 1;the base case: where the calls stopWithout one the calls never end — a runtime error, not a compile error.

Arrays

Module 9 · one-dimensional

int a[5];five ints, a[0] to a[4]a[5] is past the end. C won't stop you.
int a[5] = {1, 2};1, 2, 0, 0, 0 — the rest are zeroedWith no initializer at all, the values are garbage.
int a[] = {4, 8, 15};length taken from the list: 3sizeof(a) / sizeof(a[0]) counts elements.
for (i = 0; i < n; i++)the traversal: every element onceStart a max at a[0], never at 0.
int a[1000] = {0};every element 0Only 0 spreads like this; {5} gives 5, 0, 0, …
&a[i] a + ielements sit side by side: a float array's addresses go up by 4If mark[0] is at 2120, mark[2] is at 2128.
void f(int arr[], int size)an array parameter: the array itself, not a copyChanges made inside f stay in the caller's array. Pass the size too.
insert at the frontshift right, starting from the last elementShifting from the front copies one value over everything.
remove from the frontshift left, starting from the first elementThen take one off the count.

Extra practice — not in the slides

Operators

Extra practice · highest precedence first

() [] x++ x--grouping, indexing, post-incrementx++ hands over the old value, then adds 1.
++x --x ! ~ - (type) sizeofunary, right to left(float) a / b casts a, then divides.
* / %multiply, divide, remainder7 / 2 is 3 with ints. -7 % 3 is -1: the sign follows the left side.
+ -add, subtracta - b - c is (a - b) - c.
<< >>shift bits left, rightx << 1 doubles; x >> 1 halves, dropping the remainder.
< <= > >=relational — gives 1 or 018 <= age <= 59 is always 1. Use && .
== !=equal, not equal= assigns; == compares.
& ^ |bitwise and, xor, orBelow == : x & 1 == 0 means x & (1 == 0).
&& ||logical and, or — stop earlyThe right side may never run: y++ inside it may not happen.
? :condition ? if true : if falseBoth branches should have the same type.
= += -= *= /= %= &= |= ^= <<= >>=assignment, right to leftx *= 2 + 3 is x = x * (2 + 3).

Typecasting

Extra practice · what a value becomes

int / intan int — the fraction is dropped(80 + 85) / 2 is 82 even when stored in a float.
int op doublethe int becomes a double firstMake one side a double: / 2.0, or (double) sum / n.
(int) 9.999 — truncated, never rounded(int) -9.99 is -9. To round: (int) (x + 0.5) for x ≥ 0.
int n = 3.7;n is 3 — assignment converts silentlyNo warning at run time; the fraction is simply gone.
'A' + 267 — a char is a small int%c prints it as C; %d prints 67.
char c = 300;c is 44 — only the low 8 bits fitA char holds -128 to 127.

Built-in libraries

Extra practice · include the header, or it won't compile

<ctype.h>isalpha isdigit isalnum isspace isupper islower ispunct — toupper toloweris… returns non-zero for true, not 1. Test with if (isdigit(c)).
strlen(s)characters before the \0strlen("hi") is 2; sizeof a char s[] = "hi" is 3.
strcpy(d, s) strcat(d, s)copy s into d; append s to dd must have room for it all plus the \0.
strncpy(d, s, n)copy at most n charactersAdds no \0 when s is n characters or longer.
strcmp(a, b)0 if equal; negative if a comes first; positive if afterNever a == b. "Z" comes before "a"; "10" before "9".
<math.h>sqrt pow fabs ceil floor round fmod — all return doubleprintf a math.h result with %f, not %d.
ceil floor roundup, down, nearest (halves away from zero)floor(-2.5) is -3.0; ceil(-2.5) is -2.0.
abs(n) <stdlib.h>absolute value of an intFor a double use fabs, from math.h.

Two-dimensional arrays

Extra practice

int m[2][3];2 rows × 3 columns — m[row][col]Outer loop rows, inner loop columns.
char w[3][10] = {"one", …};an array of strings: 3 rows, each up to 9 charsw[i] is a whole string; w[i][j] is one char.