Week 1
Week 1
Outline What is C?
C is:
UNIX and C Programming (COMP1000) ▶ A programming language – one of thousands.
C at a glance
▶ Not the best, but perhaps the most important.
Lecture 1: Basics and Revision Revision ▶ An old language –
▶ Simple and primitive, powerful and pervasive.
Updated: 30th July, 2019 Jumps ▶ A compiled language –
▶ You write “source code”.
Department of Computing Functions ▶ Then, you convert it to “machine code” using a compiler.
Curtin University ▶ The machine code is the final program.
Copyright © 2019, Curtin University Expressions ▶ A statically-typed language –
CRICOS Provide Code: 00301J ▶ Strict data types: integers, real numbers, chars, etc.
C Programs ▶ These are known at compile time (“static”).
▶ A procedural language –
▶ Not object orientated – no classes or objects.
. . . . . . . . . . . .
. . . . . . . . . . . .
1 . . . . . . . . . . . .
https://www.ohloh.net/languages/c . . . . . . . . . . . .
▶ The new versions — actually not very important. ▶ Pointers, pointers, pointers and pointers.
▶ A major extension to C, implementing object orientation (and
▶ You need to know the old C (i.e. C89/C90).
other interesting features). ▶ Engineering Programming (COMP1004) mostly skips around
▶ The old C is the lowest common denominator; used
everywhere. ▶ Very widely used, alongside C. pointers.
▶ If you want a “new” language, why use C at all? Use Java, ▶ Some features of C++ have been retroactively added to C. ▶ Pointers are the heart and soul of C — most of C’s more
C#, Python, etc. instead. ▶ Like C99, C++ will not be assessed in this unit! useful features are built on pointers.
▶ Unfortunately, some C books (e.g. Kochan) don’t say what’s ▶ That said, you also need to know about:
▶ However, the last lecture will discuss C++, and it is well
C99-specific and what isn’t. ▶ The structure of a C program.
worth learning. ▶ The environment (UNIX) in which you’re working.
▶ The lectures will briefly mention C99 features where relevant.
. . . . . . . . . . . .
. . . . . . . . . . . .
Things you should have seen before Remember variables and data types? Variable declarations
These are (essentially) the same in both C and Java: ▶ Each variable stores a single value, depending on its data type. ▶ Variables must be declared before they can be used.
▶ Symbols like semicolons (“;”), braces (“{...}”) and ▶ Simple data types include: ▶ A declaration consists of a datatype and a name:
parentheses (“(...)”). Signed integers: int, short, long. float number; /* Declaration */
▶ Simple data types (e.g. int, double, char). Unsigned integers: unsigned int, unsigned short, number = 5.0 * 2.5; /* Assignment */
▶ Variables and assignments (e.g. “int v = 42;”). unsigned long (cannot be negative).
Reals: float, double, long double. Here, number is declared (created) as a float (real number).
▶ Expressions (e.g. “v = 33 * x + z / 3;”). ▶ Declarations can also initialise variables:
Characters: char (letter, digit, symbol, etc.) –
▶ if, else, switch, break, default, for, while, do, return. technically a kind of integer. float number = 5.0 * 2.5; /* Same as above */
▶ Functions/methods; e.g. ▶ Mainly use int, double and char.
▶ Multiple variables can be declared at once, if they have the
double calculate(double x, double y) ▶ There are no classes or objects in C (unlike Java).
same data type:
{ ▶ Other special data types including pointers, arrays, structs,
unions and enums (which we’ll cover later). int alpha = 3, beta, gamma, delta = 6;
return x * y;
} ▶ C has strings, but no formal “String” data type (we’ll get to Here, alpha, beta, gamma and delta are all ints. Two of
. . . . that too). . . . .
them have been initialised. . . . .
. . . . . . . . . . . .
Jumps — return, break, continue and goto The break statement The break statement
▶ break immediately ends the current switch, for, while or ▶ Can also end a loop (usually from within an if):
do-while statement.
▶ Often used in a switch statement: while(...) {
▶ These statements jump to another place 2 .
...
▶ return and break are necessary in certain places. switch(...) { if(...) {
▶ Many advise against goto under any circumstances. case 1: break;
▶ Some advise against break (outside of switch) and ... }
break; ...
continue as well.
case 2: }
▶ Some advise against multiple returns in a single function. ...
▶ However, it’s worth noting what they actually do. break;
... ▶ This is not necessary — rearranging the if statement will
} achieve the same thing.
▶ break can lead to more compact code.
2
In fact, if, switch, for, while and do-while all jump as well, but they
. . . .
This is normal, and necessary. . . . . ▶ break can also lead to unreadable code. . . . .
provide structure to it. . . . . . . . . . . . .
Historical interest Function calls and returns Function calls and returns
▶ Pre-1989 C had a different function parameter syntax: void calc(void) {
int someFunction(x, y) void calc(void) { ...
int x; ... int number = addTwo(5);
float y; int number = addTwo(5); ...
parameter
{ ... }
value (5) control
... }
} int addTwo(int param)
int addTwo(int param) {
▶ In modern C, this would be written as: { int result = param + 2;
int someFunction(int x, float y) int result = param + 2; return result;
{ return result; }
... }
The call:
}
▶ Function calc() calls function addTwo().
▶ The old form is still valid C, but rarely used. . . . . . . . . ▶ calc() pauses, addTwo() begins. . . . .
▶ param becomes 5.
. . . . . . . . . . . .
Function calls are values Function calls are values A simple C program
/* Here we call beta and gamma, and then alpha. */ /* Here we call beta and gamma, and then alpha. */
alpha(beta(), gamma() + 3.0); alpha(beta(), gamma() + 3.0);
... ...
#include <stdio.h>
float beta(void) { float beta(void) {
return 5.0; return 5.0; int main(void)
} } 10.0 (7.0 + 3.0) {
printf("Hello world.\n");
5.0 return 0;
float gamma(void) { float gamma(void) {
return 7.0 ; return 7.0 ; }
} }
. . . . . . . .
. . . . . . . .
45/46 46/46