Introduction

C was invented and first implemented by Denis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the result of a development process that started with an older language called BCPL. BCPL was developed by Martin Richards, and it influenced a language called B, which was invented by Ken Thompson. B led to the development of C in the 1970s.

C is often called a middle-level computer language. This is because it combines the best elements of high-level languages with the control and flexibility of assembly language. As a middle-level language, C allows the manipulation of bits, bytes, and addresses--the basic elements with which the computer functions.

C is a compiled language. This means the raw C code first must be converted into raw intructions, i.e. the machine code, before it can be run by the computer. A general build process is like this:

  • A compiler reads the entire C program and converts it into object code
  • Then, a linker takes this object code and links it with its dependencies (standard library and other user-defined functions) to create a final executable file.

What you should already know

This guide assumes that you have the following basic background:

  • a basic understanding of computer
  • know how to use a text editor
  • woring understanding with comman-line interface

C and C++

Many people confuse the C with C++. Originally, C++ was build on top of C and added my features to the C language by Bjarne Stroupe. One of the most notable feature was support for Obejct-Oreintation with the class keyword. For this reason, C++ was initially called as the "C with classes".

However, mordern C++ and C are completely different languages in many respects. C++ is still backward compatible with C (C89 standard), the new c++14 and above is now mostly used which can feel like a completely different programming language to a C veteran.

C was designed Originally for system programming, and its simplicity and strength made it suitable for almost anything. On the other hand, was designed to provide C with Object-Orientation support. C is much simple and light weight language as compared to C++.

It would be more helpful to think of them as very closely related languages, but different.

Hello world
To get started with C, open any text editor and write your first code:

    #include <stdio.h>
    
    int main() 
    {
        printf("Hello, world\n");
        
        return 0;
    }

                
Now, save it as hello.c. Open the terminal, and compile and run:

    $ gcc -o hello hello.c
    $ ./hello
    Hello, world
    $

                
Variables

A variable is a named location in memory that is used to hold a value that can be modified by the program. That is, a variable is a human-readable form of physical computer address. The names of variables is called identifier---which are completely used-defined.

The length of these identifiers can vary from one to several characters. The first character must be a letter or an underscore, and subsequent characters must be either letters, digits or underscores. Here are some examples:

Correct Incorrect
count 1count
test23 hi!there
high_balance high...balance

Declaring variables

In C, all variables must be declare before they can be used. The general form of a declaration is

   type variable_list;
Here, type must be a valid data type plus any modifier, and variable_list may consist of one or more identifier names separated by commas. Here are some declarations:

    int i, j, l;
    short int si;
    unsigned int ui;
    double balance, profit, loss;

                    

Remember, in C the name of a variable has nothig to do with its type.

Variable scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

Variables delared in local scope are not accessible outside that function.


    void func1(void)
    {
        int x = 10;
    }

    /* this will produce an error,
     * because no variable
     * x exist in this scope */
    printf("%d", x);

                    

Global variables

Unlike local variables, global variables are known thoughout the program and may be used by any piece of code. Also, they will hold their value thoughout the program's execution.

To declare global variable, declare variables outside of any logical block, ie outside of function, while, for, etc.

Constants

Constants refer to fixed values that the program may not alter. Unlike normal variables, constants are read-only variables. And therefore, their value cannot be changed after their definition. A constant variable can have any data type.

You can create constant variables by using cosnt keyword.


    const double PI = 3.14156;

                    
By convention, constant types are named with all uppercase letters.

Data types

C89 defines five foundational data types:

  • character ---- declared using char keyword
  • integer ---- declared using int keyword
  • floating-point ---- declared using float keyword
  • double floating-point ---- declared using double keyword
  • valueless ---- declare using void keyword

C99 adds three more: _Bool, _Complex, _Imaginary.

if...else statement

You can use if...else statements to program logical execution that will be executed only if some certain condition(s) are met. Syntax:


    if (condition)
    {
    statement(s)
    }
    else
    {
        statement(s)
    }

                    
if condition evaluates to true, the first statements will be executed, otherwise on false, second statements are executed.

You can have multiple branches which each execute when a certain condition is met using if...else if...else statement.


    if (condition)
    {
    statement(s)
    }
    else if (condition)
    {
        statement(s)
    }
    /* any number of else if */
    else
    {
        statement(s)
    }

                    
The trailing else block is executed if all of the conditions evalutes to false.

while statement

A while statement executes its statements as long as a specified condition evaluates to true. Syntax:

 
    while (condition)
    {
        statement(s)
    }

                    
If the condition evaluates to false then the execution of the while loop stops, and the control flow passes to the statement following the while loop.

An example to print first 20 integers on the screen:


    int i = 0;
    while (i < 20)
    {
        printf("%d\n", i+1);
        i++;
    }

                    
With each iteration the loop increments the i adding 1 to it. When i = 20, the mentioned condition evaluates to false and the loop stops.

Function declaration

The general form of a function is


 ret-type function-name (parameter list)
 {
   body of the function
 }

                    
  • ret-type specifies the type of data that the function returns
  • parameter list(optional) is a comma-separated list of variable names and theier assosiated types that are passed to the body of the funtion when the function is invoked
  • function body contains all the statement that the function will execute on invocation

A function to return a square of number:


    double square (double n)
    {
        return n * n;
    }

                    

Reference
Index