Tuesday, June 5, 2012

Introduction to C

     C is a general purpose high-level language developed by Dennis Ritchie and Brain Kernighan at AT&T Bell Laboratories of USA in 1972. Originally it was designed to run on a PDP-11 under UNIX operating system. C was influenced by two related languages BCPL and B. Although it was originally intended to run under UNIX, there has been a great interest in running it under MS-DOS on IBM-PC.






Many high-level languages like Pascal are highly disciplined and structured. C is much more flexible and free-wheeling. This freedom gives C much more power that experienced users can employ.


C has been a popular language due to its simplicity of expression, the compactness of the code,
extensive use of function calls and the wide range of applicability. C is cryptic in nature. It allows the programmer a wide range of operations from high-level up to low-level.

Structure of a C Program

A, C program basically has the following structure.

  • Pre-processor commands
  • Type definitions
  • Function prototypes
  • Variables
  • Functions




Structure of a Function

             Function name (parameters)
             {
             local variables ;
             statements ;
             }

The Minimum C Program

            main()
            {
             }


Note: Every C program must have one and only one main function. The { } groups the statements
        together.
     

Stages in Creating a C program


  • Designing [will be discussed later]
  • Coding

          You must first create a file which contain the C source code. For this you may use any text
     editor. The file name could be any legal name accepted by the DOS / UNIX and must have an
     extension of C.
     E.g. pro1.c


The source code that you enter must obey the structures, the syntax and semantics of the C language.


  • Compiling

The C compiler receives the source code from the pre-processor and translates it in to
assembly code.


  • Assembling

The assembler creates object code. In MSDOS the object files are seen with .obj extension.


  • Linking

If a source file references library functions defined in other source files, the link editor combines these functions with main() to create an executable file (.EXE)


The Pre-Processor

The pre-processor accepts source code as input and is responsible for
        1. removing comments
        2. Interpreting special pre-processor directives  denoted by #.
                                   







Directives in a Program

A directive is a special statement that can control the features of a compiler.
           E.g. a) #include <stdio.h> provides a convenient way of telling the compiler that all the I/O        functions have been included.                                                                           ;
                  b) #include <math.h> provides the standard math library.
                  c) #define defines a symbolic name or a constant.


Comments in a Program

Any text that you enter between /* and */ is considered to be a comment and is ignored by
the compiler.

White Space Characters

The characters - space, Tab, Carriage Return (CR), Line Feed (LF) and Back Space are

considered to be white space characters and they will be ignored by the compiler.


Statement Terminator

         In C a line of text can contain several statements and a statement can straddle several lines. So the ; tells the compiler how to find the end of a statement. You can also have a ; without having a prior statement where an action is not required.

No ; is required after an #include directive




















No comments:

Post a Comment