Comp-206: Introduction To Software Systems Alexandre Denault Computer Science Mcgill University Fall 2006
Comp-206: Introduction To Software Systems Alexandre Denault Computer Science Mcgill University Fall 2006
Next Week
Quiz
Structure of a C program
Preprocessor Commands
Type Definitions
Function Prototypes
Variables
Functions
Core Dump
Segmentation Fault
Variable Declaration
Assigning a value
typedef
Constants
Arithmetic Operations
Comparison Operators
== : equality
!= : not equal
< : smaller than
> : greater than
<= : smaller or equal than
>= : greater or equal than
Logical Operators
&& : AND
|| : OR
! : NOT
(
(
(
(
(a == 5) || (a == 6) ) // a = 5 or 6
(a == 3) && (b == 4) ) // a = 3 and b = 4
!(a == 5) )
// a is not 5
a != 5 )
// a is not 5
If statement
? operator
Switch statement
Break keyword
For loop
While loop
continue will skip to the end of the current iteration to the next
iteration.
break will exit the loop (just as it exits a switch statement).
For example, the following loop will print out the modulus
of 3 smaller than 10.
i = 0;
while(1==1) {
if ((i%3)!=0) continue;
if ((i >= 10)==0) break;
printf(%d\n, i);
}
Arrays
Array of Characters
H e l
l o
W o r
l d ! \0
Functions
Function Prototyping
Incorrect code
main() {
a();
b();
}
void a() {
//do something
}
void b() {
//do something
}
Better code
void a() {
//do something
}
void b() {
//do something
}
main() {
a();
b();
}
Function prototype
main() {
a();
b();
}
void a() {
//do something
}
void b() {
//do something
}
Popular Function
printf
Printf Conversion
%d or %i : signed integer
%x : unsigned hexadecimal integer
%u : unsigned decimal integer
%c : unsigned char
%s : char* (string)
%f : float or double of the form [-]mmm.ddd
%m.df : float or double of the form [-]mmm.ddd where m
and d specifies the maximum number of digits.
%E : double of the form [-]m.dddExx
getchar