30
C Review EE319K: Introduction to Embedded Systems Spring 2015

C Review Spring 2015

  • Upload
    alan

  • View
    216

  • Download
    0

Embed Size (px)

DESCRIPTION

C Review Spring 2015

Citation preview

  • C ReviewEE319K: 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

    /*

    this is a block comment

    */

    int main(){

    printf("Hello World!\n");

    }

    Output:

    Hello World!

  • Common Data Types

    Integer types:

    int

    unsigned int

    int16_t

    uint16_t

    int32_t

    uint32_t

    char (int8_t)

    unsigned char (uint8_t)

    Floating point

    types:

    float

    double

    Other types:

    bool

    void

  • 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 Example

    == Equal a == b

    != Not equal a != b

    > Greater than a > b

    < Less than a < b

    >= Grater than or equal a >= b

  • Operators Bitwise

    Operator Description Example

    & Bitwise AND a & b

    | Bitwise OR a | b

    ^ Bitwise XOR a ^ b

    ~ Bitwise NOT/Ones

    Compliment

    ~a

    Right shift a >> 2

    Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm

  • Operators Assignment

    Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm

    Operator Description Example

    = Assignment C = A + B

    += Add AND assignment C += A; // c = c + a

    -= Subtract AND

    assignment

    C -= A; // c = c - a

    *= Multiply AND

    assignment

    C *= A; // c = c * a

    /= Divide AND assignment C /= A

    %= Modulus AND

    assignment

    C %= A

  • Operators Assignment cont.

    Source: http://www.tutorialspoint.com/ansi_c/c_operator_types.htm

    Operator Description Example

    = 2

    &= Bitwise AND assign C &= 2

    ^= bitwise exclusive OR

    and assign

    C ^= 2

    |= bitwise inclusive OR

    and assign

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

    statements;

    }

    else if (expression){

    statements;

    }

    else {

    }

    Example:

    if (grade > 90){

    printf(You got an A!);

    }

    else if (grade > 80){

    printf(You got an B!);

    }

    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;

    default: statements;

    }

    Example:

    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;

    }

    do-while loop

    do{

    statements;

    }while(expression);

    Example:

    int num = 5;

    int factorial = 1;

    while(num > 1){

    factorial *= num;

    --num;

    }

  • Control Statement looping cont.

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

    {

    if(x==5)

    continue;

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

    }

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

    {

    if(x==5)

    break;

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

    }

    continue

    skips the

    statements below

    and move on to the

    next iteration

    break

    exits out of the

    loop completely

  • 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

    #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