C – Basic Introduction

C – Basic Introduction

C is a general-purpose high-level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972.

The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.

  • Easy to learn
  • Structured language
  • produces efficient programs.
  • can handle low-level activities.
  • It can be compiled on a variety of computers.

Facts about C

  • It was invented to write an operating system called UNIX.
  • C is a successor of B language which was introduced around 1970.
  • The language was formalized in 1988 by the American National Standard Institute (ANSI).
  • By 1973 UNIX OS almost totally written in C.
  • Today C is the most widely used System Programming Language.
  • Most of the state of the art software has been implemented using C.

Some examples of the use of C might be:

  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • DataBases
  • Language Interpreters
  • Utilities

History of C Language

In late 1960s Ken Thompson and Dennis Ritchie developed, a language called B. Earlier version of UNIX uses B programming language. It inherits many of its features from BCPL (Basic Combined Programming Language).

Later in the early 1970s need for a portable operating system and programming language originated. Rather developing programs and operating system for a particular machine. The need for a portable operating system came into existence. This was the period when the development of the C programming language started.

During 1969 to 1972, Dennis Ritchie at Bell Laboratories developed C. In 1972, the first release of C programming got officially public. C inherited many features from ALGOL, BCPL, and B. It is such a powerful language that UNIX operating system is almost completely written in C.

In 1978, it gained huge popularity from the Bell laboratories to the heart of every programmer. Brain Kernighan along with Dennis Ritchie published the first book on C “The C Programming Language”. The book is popularly also known as “K&R C book”.

K&R introduced several language features:

  • Standard I/O library
  • long int datatype
  • unsigned int datatype
  • Compound assignment operators of the form =op (such as =-) were changed to the form op= (that is, -=) to remove the semantic ambiguity created by constructs such as i=-10, which had been interpreted as i =- 10 (decrement i by 10) instead of the possibly intended i = -10 (let i be -10).

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 others.
  • 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 be used repeatedly in different application
    • C program basically collection of function
    • The function is supported by ‘c’ library
    • Function can be added to the ‘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’ lies in its in-built function
    • Unix system provides a large number of C function
    • Some function is used in operation.
    • Other is for specialized in their application

C program structure

pre-processor directives
global declarations

main()
{
    local variable deceleration
    statement sequences
    function invoking
}

The components of the above structure are:

Header Files Inclusion:

The first and foremost component is the inclusion of the Header files in a C program.
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. Some of C Header files:

  • stddef.h – Defines several useful types and macros.
  • stdint.h – Defines exact width integer types.
  • stdio.h – Defines core input and output functions
  • stdlib.h – Defines numeric conversion functions, pseudo-random network generator, memory allocation
  • string.h – Defines string handling functions
  • math.h – Defines common mathematical functions

Syntax to include a header file in C:

#include <(header_file_name).h>
    1. Main Method Declaration: The next part of a C program is to declare the main() function. The syntax to declare the main function is: Syntax to Declare the main method:
      int main()
      {}
      
    2. Variable Declaration: The next part of any C program is the variable declaration. It refers to the variables that are to be used in the function. Please note that in the C program, no variable can be used without being declared. Also in a C program, the variables are to be declared before any operation in the function.Example:
      int main()
      {
          int a;
      }
    3. Body: Body of a function in the C program, refers to the operations that are performed in the functions. It can be anything like manipulations, searching, sorting, printing, etc. Example:
      int main()
      {
          int a;
      
          printf("%d", a);
      }
    4. Return Statement: The last part of any C program is the return statement. The return statement refers to the returning of the values from a function. This return statement and return value depend upon the return type of the function. For example, if the return type is void, then there will be no return statement. In any other case, there will be a return statement and the return value will be of the type of the specified return type.Example:
      int main()
      {
          int a;
      
          printf("%d", a);
      
          return 0;
      }

Writing the first program:

Following is the first program in C

 

#include <stdio.h>
int main(void)
{
    printf("GeeksQuiz");
    return 0;
}

Let us analyze the program line by line.
Line 1: [ #include <stdio.h> ] In a C program, all lines that start with are processed by preprocessor which is a program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program. The produced program has no lines starting with #, all such lines are processed by the preprocessor. In the above example, preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called header files in C. These header files generally contain the declaration of functions. We need stdio.h for the function print() used in the program.

Line 2 [ int main(void) ] There must be a starting point from where the execution of the compiled C program begins. In C, the execution typically begins with the first line of main(). The void written in brackets indicates that the main doesn’t take any parameter. main() can be written to take parameters also. We will be covering that in future posts.
The int is written before main indicates return type of main(). The value returned by main indicates the status of program termination.

Line 3 and 6: [ { and } ] In C language, a pair of curly brackets define the scope and mainly used in functions and control statements like if, else, loops. All functions must start and end with curly brackets.

Line 4 [ printf(“GeeksQuiz”); ]  printf() is a standard library function to print something on standard output. The semicolon at the end of printf indicates line termination. In C, the semicolon is always used to indicate the end of a statement.

Line 5 [ return 0; ] The return statement returns the value from main(). The returned value may be used by the operating system to know the termination status of your program. The value 0 typically means successful termination.

  1. How to execute the above program:
    In order to execute the above program, we need to have a compiler to compile and run our programs.
  2. Windows: There are many compilers available freely for the compilation of C programs like Code Blocks and Dev CPP.   We strongly recommend Code Blocks.
  3. Linux: For Linux, GCC comes bundled with the Linux,  Code Blocks can also be used with Linux.

2 Comments

Add a Comment
  1. C++ kb tk aa jayegi Mam

Leave a Reply

Your email address will not be published. Required fields are marked *