An Introduction to C

    Surabhi Saxena
    Share

    Introduction

    C is a general purpose, structured programming language. Its instructions consist of terms that resemble algebraic expressions, augmented by certain English keywords such as if, else, for, do and while. In this respect it resembles high level structured programming languages such as Pascal and Fortran. C also contains additional features, that allow it to be used at a lower level, thus bridging the gap between machine language and high level language. This flexibility allows C to be used for systems programming as well as for applications programming. Therefore C is called a middle level language.  

    C is characterized by the ability to write very concise source programs, due in part to the large number of operators included within the language. It has a relatively small instruction set, though actual implementations include extensive library functions which enhance the basic instructions. C encourages users to create their own library fuctions.

    An important characteristic of C is that its programs are highly portable. The reason for this is that C relegates most computer dependent features to its library functions. Thus, every version of C is accompanied by its own set of library functions which are relatively standardized. Therefore most C programs can be processed on many different computers with little or no alteration.

    History of C:

    C was developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories,Inc. (now a part of AT&T). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell Laboratories.

    The Combined Programming Language(CPL) was developed at Cambridge University in 1963 with the goal of developing a common programming language which can be used to solve different types of problems on various hardware platforms. However it turned out to be too complex, hard to learn and difficult to implement. Subsequently in 1967, a subset of CPL, Basic CPL(BCPL) was developed by Martin Richards incorporating only the essential features. However it was not found to be sufficiently powerful. Around the same time another subset of CPL, a language called B was developed by Ken Thompson at Bell Labs. However it also turned out to be insufficient . Then, in 1972, Dennis Ritchie at Bell Labs developed the C language incorporating the best features of both BCPL and B.

    C was largely confined to use within Bell Labs until 1978, when Brian Kernighan and Ritchie published a definitive description of the language . The Kerninghan and Ritchie description of C is commonly referred to as ‘K &R C’.

    Following the publication of ‘K&R C’,computer professionals, impressed with C’s many desirable features, began to promote the use of C. By the mid 1980’s the popularity of C had become widespread-many c compilers and interpreters had been written for computers of all sizes and many commercial application programs had been developed. Moreover, many commercial software products that had originally been written in other languages were rewritten in C in order to take advantage of its efficiency and portability.

    Early commercial implementations of C differed a little from Kerninghan and Ritchie’s original description, resulting in minor incompatibilities between different implementations. As a result, the American National Standards Institute(ANSI committee X3J11)  developed a standardized definition of C. Virtually all commercial compilers and interpreters adhere to the ANSI standard. Many provide additional features of their own.

    C and Systems Programming:

    There are several features of C, which make it suitable for systems programming. They are as follows:

    • C is a machine independent and highly portable language.
    • It is easy to learn; it has only 28 keywords.
    • It has a comprehensive set of operators to tackle business as well as scientific applications with ease.
    • Users can create their own functions and add to the C library to perform a variety of tasks.
    • C language allows the manipulation of bits, bytes and addresses.
    • It has a large library of functions.
    • C operates on the same data types as the computer, so the codes generated are fast and efficient.

    Structure of a C Program:

    Every C program consists of one or more modules called functions. One of the functions must be called main. The program will always begin by executing the main function, which may access other functions. The main function is normally,but not necessarily located at the beginning of the program. The group of statements within main( ) are executed sequentially. When the closing brace of main( ) is encountered, program execution stops and control is returned to the operating system.

    Any other function defintions must be defined separately, either ahead or after main( ). Each function must contain:

    1. A function heading, which consists of the function name, followed by an optional list of arguments, enclosed in parantheses.

    2. A return type written before the function name. It denotes the type of data that the function will return to the program.

    3. A list of argument declarations, if arguments are included in the heading.

    4. A compound statement, which comprises the remainder of the function.

    The arguments(also called parameters) are symbols that represent information being passed between the function and other parts of the program.

    Each compound statement is enclosed between a pair of braces{ }. The braces may contain one or more elementary statements (called expression statements) and other compound statements. Thus compound statements may be nested one within another. Each expression statement must end with a semicolon(;).

    Comments (remarks) may appear anywhere within a program as long as they are enclosed within the delimiters /* and */. Comments are used for documentation and are useful in identifying the program’s principal features or in explaining the underlying logic of various program features.

    Components of C Language:

    There are five main components of the C Language:-

    1. The character set: C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9 and certain special characters as building blocks to form basic program elements(e. g. constants, variables, expressions, statements etc. ).

    2. Data Types: The C language is designed to handle five primary data types, namely, character, integer, float, double and void; and secondary data types like array, pointer, structure, union and enum.

    3. Constants: A constant is a fixed value entity that does not change its value throughout program execution.

    4. Variables: A variable is an entity whose value can change during program execution. They are used for storing input data or to store values generated as a result of processing.

    5. Keywords: Keywords are reserved words which have been assigned specific meanings in the C language. Keywords cannot be used as variable names.

    The components of C language will be discussed in greater detail in the following articles. This section gives only a brief introduction to the components of C.

    Example 1: The following program reads in the radius of a circle, calculates the area and then prints the result.

    /* program to calculate the area of a circle*/
    
    #include<stdio.h> /*Library file access*/
    
    #include<conio.h> /*Library file access*/
    
    void main( )            /* Function Heading*/
    
        {
    
           float radius, area; /*Variable declarations*/
    
           /*Output Statement(prompt)*/
    
           printf("Enter the radius :");
    
          /*Input Statement*/
    
          scanf("%f", &radius);
    
          /*Assignment Statement*/
    
          area = 3.14159*radius*radius;
    
          /*Output Statement*/
    
          printf("Area of the circle :%f", area);
    
           getch( );
    
         }

    Program output:-

    Enter the radius: 3

    Area of the circle: 28. 27431

    The following points must be considered to understand the above program:-

    1. The program is typed in lowercase. C is case sensitive i. e. uppercase and lowercase characters are not equivalent in C. It is customary to type C instructions in lowercase. Comments and messages(such as those printed using printf() ) can be typed in anycase.

    2. The first line is a comment that identifies the purpose of the program.

    3. The instruction #include <stdio.h> contains a reference to a special file called stdio. h . This file contains the definition of certain functions required to read and print data such as printf() and scanf() . It is a header file and hence the extension . h.

    4. Similarly #include <conio.h> links the file conio. h which is another header file that contains the definitions of functions used for reading and printing data at the console. The function getch() is defined in conio. h. # denotes a preprocessor directive. More about this in a later article.

    5. The instruction void main() is a heading for the function main( ). The keyword void denotes the return type of main and indicates that the function does not return any value to the program after the program has finished executing. The empty parantheses ( ) after main indicates that this function does not include any arguments. Program execution always begins from main( ).

    6. The remaining five lines of the program are indented and enclosed in a pair of braces { }. These five lines comprise the compound statement within the function main( ).

    7. The instruction float radius, area;  is a variable declaration. It establishes the symbolic names ‘radius’ and ‘area’ as floating point variables. These variables can accept values of type ‘float ‘ i. e numbers containing a decimal point or an exponent.

    8. The next four instructions are  expression statements. The instruction printf(“Enter the radius :”); generates a request for information namely,the value for the radius. This statement generates a prompt where the user enters the value .

    9. The value of the radius is read into (or stored in) the variable radius with the help of the scanf ( ) function. The instruction scanf(“%f”, &radius); is used for reading data.   “%f” is a conversion character which is used to accept a floating point value.

    10. The next instruction, area = 3.14159*radius*radius; is called an  assignment statement. This instruction calculates the area by using the value of radius entered by the user and assigns the value to the variable area.

    11. The next printf( ) statement prints the message Area of the circle followed by the calculated area.

    12. The statement getch(); is used to pause the screen so that you can read the output. If getch( ) is not used the screen will just flash and go away. This function waits for the user to input some character(as it accepts a character as input), after the program has finished executing. Any key present on the keyboard pressed by the user is accepted by the getch function as input and its ASCII value is returned to main( ).

    Example2: Below is a variation of the above program:

    /*program to calculate the area of a circle using a user defined function*/
    
    #include <stdio.h>
    
    #include <conio.h>
    
    #define PI 3.14159
    
    float process(float radius);/*function prototype*/
    
    void main()
    
        {
    
          float area,radius;
    
          printf("n Enter the radius:");
    
          scanf("%f", &radius);
    
          area= process(radius);
    
          printf("Area =%f", area);
    
          getch();
    
         }
    
    float process( float r)
    
         {
    
         float a; /*local variable declaration*/
    
         a= PI*r*r;
         return(a);
    
         }

    This version utilizes a separate programmer defined function called process, to calculate the area. Within this function, r is an argument (also called a parameter) that accepts the value of radius supplied to  process from main, and a is the calculated result returned to main. A reference to the function appears in main( ), within the statement area= process(radius);

    In this statement, the value of area being returned from the function process is stored in the variable area.

    The main function is preceeded by a function prototype, which indicates that there is a user defined function called process which is defined after main and that it accepts a floating point argument and returns a floating point value. If the user defined function process, was defined before main( ), the function prototype would,generally, have not been required.

    More explanation about this when I write about functions in a later article.

    This program also contains a symbolic constant, PI, which represents the numeric value 3. 14159. This is a form of shorthand that exists for the programmers convenience. When the program is actually compiled, the symbolic constant will automatically be replaced by its numerical value. The output of this program is the same as that of the previous program.

    This article was a brief introduction, it gives an idea of C programming. The next article will talk about the fundamental concepts of C which include the C character set, Identifiers and keywords, data types in detail, constants,variables, variable declarations, expressions, statements and symbolic constants .