Introduction C-Programming Language
History of C-Language
C language is a structure oriented programming language, developed at Bell Laboratories in 1972 by Dennis Ritchie
C language features were derived from earlier language called “B” (Basic Combined Programming Language – BCPL)
C language was invented for implementing UNIX operating system
In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming Language” and commonly known as K&R C
In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”, was completed late 1988.
Features of C-Language
The most important features of C language are as follows.
1. Modularity.
2. Portability.
3. Middle Level.
4. Powerful
5. Efficient Use of Pointers.
6. Flexibility.
Modularity: Ability to breakdown a large module into manageable sub modules called as modularity that is an important feature of structured programming languages.
Advantages:
1. Projects can be completed in time.
2. Debugging will be easier and faster.
Portability:
Department of Computer Science, BVRICE Page 2
Middle Level:
‘C’ is also called as middle level language because programs written in ‘c’ language run at the speeds matching to that of the same programs written in assembly language so ‘c’ language has both the merits of high level and middle level language and because if this feature it is mainly used in developing system software.
Powerful
Provides Wide verity of ‘Data Types‘
Provides Wide verity of ‘Functions’
Provides useful Control & Loop Control Statements
Efficient Use of Pointers:
Pointers have direct access to memory.
C Supports efficient use of pointer.
Flexibility
This feature refers to the programmer’s involvement and control on the language. There are number of reserve words in C Language which help the programmer to take control over the usage of language and modified the structure of program.
Applications of C-Language
C Programming is best known programming language. C Programming is near to machine as well as human so it is called as Middle level Programming Language. C Programming can be used to do verity of tasks such as networking related,OS related.
Application of C Programming are listed below
1. C language is used for creating computer applications 2. Used in writing embeddedsoftware’s.
3. Firmware for various electronics, industrial and communications products which use micro-controllers.
4. It is also used in developing verification software, test code, simulators etc. for various applications and hardware products.
5. For Creating Compiles of different Languages which can take input from other language and convert it into lower level machine dependent language.
Structure of a C-Program
The structure of a C program is a protocol (rules) to the programmer, while writing a C program. The general basic structure of C program is shown in the figure below.
Documentations or Commenting Section
The documentation section consist of a set of comment lines giving the name of the program, the author name and other details, which the programmer would like to use later.
Preprocessor Statements or Header File Section
Department of Computer Science, BVRICE Page 4
Global Declaration Section
The variables are declared before the main ( ) function as well as user defined functions is called global variables. These global variables can be accessed by all the user defined functions including main ( ) function.
The main ( ) function
Each and Every C program should contain only one main ( ). The C program execution starts with main ( ) function. No C program is executed without the main function. The main ( ) function should be written in small (lowercase) letters and it should not be terminated by semicolon.Main ( ) executes user defined program statements, library functions and user defined functions and all these statements should be enclosed within left and right braces.
Local Declarations
The variable declaration is a part of C program and all the variables are used in main ( ) function should be declared in the local declaration section is called local variables.
Program statements
These statements are building blocks of a program. They represent instructions to the computer to perform a specific task (operations). An instruction may contain an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it also includes comments that are enclosed within /* and */ . The comment statements are not compiled and executed and each executable statement should be terminated with semicolon.
User defined functions
These are subprograms, generally, a subprogram is a function and these functions are written by the user are called user defined functions. These functions perform user specific tasks and this also contains set of program statements. They may be written before or after a main () function and called within main () function. This is an optional to the programmer.
Rules to write C program
The following are some of rule to write C programs.
1. All C statements must end with semicolon.
2. C is case sensitive. That is upper case and lower case letter are distinct. 3. A C statement can be written in one or it can split into multiple lines.
5. No restriction in using blank spaces or blank line in program segments. But spaces not allowed within variable, constant or keyword.
6. Comments cannot be nested. For example
/* A simple ‘C’ program, /* it prints a message */ */ is not valid. 7. A comment can be split into more number of lines.
8. The global variables can be declared outside of all functions.
Steps for writing & execution a simple C program There are three steps in developing C program.
1. Creation of Source Program 2. Compilation of program 3. Program execution
Creation of source program:
Create a text file using any word processing software with its name end with a C extension and type a C program in that file. Various C-compilers are available under DOS wizard. Quick C, Microsoft C, Turbo C, ANSI C etc are examples for the C-Compilers.
Compilation of program:
The compilation command under Turbo C
Alt+F9
For example, if file name is “Sample.c”, open the file using Turbo C editor and press Alt+F9 in keyboard. If no errors found it will produce object file i.e., Sample.obj(It is the machine understandable file).
Program execution:
In Turbo C environment, the RUN option will do the execution of program. Ctrl+F9 is short cut key from keyboard to run program. After completing the execution of the C-Program, it generates the desired output to our program with respect to the given input. And the resultant file is Sample.exe.
C-Character Set
Department of Computer Science, BVRICE Page 6
Letters A to Z, a to z.
Digits 0 to 9 decimal digits.
Some Special Symbols as follows.
Symbol Meani
ng Symbol Meaning Symbol Meaning Symbol Meaning
~ Tilde - Minus sign ( Lest
parenthesis ; Semicolon
!
Exclam ation mark
= Equal to
sign ) Right parenthesis < Opening angle bracket # Numbe
r sign { Left brace _ Underscore >
Closing angle bracket
$ Dollar
sign } Right brace + Plus sign ?
Question mark % Percent sign [ Left bracket | Vertical
bar , Comma
^ Caret ] Right
bracket \ Backslash . Period
& Ampers
and : Colon ` Apostrophe / Slash
* Asteris
k "
Quotation mark
Delimiters in C
A delimiter is a unique character or series of characters that indicates the beginning or end of a specific statement, string or function body set.
The following tables explain about the delimiters in C.
Delimiter Purpose
: Useful for labeling
; Statement termination symbol
{ } Justifies the scope of the variable
( ) To define function and function call
[ ] Used for array declaration.
# Preprocessor Directive.