0% found this document useful (0 votes)
40 views

C Review Spring 2015

The document provides an outline and overview of key concepts for an introduction to embedded systems course, including common data types, operators, control statements, functions, macros, pointers, arrays, structs, and linked lists. The outline covers topics like declaring variables, arithmetic and logical operators, if/else conditional statements, for/while loops, functions and scope, defining macros, pointers and pointer arithmetic, arrays and their relationship to pointers, defining structs to group data, and using pointers to link nodes in a linked list.

Uploaded by

Alan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

C Review Spring 2015

The document provides an outline and overview of key concepts for an introduction to embedded systems course, including common data types, operators, control statements, functions, macros, pointers, arrays, structs, and linked lists. The outline covers topics like declaring variables, arithmetic and logical operators, if/else conditional statements, for/while loops, functions and scope, defining macros, pointers and pointer arithmetic, arrays and their relationship to pointers, defining structs to group data, and using pointers to link nodes in a linked list.

Uploaded by

Alan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C Review

EE319K: Introduction to Embedded Systems


Spring 2015

Outline

Hello World!

Common Data Types

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");
}

Common Data Types

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)

unsigned char (uint8_t)

Declaring and Defining Variables

Every variable needs a type


type variable_name;

Examples:
bool finished; // declaration
uint32_t size = 42; // declaration and definition

Rules for variable names:


Can

only consist of alphabets, digits and


underscore
Cannot start with a digit

Declaring and Defining Variables cont.

Conventions:
Use

meaningful names (but not too long)

Use average, sum, rather than x, y


Constants

are usually all caps

const double PI = 3.14;


Unlike

Java, underscore is used to break up


multiple words in variable names
int grade_average; vs. int gradeAverage;

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

Operators Logical (or Relational)


Operator Description
==
Equal

Example
a == b

!=

Not equal

a != b

>

Greater than

a > b

<

Less than

a < b

>=

Grater than or equal

a >= b

<=

Less than or equal

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

+=

Add AND assignment

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

Operators Assignment cont.


Operator Description

Example

<<=

Left shift AND assign C <<= 2

>>=

Right shift AND


assign
Bitwise AND assign

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

Control Statements - conditional

if statement
if (expression){
statement;
statement;

}
if else statement
if (expression){
statements;
}
else{
statements;
}

Example:

if (temperature > 99.6){


fever = true;
printf(You are having a fever!);

}
Example:
if (temperature > 90)
fever = true;
else
fever = false;

Control Statements - conditional

if else if statement

if (expression){

Example:

if (grade > 90){

statements;

printf(You got an A!);

else if (expression){

else if (grade > 80){

statements;

printf(You got an B!);

else {

else if (grade > 70){


printf(You got an C!);

Control Statements conditional cont.

Switch statement

switch(expression){ // note: expression MUST be


integer type
case some_value: statements; break;

case some_value: statements; break;

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;
}

Control Statements conditional cont.

Selector
(expression)? value_if_true:value_if_false;

Generally used in assignment


Example:
int min = (a > b)? b:a;

Control Statement - looping

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

Control Statement looping cont.

while loop
while(expression){
statements;

Example:
int num = 5;
int factorial = 1;

do-while loop

do{
statements;
}while(expression);

while(num > 1){


factorial *= num;
--num;
}

Control Statement looping cont.

continue
skips the
statements below
and move on to the
next iteration

for(int x = 0; x < 10; ++x)


{
if(x==5)
continue;
printf("%d\n",x);
}

break
exits out of the
loop completely

for(int x = 0; x < 10; ++x)


{
if(x==5)
break;
printf("%d\n",x);
}

Functions

Similar to subroutines in assembly language

4 important parts: return type, function


name, argument list, and function body

Example:
int sum (int a, int b){
int c = a + b;
return c;
}

IMPORTANT: function needs to be declared


before use

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);

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;
}

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

Use #define to define a label/symbol

Example:
// example.c
#include <stdio.h>

#define SW1 0x01


int main(){

int input = data_reg & SW1;


}

Pointers

Pointers are addresses to data in memory

Pointers of any type can be declared with *

Examples:
int *p; // this is a pointer to an integer
char* name_p; // pointer of char

Two new operators:

& : 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

printf(x = %d\n, x);


}

Output:
x = 14;

Arrays

Data structure that stores data sequentially in memory

FIXED LENGTH!!

type array_name[array_size];

All arrays starts with index 0

Example:
int main(){

int grades[10]; // allocates a block of memory for 10


integers
grades[0] = 95; // assign 95 to first element of grades

grades[10] = 100; // segmentation fault!


double prices[] = {1.1, 2.2, 3.3, 4.4, 5.5};

Arrays and Pointers

array_name is actually a pointer to the first


element
int x[10], *ptr;

ptr = x; // is the same thing as ptr = &x[0];

Examples of pointer arithmetic


*(ptr + 2) = 14; // x[2] = 14;

ptr++; // ptr now points to x[1];

Struct

Struct is a way to group data together

Allows user to define his/her own type

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.

Pointer to struct, use -> to access elements

Example:

struct Student *s_ptr= &(Student_node){ Jenny


Chen , 12345};
printf(ID: %d\n, s_ptr->id );
// use -> to access member using a pointer

Lined List

Stores a list of data, but not in sequential order

Instead, use pointers to point to the next element

Example:
struct Student_node{
char name[50];

int id;
struct Student_node* next;
};
Jenny

Matt

Steven

12345

54321

56789

You might also like