C Review Spring 2015
C Review Spring 2015
Outline
Hello World!
Operators
Control Statements
Functions
Macros
Pointers
Arrays
Struct
Linked List
Hello World
// test.c
#include <stdio.h>
/*
this is a block comment
*/
Output:
Hello World!
int main(){
printf("Hello World!\n");
}
Integer types:
int
Floating point
types:
unsigned int
float
int16_t
double
uint16_t
Other types:
int32_t
bool
uint32_t
void
char (int8_t)
Examples:
bool finished; // declaration
uint32_t size = 42; // declaration and definition
Conventions:
Use
Operators Arithmetic
Operator Description
Example
Addition
a + b
Subtraction
a b
Multiplication
a * b
Division
b / a
Modulus
b % a
++
Increment (by 1)
a++ or ++a
--
Decrement (by 1)
a-- or --a
Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
Example
a == b
!=
Not equal
a != b
>
Greater than
a > b
<
Less than
a < b
>=
a >= b
<=
a <= b
&&
Logical AND
a && b
||
Logical OR
a || b
Logical NOT
!a
Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
Operators Bitwise
Operator Description
Example
&
Bitwise AND
a & b
Bitwise OR
a | b
Bitwise XOR
a ^ b
~a
<<
Bitwise NOT/Ones
Compliment
Left shift
>>
Right shift
a >> 2
Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
a << 2
Operators Assignment
Operator Description
Example
Assignment
C = A + B
+=
C += A;
// c = c + a
-=
Subtract AND
C -= A;
assignment
Multiply AND
C *= A;
assignment
Divide AND assignment C /= A
// c = c - a
*=
/=
%=
Modulus AND
assignment
Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
C %= A
// c = c * a
Example
<<=
>>=
C >>= 2
bitwise exclusive OR
and assign
bitwise inclusive OR
and assign
C ^= 2
&=
^=
|=
Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
C &= 2
C |= 2
if statement
if (expression){
statement;
statement;
}
if else statement
if (expression){
statements;
}
else{
statements;
}
Example:
}
Example:
if (temperature > 90)
fever = true;
else
fever = false;
if else if statement
if (expression){
Example:
statements;
else if (expression){
statements;
else {
Switch statement
Example:
default: statements; char letter_grade;
switch (letter_grade){
case A: gpa += 4; break;
case B: gpa += 3; break;
case C: gpa += 2; break;
defuault: gpa += 0;
}
Selector
(expression)? value_if_true:value_if_false;
for loop
for(initialization; termination; increment){
statements;
}
Example:
for(int i = 0; i < 5; ++i)
printf(%d\n, i);
Output:
0
1
2
3
4
while loop
while(expression){
statements;
Example:
int num = 5;
int factorial = 1;
do-while loop
do{
statements;
}while(expression);
continue
skips the
statements below
and move on to the
next iteration
break
exits out of the
loop completely
Functions
Example:
int sum (int a, int b){
int c = a + b;
return c;
}
Functions cont.
int main (){
int x = 2, y = 3;
int z = sum(x, y);
printf(%d\n, z);
}
int sum(int a, int b){
return a + b;
}
Compile
Error!!
Functions cont.
int sum(int a, int b);
Output:
5
Scope
// example.c
int x; // x is global
int main(){
int y = 4; // y is local
x = 0;
for(int i = 0; i < y; ++i)
// i is only defined inside the for loop
++x;
}
void foo(int a){ // a is local
int y; // this y is different from y in main
int x; // this x is different from global
}
Macros
Example:
// example.c
#include <stdio.h>
Pointers
Examples:
int *p; // this is a pointer to an integer
char* name_p; // pointer of char
& : address of
* : dereference
Pointers cont.
// example.c
int main(){
int x = 5;
int *p = &x; // p is address of x
*p = 14; // assign 14 to the location
pointed by p
Output:
x = 14;
Arrays
FIXED LENGTH!!
type array_name[array_size];
Example:
int main(){
Struct
Example:
struct Student{
char name[50];
int id;
}; // make sure to end with semicolon here!
int main(){
struct Student s= {Jenny Chen, 12345};
printf(ID: %d\n, s.id );
// use . to access object member
}
Struct cont.
Example:
Lined List
Example:
struct Student_node{
char name[50];
int id;
struct Student_node* next;
};
Jenny
Matt
Steven
12345
54321
56789