C BASICS

 

Content

 

C Lang Intro

Flow Control

Loops

Arrays

 

Example: Bubble sort

Example: Sieve of Eratosthenes

 

C Lang Intro

C Lang Intro

  • Flexible & compact lang
  • From operating systems to games
  • Basis for more advanced langs
  • Lang defined by an international standard (C11)
  • Some elements in C11 optional
  • C11 also defines a standard library for C
    • Common constants, symbols, functions, etc.
    • Optional extensions to C lang
  • Machine-dependet facilities implemented in machine-independet   form (e.g. I/O)
  • Standard library defined by a set of header files (.h)
  • App dev includes the according headers

Compiler & Linker

  • Compiler converts source code into machine lang

  • Detects and reports errors

  • Compiler outputs object code to object files (.obj, .o)

  • Compilation phases

    • Preprocessing phase: source code may be modified or added

    • Actual compilation
  • Linker
    • Combines object files
    • Adds required modules from std lib
    • Links everything to an executable
    • Also reports problems (reference errors)

 

C Development

 

  1. Build with command line
    1. Start - Visual Studio Tools
    2. Developer Command Prompt - cd to dev folder
    3. cl main.c (compiles and links main.c)
  2. Create C++ Project in Visual Studio
    1. File - New - Project
    2. Visual C++ - Empty Project
    3. Add a source file

First App

// main.c                        // This is a comment
#include <stdio.h>               // This is a preprocessor directive

int main()                       // Define function main
{                                // Mark beginning of main
    printf("Hi, there!\n");      // Print string to console
    return 0;                    // Return control to operating system
}                                // Mark end of main

Comments

// Comment 1: Compiler ignores everything with two slashes on a line

/* Comment 2 */

/* 
 * Comment 3:
 * Compiler ignores everything between /* ... */
 */

Preprocessing Directives

#include <stdio.h>         // # marks a preprocessing directive 

#define PI 3.1415          // define a constant
  • Preprocessing directive tells the compiler to do something before comping
  • Include header file into, define constants, etc...
  • Header file provides info about types and functions in the stdlib
  • Compiler uses info to integrate predefined functions or other objects to the app
  • E.g. info about printf(..)

Functions

int main()     // Function header 
{              // Opening brace
    // ...     // Function body
}              // Closing brace
  • Functions as named block of code that does something
  • Every C app needs at least the main function
  • Function signature
    • defines return type
    • defines function name
    • defines input parameters with types
  • Keywords (reserved words) used to describe types (int, void, etc.)

Variables and Memory

  • App and data is stored in RAM
  • 0/1 in memory
  • 8 bit groups - byte
  • Each byte is labeled with a number (address)
  • 1KB - 1024bytes
    • 1023 = 2^10 = 11 1111 1111
  • Variables as piece of memory
    • with one or more contiguous bytes
    • 1, 2, 4 (32bit), 8, 16 bytes
    • every variable has a name
  • Use variable name to store and retrieve data
  • Variable type specifies data size

Variables #2

  • Variable naming
    • May contain upper/lowercase letters
    • digits
    • underscore
  • Must not begin with digit
  • Different type of variables store different type of data
#include <stdio.h>

int main(void)
{
    int salary;         // Declare a variable called salary
    salary = 10000;     // Store 10000 in salary
    printf("My salary is %d.\n", salary);
    return 0;
}

Variables #3

  • Variable declaration
    • introduces a variable name
  • Variable definition
    • causes memory to be allocated
  • Explicit/Implicit conversions
    • compiler performs implicit conversions when no data loss
    • explicit conversion needed if data loss possible

Flow Control

If Statement

if(a > b)
    printf('a is greater b');

if(a < b)
    printf('b is greater a');
else 
    printf('a is greater b');


if(a < b) 
{
    if(a < z) 
    {
        // ...
    }
    else 
    {
        // ...
    }
} 
else if(z < b) 
{
    // ...
}
else 
{
    // ...
}

Switch Statement

switch(customerId)
{
    case 35:
        printf('Customer id is 35');
        break;
    case 36:
        // ...
        break;
    default: 
        // ...
        break;
}

Loops

For Loop

for(int i = 0; i < 10; i++)
{
    // ...
}

int count = 0;
for( ; count < 10; ++count)
{
    // ...
}

for( ;; )
{
   // ...
   if(quitPrg)
        break;
}
  • i is defined once
  • loop expression is evaluated in each iteration
  • variable i is incremented in each iteration

While Loop

int i = 0;
int count = 10;
while(i < count)
{
   //...
   i++;
}

for( ;; ) 
{
    // ...
    while(true)
    {
       // ...
    }
    // ...
}

Arrays

Arrays

#include <stdio.h>         // # marks a preprocessing directive 
  • Preprocessing directive tells the compiler to do something before comping
  •  Include header file into
  • Header file provides info about types and functions in the stdlib
  • Compiler uses info to integrate predefined functions or other objects to the app
  • E.g. info about printf(..)

 

 

References

 

Beginning C (5th Edition)

Ivor Horton

 

 

 

 

 

Thank you for your attention!

 

Made with Slides.com