Article

EDP

C & DS

Engg.Chemistry

Engg.Physics

Introduction to ‘C’ Language

  • D. Vijay Kumar
    Asst. Prof., Dept IT
    MGIT, Hyd

    Background and History

         C language facilitates a very efficient approach to the development and implementation of computer programs. The history of C started in 1972 at the Bell Laboratories, USA where Dennis M. Ritchie proposed this language. In 1983 the American National Standards Institute (ANSI) established committee whose goal was to produce “an unambiguous and machine independent definition of the language C" while still retaining it’s spirit.

         C is the programming language most frequently associated with UNIX. Since the 1970s, the bulk of the UNIX operating system and its applications have been written in C. Because the C language does not directly rely on any specific hardware architecture, UNIX was one of the first portable operating systems. In other words, the majority of the code that makes up UNIX does not know and does not care which computer it is actually running on. Machine-specific features are isolated in a few modules within the UNIX kernel, which makes it easy for you to modify them when you are porting to a different hardware architecture.
         C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11 computers. The language evolved from Martin Richard's BCPL and one of its earlier forms was the B language, which was written by Ken Thompson for the DEC PDP-7. The first book on C was The C Programming Language by Brian Kernighan and Dennis Ritchie, published in 1978.
         In 1983, the American National Standards Institute (ANSI) established a committee to standardize the definition of C. The resulting standard is known as ANSI C and it is the recognized standard for the language, grammar, and a core set of libraries. The syntax is slightly different from the original C language which is frequently called K&R for Kernighan and Ritchie. There is also an ISO (International Standards Organization) standard that is very similar to the ANSI standard.
         It appears that there will be yet another ANSI C standard officially dated 1999 or in the early 2000 years it is currently known as "C9X."

    Simple ‘C’ Program
              /* simple program in c */
              #include<stdio.h>
              main()
              {
              printf(“welcome to c programming”); }
              /* End of main */
    C-Tokens
              
    Tokens are individual words and punctuations marks in English language sentence. The smallest individual units are known as C tokens.

              A C program can be divided into these tokens. A C program contains minimum 3 C tokens no matter what the size of the program is.
    C Language Components
              The four main components of C language are
    1) The Character Set
    2) Tokens
    3) Variables
    4) Data Types
    The Character Set
              Character set is a set of valid characters that a language can recognize. A character represents any letter, digit or any other sign.

    Letters

    A,B,C……Z or a,b,c …………z.

    Digits

    0,1…………9

    Special Symbols

    ~!@#$%^&.........

    White Spaces

    Blank space, horizontal tab, carriage return,new line, form feed.

    Tokens
              The smallest individual unit in a program is known as a token. C has five tokens
    i. Keywords
    ii. Identifiers
    iii. Constants
    iv. String Constants
    v. Special Symbols
    vi. Operators
    i. Keywords: Keywords are reserved word in C. They are predefined put meaning cannot be changed. All keywords must be written in lowercase. Eg:- auto,long,char,short etc.
    ii. Identifiers: Identifiers refer to the names of variable, functions and arrays. These are user-defined names. An identifier in C can be made up of letters, digits and underscore. Identifiers may start with either alphabets or underscore. The underscore is used to make the identifiers easy to read and mark functions or library members.
    iii. Constants: Constants in C refers to fixed values that do not change during the execution of a program. C support several types of constants.
    a. Numerical Constants
    i. Integer Constant
    1. Decimal Constant
    2. Octal Constant
    3. Hexadecimal Constant
    ii. Float Constant
    b. Character Constants
    ii Single Character Constant
    iii String Constant
    Integer Constant: An integer constant is a whole number without any fractional part. C has three types of integer constants.
    Decimal Constant: Decimal integers consists of digits from 0 through 9 Eg.: 34,900,3457,-978
    Octal Constant: An Octal integer constant can be any combination of digits from 0 through 7. In C the first digit of an octal number must be a zero(0) so as to differentiate it from a decimal number. Eg.: 06,034,-07564
    Hexadecimal Constant: Hexadecimal integer constants can be any combination of digits 0 through 9 and alphabets from ‘a’ through ‘f’ or ‘A’ through ‘F’. In C, a hexadecimal constant must begin with 0x or 0X (zero x) so as to differentiate it from a decimal number. Eg:-0x50,0XAC2 etc
    Floating Constants (Real): Real or floating point numbers can contain both an integer part and a fractional part in the number. Floating point numbers may be represented in two forms, either in the fractional form or in the exponent form. A float point number in fractional form consists of signed or unsigned digits including decimal point between digits. E.g:- 18.5, 18 etc.
              Very large and very small numbers are represented using the exponent form. The exponent notation use the ‘E’ or ‘e’ symbol in the representation of the number. The number before the ‘E’ is called as mantissa and the number after forms the exponent. Eg.:
    String Constant: A sequence of character enclosed between double quotes is called string constant. Eg.: “Hello Good Morning”
    Single Character Constant: A character constant in usually a single character or any symbol enclosed by apostrophes or single quotes. Eg.: ch=’a’
    iv) String Constants
    A sequence of character enclosed between double quotes is called string constant. Eg.: “Hello Good Morning”
    v) Special Symbols
    23 characters are used as punctuations in C. eg: + _ / ; : > ! etc
    vi)Operators
    An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated as operands. C operator can be classified into 8 types.
    i. Arithmetic Operators : + - * / %
    ii. Assignment Operators : =
    iii. Relational Operators: < > <= >= == !=
    iv. Logical Operators:! && ||
    v. Conditional Operators: ! :
    vi. Increment & Decrement Operator : ++ --
    vii. Bitwise Operator:! & | ~ ^ << >>
    viii. Special Operator : sizeof ,(comma)
    Variables
    A variable is an object or element that may take on any value or a specified type. Variable are nothing but identifiers, which are used to identify variables programming elements. Eg: name, sum, stu_name, acc_no etc.
    Data types
    Data types indicate the types of data a variable can have. A data types usually define a set of values, which can be stored in the variable along with the operations that may be performed on those values. C includes two types of data.
    * Simple or Fundamental data type
    * Derived data type
    Simple Data Types: There are four simple data types in C.
    * int
    * char
    * float
    * double
    int:- This means that the variable is an integer are stored in 2 bytes, may range from -32768 to 32767.
    char:- This means that the variable is a character type, char objects are stored in one byte. If unsigned, the values may be in the range 0 to 255.
    Float:- This means that the variable is a real number stored in 4 bytes or 32 bits. The range of floating point values are between or 6 significant digits after the decimal point.
    Double 8 1.7E +/- 308(15 Digits)
    Long Double 10 3.4E - 4932 to 1.1E + 4932
    Double:- This means that the variable is a double precision float type. In most cases the systems allocates 8 bytes or 64 bits space, between .
    The following table shows the Storage size and Range of basic data types:

    Derived Data Types: Derived data types are constructed from the simple data types and or other derived data types. Derived data include arrays, functions, pointers, references, constants, structures, unions and enumerations.
    User Defined Datatypes: C Provides a facility called typedef for creating new data type names defined by the user. For Example ,the declaration ,
    typedef int Integer
    makes the name Integer a synonym of int. Now the type Integer can be used in declarations ,casts,etc,like,
    Integer num1,num2
    Which will be treated by the C compiler as the declaration of num1,num2as int variables. “typedef” ia more useful with structures and pointers.
    Pointer Data Types: Pointer data type is necessary to store the address of a variable.
    Variables : A quantity that can vary during the execution of a program is known as a variable. To identify a quantity we name the variable for example if we are calculating a sum of two numbers we will name the variable that will hold the value of sum of two numbers as 'sum'.
    Identifiers : Names of the variables and other program elements such as functions, array etc. are known as identifiers.
    There are few rules that govern the way variable are named(identifiers).
    Identifiers can be named from the combination of A-Z, a-z, 0-9, _(Underscore).
    1. The first alphabet of the identifier should be either an alphabet or an underscore. digits are not allowed.
    It should not be a keyword.
    Eg: name, ptr, sum
    After naming a variable we need to declare it to compiler of what data type it is. The format of declaring a variable is
    Data-type id1, id2,.....idn;
    where data type could be float, int, char or any of the data types.
    id1, id2, id3 are the names of variable we use. In case of single variable no commas are required.
    eg: float a, b, c;
    int e, f, grand total;
    char present_or_absent;
    Operators : An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. They form expressions.
    C operators can be classified as
    * Arithmetic operators
    * Relational operators
    * Logical operators
    * Assignment operators
    * Increment or Decrement operators
    * Conditional operator
    * Bit wise operators
    * Special operators
    1. Arithmetic Operators : All basic arithmetic operators are present in C. operator meaning
           +           add
           -           subtract
           *           multiplication
            /           division
           %           modulo division(remainder)
           An arithmetic operation involving only real operands(or integer operands) is called real arithmetic(or integer arithmetic). If a combination of arithmetic and real is called mixed mode arithmetic.
    2. Relational Operators : We often compare two quantities and depending on their relation take certain decisions for that comparison we use relational operators.
           operator              meaning
               <                  is less than
               >                  is greater than
               <=                is less than or equal to
               >=                is greater than or equal to is equal to
               !=                 is not equal to It is the form of
    ae-1 relational operator ae-2
    3. Logical Operators : An expression of this kind which combines two or more relational expressions is termed as a logical expressions or a compound relational expression. The operators and truth values are
          op-1             op-2             op-1 &&       op-2 op-1 || op-2
        non-zero       non-zero             1                   1
        non-zero             0                 0                   1
             0            non-zero             0                   1
             0                  0                 0                   0
           op-1             !op-1
        non-zero           zero
          zero            non-zero
    4. Assignment Operators : They are used to assign the result of an expression to a variable. The assignment operator is '='.
         Vop = exp
         V is variable
         op binary operator
         exp expression
         op= short hand assignment operator
    short hand assignment operators
    use of simple assignment operators use of short hand assignment operators
              a=a+1                      a+=1
              a=a-1                      a-=1
              a=a%b                     a%=b
    5. Increment And Decrement Operators : And == are called increment and decrement operators used to add or Substract. Both are unary and as follows
    ++m or m++ --m or m--
    The difference between ++m and m++ is
    if m=5; y=++m then it is equal to m=5;m++;y=m; if m=5; y=m++ then it is equal to m=5;y=m;m++;
    6. Conditional Operator : A ternary operator pair "?:" is available in C to construct conditional expressions of the form
    exp1 ? exp2 : exp3;
    It work as
    if exp1 is true then exp2 else exp3
    7. Bit Wise Operators : C supports special operators known as bit wise operators for
    manipulation of data at bit level. They are not applied to float or double.
              operator           meaning
                  &                Bitwise AND
                  |                Bitwise OR
                  ^                Bitwise exclusive OR
                  <                left shift
                  >                right shift
                  ~                one's complement
    8. Special Operators : These operators which do not fit in any of the above classification are ,(comma), sizeof, Pointer operators(& and *) and member selection operators (. and ->). The comma operator is used to link related expressions together. sizeof operator is used to know the sizeof operand.
    Expressions :
            An expression is a sequence of operands and operators that reduces to a single value. Expression can be simple or complex. An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed. A simple expression contains only one operator.E.g: 2 + 3 is a simple expression whose value is 5.A complex expression contains more that one operator. E.g: 2 + 3 * 2.
    Precedence of Operators:
            Precedence is defined as the order in which the operators in a complex evaluation are evaluated.
    Associativity:
            When two or more operators have the same precedence then the concept of associativity comes into discussion.
    Type Conversion
           When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general, the only automatic conversions are those that convert a "narrower'' operand into a "wider'' one without losing information, such as converting an integer into floating point in an expression like f + i. Expressions that don't make sense, like using a float as a subscript, are disallowed. Expressions that might lose information, like assigning a longer integer type to a shorter, or a floating-point type to an integer, may draw a warning, but they are not illegal.
           A char is just a small integer, so chars may be freely used in arithmetic expressions. This permits considerable flexibility in certain kinds of character transformations. One is exemplified by this naive implementation of the function atoi, which converts a string of digits into its numeric equivalent.
           Implicit arithmetic conversions work much as expected. In general, if an operator like + or * that takes two operands (a binary operator) has operands of different types, the "lower'' type is promoted to the "higher'' type before the operation proceeds. The result is of the integer type. If there are no unsigned operands, however, the following informal set of rules will suffice:
    * If either operand is long double, convert the other to long double.
    * Otherwise, if either operand is double, convert the other to double.
    * Otherwise, if either operand is float, convert the other to float.
    * Otherwise, convert char and short to int.
    * Then, if either operand is long, convert the other to long
    Input and Output Statements
           The simplest of input operator is getchar to read a single character from the input device. varname=getchar();
            you need to declare varname.
           The simplest of output operator is putchar to output a single character on the output device.
           putchar(varname)
    The getchar() is used only for one input and is not formatted. Formatted input refers to an input data that has been arranged in a particular format, for that we have scanf.
           scanf("control string", arg1, arg2,...argn);
    Control string specifies field format in which data is to be entered.
    arg1, arg2... argn specifies address of location or variable where data is stored.
           eg scanf("%d%d",&a,&b);
           %d used for integers
           %f floats
           %l long
           %c character for formatted output you use printf
           printf ("control string", arg1, arg2,...argn);
           /* program to exhibit i/o */
           #include<stdio.h>
           main()
           {
           int a,b; float c;
           printf("Enter any number"); a=getchar();
           printf("the char is ");
           putchar(a);
           printf("Exhibiting the use of scanf");
           printf("Enter three numbers");
           scanf("%d%d%f",&a,&b,&c); printf("%d%d%f",a,b,c);
    Statements And Blocks
           A statement causes an action to be performed by the program. It translates directly in to one or more executable computer instructions.
    Statement Types:
    1.Null Statement
           The null statement is just a semicolon (the terminator).
                  Eg:        //null statement
    Although they do not arise often, there are syntactical situations where we must have a statement but no action is required .In these situations we use the null statement.
    2.Expression Statement
           An expression is turned in to a statement by placing a semicolon(;)after it.
                  expression;        //expression statement
                           Eg: a = 2;
    3.Return Statement
           A return statement terminates a function. All functions ,including main, must have a return statement. Where there is no return statement at the end of the function ,the system inserts one with a void return value.
                  return expression;               //return statement
    The return statement can return a value to the calling function. In case of main ,it returns a value to the operating system rather than to another function. A return value of zero tells the operating system that the program executed successfully.
    4.Compound Statements
           A compound statement is a unit of code consisting of zero or more statements .It is also known as a block. The compound statement allows a group of statements to become one single entity.
           A compound statement consists of an opening brace ,an optional declaration and definition section ,and an optional statement section ,followed by a closing brace.
           Eg:
                  {
                         //Local Declarations int x;
                         int y; int z;
                         //Statements
                         x=1;
                         y=2;
                         
                  } //End Block
    Conditional Statements
           Conditional statements control the sequence of statement execution, depending on the value of a integer expression.
    1) If Statement
           a) Simple If statement
           + If-else statement
           + if-else if statement
           + Nested if statement
    & Switch Statement
    1)If Statement:
           i) Simple if
                   Syntax:
                            if ( <integer expression> )
                            <statement list>
           ii) If-else statement
                   Syntax:
                            if ( <integer expression> )
                             { <statement list>
                             } else {
                             <statement list>
                             }
           iii) if-else if statement
                   Syntax:
                            if ( <integer expression> )
                             { <statement list>
                             } else if ( <integer expression> ) {
                             <statement list>
                             . . .
                             } else if ( <integer expression> ) {
                             <statement list>
                             } else {
                             <statement list>
                             }
           iv) Nested if statement
                            if ( <integer expression> )
                            { if(<integer
                            expression>{ <statement list> }
                            else{<statement
                            list>}
                            } else { if(<integer expression>{ <statement
                             list> }else{<statement list>}
                            }
    Switch statement
           The switch statement provides a multi-way selection control structure to execute a selected section of code determined by the run-time value of an expression. The condition is an expression which evaluates to an integer value, which include signed, unsigned, char, enum, and even boolean values.
                   Syntax:    switch ( <expression> ) {
                                 case this-value:
                                           Code to execute if <expression> == this-value
                                 break;
                                 case that-value:
                                           Code to execute if <expression> == that-value
                                 break;
                                 ...
                                 default:
                                 Code to execute if <expression> does not equal the value                                     following any of the cases
                                 break;
                                 }

    Sample program:
                   #include <stdio.h>
                   main()
                   {
                   int menu, numb1, numb2, total;
                   printf("enter in two numbers -->");
                   scanf("%d %d", &numb1, &numb2 );
                   printf("enter in choice\n");
                   printf("1=addition\n");
                   printf("2=subtraction\n");
                   scanf("%d", &menu );
                   switch( menu ) {
                                 case 1: total = numb1 + numb2;
                                 break;case 2: total = numb1 - numb2;
                                 break; default: printf("Invalid option selected\n");
    }
                   if( menu == 1 )
                   printf("%d plus %d is %d\n", numb1, numb2, total ); else if( menu == 2 )
                   printf("%d minus %d is %d\n", numb1, numb2, total );
    }
    Sample Program Output:
    enter in two numbers --> 37 23 enter in choice
    1=addition
    2=subtraction
    2
    37 minus 23 is 14
    Looping Statements
           Loops provide a way to repeat a set of statements and control how many times they are repeated.
           C supports three looping statements. They are while, do-while and for.
           Both while and for statements are called as entry-controlled loops because they evaluate the expression and based on the value of the expression they transfer the control of the program to a particular set of statements.
           Do-while is an example of exit-controlled loop as the body of the loop will be executed once and then the expression is evaluated. If it is non-zero value then the body of the loop will be executed once again until the expression's value becomes zero.
           Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
    While loop
           This is an entry controlled looping statement. It is used to repeat a block of statements until the expression evaluates to nonzero value
           Syntax:
           while(expression)
                   { statements;
                   increment/decrement;
           }
    Example program for while:
           #include<stdio.h>
           main()
           {
                  int i,times;
                  scanf("%d",
                  &times);
                  i = 0;
                  while (i <= times)
                  {
                         i++;
                       printf("%d\n",i);
                  }
                     return 0;
           }
    OUTPUT:
           3
           1
           2
           3
    For loop
           This is an entry controlled looping statement.
           In this loop structure, more than one variable can be initialized. One of the most important features of this loop is that the three actions can be taken at a time like variable initialization, condition checking and increment/decrement. The for loop can be more concise and flexible than that of while and do-while loops.
           Syntax:
                  for(initialization; test-condition; increment/decrement)
                  {
                  statements;
                  }
           In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.
    Do-While loop
           This is an exit controlled looping statement.
           Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In this, block of statements are executed first and then condition is checked.
           Syntax:
                  do
                  {
                         statements;
                         (increment/decrement); }
                         while (expression);
           In above syntax, the first the block of statements are executed. At the end of loop, while statement is executed. If the expression is evaluated to nonzero value then program control goes to evaluate the body of a loop once again. This process continues till expression evaluates to a zero. When it evaluates to zero, then the loop terminates.
    Break and continue statements
           C break statement is used to terminate any type of loop such as while loop, do while loop and for loop. C break statement terminates the loop body immediately and passes control to the next statement after the loop.
           C continue statement is used to skip over the rest of the current iteration. After continue statement, the control returns to the top of the loop.
    Example program for continue:
           main()
            {
                  int i;
                int j = 10;
                 for( i = 0; i <= j; i ++ )
                  {
                  if( i == 5 )
                  {
                  continue;
            }
                 printf("Hello %d\n", i );
                 }
            }
    OUTPUT:
            Hello 0
            Hello 1
            Hello 2
            Hello 3
            Hello 4
            Hello 6
            Hello 7
            Hello 8
            Hello 9
            Hello 10
    Goto statement
            A goto statement is used to branch (transfer control) to another location in a program.
            Syntax: goto label;
            The label can be any valid identifier name and it should be included in the program followed by a colon.