C

C

  • A computer language
  • Initially developed between 1969 and 1973
  • Came after language B (but there was no A!)
  • By Dennis Ritchie at Bell Labs
  • Initially used to rewrite the Unix operating system

C

  • General purpose
  • Compiled
  • Imperative
  • Statically typed

C data structures and constructs map efficiently to machine language instructions and memory mapping

C

  • Code is freeform, i.e. whitespace is generally irrelevant and ignored
  • Scope is determined by curly braces: { }
  • There must be a main() function which serves as the point of entry to the program
  • Although variables can be outside functions, all execution occurs inside functions
  • All statements end with ;
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello World!\n");
    return 0;
}

Compiling and linking

  • The C compiler takes C code (a text file) and turns it into binary executable code
  • All code can be written in a single file
  • But the linker can bundle together multiple binary files (called object files) into a single one.
  • The linker can also let a C binary use existing dynamic libraries by connecting them to the executable without bundling them.

Compiling

COMPILING

  • Compilers are command line programs (i.e. no GUI)
  • So options are passed as text commands to the program
  • Each compiler has different options, configuration and name
  • On my system it can look like:
$ gcc hello.c -o hello
$ ./hello
Hello World!
$

Build Systems

  • As programming projects get more complicated using command line building directly becomes very clumsy
  • Build systems are tools to assist the building of sources
  • Using a build system means describing what and how you want to build your software and the build system will do it for you

Build Systems

  • Makefile
  • CMake
  • Scons
  • Waf
  • many more...

Build systems are generally command line programs that read a "configuration" file in a particular format

IDE

  • Integrated Development Environment
  • Provide editing, compiling, and debugging functionality
  • IDE projects serve as build systems
  • You will often want to use an IDE as they reduce the knowledge needed to use the compilers and build systems directly
  • Most functionality is exposed in the GUI, which makes it discoverable

IDE Workflow

  • Create a project
  • Write the source code (starting from the boiler plate code provided)
  • Build Project
    • Fix syntax and compilation errors if needed
  • Run Project
    • Fix runtime errors or undesired behavior and rebuild
    • Add new functionality and rebuild

Libraries in C

  • The C standard library provides a lot of functionality:
    • printing and I/O (stdio.h)
    • math functions (math.h)
    • String manipulation (string.h)
    • Date and time handling functions (time.h)

To use the standard library you often only need to include the headers:

#include <stdio.h>
#include <math.h>

Libraries in C

  • There are thousands of libraries out there providing additional functionality for the C language
  • You don't want to reinvent the wheel, so when possible use libraries
  • To use libraries:
    • Install them (what?)
    • "Include" the headers in the code
    • Tell the linker to link to the the compiled libraries

What exactly these steps mean depends on your setup, e.g. how you are handling libraries, your operating system and you IDE.

Version Control

  • AKA Revision Control or Source Control
  • Manages changes to source files
  • Allowing:
    • sharing and resolving conflicts
    • reverting to previous states
    • branching (to test functionality without affecting the main code)
    • review code revisions (to know if someone has introduced or fixed an error or added a feature)

Version Control

  • GIT
  • SVN
  • CVS
  • Bazaar

Many websites like sourceforge.net, github.com or bitbucket.com offer free code hosting under version control

And Now C....

Comments

  • Start with /* and end with */
  • Can span more than one line
  • Many compilers will accept the C++ comment style using //
/* A C style
   multiline comment
*/
// A C++ style single line comment

Variables

  • Must be declared with a type
  • Are only available in their scope or children scopes
  • Can only be assigned and compared with the same type (or a type that can be converted to it)
int a;
int b = 10;
{
    a = 12;
    float c = a + b;
}
/* variable c is not available anymore  */

Types

  • C provides only a few built-in types:
    • integers: int, short, long, long long
      • integers are "signed" by default, i.e. they have a sign and can be positive or negative
      • but they can be forced to be unsigned to increase their range of positive values
    • "real" numbers: float, double, long double
    • characters: char
      • Are actually a form of integer! (One byte integers)

Special Operators

  • ++, --: Increment or decrement by one
  • +=, -=, /=, *=: Operate and store
int a = 1;
a++; // a is now 2
a += 1; // Equivalent to a++, a is now 3
a /= 2; // What is a now?

Functions

  • Have a return type and 0 or more input arguments
  • Have to have a unique name
  • Can be declared before they are implemented
#include <stdio.h>

/* Function declarations */
int function1(int value1);
int function2(int value1);

int main(void)
{
    int value = function1(5) + function2(10);
    printf("%i\n", value); 
    return 0;
}

/* Function implementations */
int function1(int value1)
{
    return value1 + 10;
}

int function2(int value1)
{
    return value1 + 20;
}

If Statement

  • Evaluated expression in parenthesis
  • Then new scope with { }
  • Ifs can be nested and chained with else
  • && and || represent the logical and and or operators
if (value > 10) {
    /* if true */
} else if (value < 10) {
    /* < 10 */
} else {
    /* value == 10 */
}

For Loops

  • Perform three operations:
    • Initialization
    • Check value before iteration (Loop condition)
    • Loop operation (Done after each iteration)
for(int i = 0; i < 10; i++) {
    printf("%i\n", i);
}

While Loop

  • Like the for loop, but with only the condition check
int i = 0;

while (i < 10) {
    print("%i\n", i);
    i++;
}
int i = 0;

while (i < 10) {
    print("%i\n", i++);
}

Pointers

  • The hardest thing about C
  • Pointers store memory addresses for a specific type
  • Pointers must be "dereferenced" to get the contents of the address
  • The dereference operator is *
  • You can get the address of a variable using &
int a = 10;
int *b;
b = &a; // b is a pointer of type int *
*b = 5; // now a == 5 too

Common Pitfalls

  • Forgetting the semicolon
  • Forgetting the declaration type of a variable
  • Issues with scope closing (failing to close, close to much)
Made with Slides.com