top of page
Introduction - C Programming

1. What is C?

  • C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 by Dennis Ritchie.

  • Any programming Language can be divided in to two categories.

    • Problem oriented (High level language)

    • Machine oriented (Low level language)

But C is considered as a Middle level Language.

  • C is modular, portable, reusable.

2. Feature of C Program

  • Structured language

    • It has the ability to divide and hide all the information and instruction.

    • Code can be partitioned in C using functions or code block.

    • C is a well structured language compare to other.

  • General purpose language

    • Make it ideal language for system programming.

    • It can also be used for business and scientific application.

    • ANSI established a standard for c in 1983.

    • The ability of c is to manipulate bits,byte and addresses.

    • It is adopted in later 1990.

  • Portability

    • Portability is the ability to port or use the software written .

    • One computer C program can be reused.

    • By modification or no modification.

  • Code Re-usability & Ability to customize and extend

    • A programmer can easily create his own function

    • It can can be used repeatedly in different application

    • C program basically collection of function

    • The function are supported by 'c' library

    • Function can be added to 'c' library continuously

  • Limited Number of Key Word

    • There are only 32 keywords in 'C'

    • 27 keywords are given by ritchie

    • 5 is added by ANSI

    • The strength of 'C' is lies in its in-built function

    • Unix system provides as large number of C function

    • Some function are used in operation .

    • Other are for specialized in their application

3. C program structure

pre-processor directives 

global declarations

 

main() 

{

    local variable deceleration

    statement sequences

    function invoking

}

4. C Keywords

Keywords are the words whose meaning has already been explained to the C compiler. There are only 32 keywords available in C. The keywords are also called ‘Reserved words’.

auto        double      int         struct 

break       else        long        switch 

case        enum        register    typedef 

char        extern      return      union 

const       float       short       unsigned 

continue    for         signed      void 

default     goto        sizeof      volatile 

do          if          static      while 

5. C Character Set

A character denotes any alphabet, digit or special symbol used to represent information. Following are the valid alphabets, numbers and special symbols allowed in C. 
* Alphabets - A, B, ….., Y, Z a, b, ……, y, z * Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 * Special symbols - ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " ' < > , . ? /

6. Rules for Writing, Compiling and Executing the C program

  • C is case sensitive means variable named "COUNTER" is different from a variable named "counter".

  • All keywords are lowercased.

  • Keywords cannot be used for any other purpose (like variable names).

  • Every C statement must end with a ;. Thus ;acts as a statement terminator.

  • First character must be an alphabet or underscore, no special symbol other than an underscore, no commas or blank spaces are allowed with in a variable, constant or keyword.

  • Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.

  • Variable must be declared before it is used in the program.

  • File should be have the extension .c

  • Program need to be compiled before execution.

7. Data types & Placeholders

  • C has 5 basic built-in data types.

  • Data type defines a set of values that a variable can store along with a set of operations that can be performed on it.

  • A variable takes different values at different times.

  • General form for declaring a variable is: type name;

  • An example for using variables comes below:

#include<stdio.h>

main() 

{

    int sum;

    sum=12;

    sum=sum+5;

    printf("Sum is %d",sum);

}

printf function will print the following: Sum is 17 In fact %d is the placeholder for integer variable value that its name comes after double quotes. 
* Common data types are: * int - integer * char - character * long - long integer * float - float number * double - long float * Other placeholders are:

Placeholders        Format 

%c                  Character

%d                  Signed decimal integer

%i                  Signed decimal integer

%e                  Scientific notation[e]

%E                  Scientific notation[E]

%f                  Decimal floating point

%o                  unsigned octal

%s                  String of character

%u                  unsigned decimal integer

%x                  unsigned Hexadecimal (lower)

%X                  unsigned Hexadecimal (upper)

%p                  dispaly a pointer

%%                  print a %

8. Control characters (Escape sequences)

Certain non printing characters as well as the backslash () and the apostrophe('), can be expressed in terms of escape sequence.

  • \a - Bell

  • \n - New line

  • \r - Carriage return

  • \b - Backspace

  • \f - Formfeed

  • \t - Horizontal tab

  • \" - Quotation mark

  • \v - Vertical tab

  • \' - Apostrophe

  • \\ - Backslash

  • \? - Question mark

  • \0 - Null

9. Receiving input values from keyboard

scanf function used to receiving input from keyboard. 
General form of scanf function is :

scanf("Format string",&variable,&variable,...);

Format string contains placeholders for variables that we intend to receive from keyboard. A & sign comes before each variable name that comes in variable listing. Character strings are exceptions from this rule. They will not come with this sign before them.

Note: You are not allowed to insert any additional characters in format string other than placeholders and some special characters. Entering even a space or other undesired character will cause your program to work incorrectly and the results will be unexpected. So make sure you just insert placeholder characters in scanf format string. The following example receives multiple variables from keyboard.

float a; 

int n; 

scanf("%d%f",&n,&a); 

Pay attention that scanf function has no error checking capabilities built in it. Programmer is responsible for validating input data (type, range etc.) and preventing errors

The Decision Control Structure - C Programming

C has three major decision making instructions—the if statement, the if-else statement, and the switch statement.

1. The if Statement

C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:

//for single statement

if(condition) 

    statement;

 

//for multiple statement

if(condition) 

{

    block of statement;

}

The more general form is as follows:

//for single statement

if(expression) 

    statement;

 

//for multiple statement

if(expression) 

{

    block of statement;

}

Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the ifstatement. For example all the following if statements are valid

if (3 + 2 % 5) 

    printf("This works");

The expression (3 + 2 % 5) evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf("This works"); gets executed.

2. The if-else Statement

The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements ifthe expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as

if (expression) 

{

    block of statement;

}

else 

    statement;

Note * The group of statements after the if upto and not including the else is called an if block. Similarly,the statements after the else form the else block. * Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right. * Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces. * As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above "Multiple Statements within if" must be used.

3. Nested if-elses

If we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called "nesting" of ifs. This is demonstrated as -

if (expression1) 

    statement;

else 

{

    if (expression2)

        statement; 

    else

    {

        block of statement;

    }

}

4. The if-else Ladder/else-if Clause

The else-if is the most general way of writing a multi-way decision.

if(expression1) 

    statement;

else if(expression2) 

    statement;

else if(expression3) 

    statement;

else if(expression4) 

{

    block of statement;

}

else 

    statement;

The expressions are evaluated in order; if an expressionis true, the "statement" or "block of statement" associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces. The last else part handles the "none of the above" or default case where none of the other conditions is satisfied.

5. Switch Statements or Control Statements

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. The switch statement that allows us to make a decision from the number of choices is called a switch, or more correctlya switch-case-default, since these three keywords go together to make up the switch statement.

switch (expression) 

{

    case constant-expression:

        statement1;

        statement2;

        break;

    case constant-expression:

        statement;

        break;

    ...

    default:

        statement;

}

  • In switch…case command, each case acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one of casesections or labels from where the execution of the program will continue. The program will continue execution until it reaches break command.

  • break statements have vital rule in switch structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end of switch block will be executed (while most of the time we just want one case section to be run).

  • default section will be executed if none of the case sections match switch comparison.

6. switch Versus if-else Ladder

There are some things that you simply cannot do with a switch. 
These are:

  • A float expression cannot be tested using a switch

  • Cases can never have variable expressions (for example it is wrong to say case a +3 :)

  • Multiple cases cannot use same expressions.

bottom of page