CHAPTER – 1 : INTRODUCTION TO C
C is a high level language. It is both general purpose and specific purpose programming
language. Now a days, C has become a common programming language for every application
developer. It was developed at Bell & T Laboratory, USA in 1972. It is the outcome of the efforts of
Dennis Ritchie and Brian Kernighan.
C is derived from two early programming languages such as BCPL (Basic Combined
Programming Language) and B Language. The BCPL was developed by Martin Richards. And, B
was developed by Ken Thompson.
In 1972, Dennis Ritchie developed a new version of B and named it as C. He selected the
name C for his new language because C comes after B in the alphabetical order which indicates
advancement to B. Some sources also say that Dennis Ritchie selected the second character of BCPL
for naming his language.
In 1978, Dennis Ritchie and Brian Kernighan jointly published a detailed description of the C
Language document. It is known as “K & R C”. It was released to commercial applications in 1978.
CHARACTERISTICS OF C
C has become popular programming language because of its many features. The important
characteristics of C are :
C is a general purpose programming language
C is a structured programming language
Helps in development of System Software
It has rich set of operators
It provides compact representation for expressions
It allows manipulation of internal processor registers
No rigid format. Any number of statements can be typed in a single line.
Portability : any C program can be run on different machines with little or not modification.
Supports a rich set of data types
Very less number of reserved words
Pointer arithmetic and pointer manipulation
Ability to extend itself by adding functions to its library
APPLICATIONS OF C
Because of its portability and efficiency, C is used to develop the system as well as
application software. Some of the system and application software are listed below.
System Software
1] Operating Systems 2] Interpreters 3] Compilers 4] Assemblers
5] Editors 6] Loaders 7] Linkers
Application Software
1] Data Base Systems 2] Graphic Packages 3] Spread Sheets
4] CAD/CAM Application 5] Word Processors 6] Office Automation Tools
7] Scientific and Engineering Applications
CHARACTER SET
Every programming language has its own set of characters to form the lexical elements. The
characters used in C are grouped into the following three categories.
1] Alphabets : Upper Case A-Z
Lower Case a-z
2] Digits : 0 to 9
Page No. 3
3] Special Characters :
, comma + plus sign
. dot - minu sign
: colon * asterisk
; semicolon / slash
‘ opostrophe % percentage
“ quotation mark & ampersand
? question mark ^ caret
! exclamation mark ~ tilde
_ underscore < less than
# hash > greater than
= equal sign \ back slash
| pipeline character ( left parenthesis
) right parenthesis
[ left bracket
] right bracket
{ left brace
} right brace
BASIC STRUCTURE OF A C PROGRAM :
Every programming languages have their own format of coding. The complete structure of C
program as shown below.
preprocessor statements
global declarations;
main( )
{
declaration;
statements;
/* comments */
}
user defined function
The basic components of a C program are :
1] Preprocessor Statements :
These statements begin with # symbol and also called preprocessor directives. These
statements direct the C preprocessor to include header files and also symbolic constants into a C
program. Some of the preprocessor statements are :
# include <stdio.h>
# include <conion.h> etc.,
2] Global Declaration :
Variables or functions whose existence is known in the main( ) function and other user
defined functions, are called the global variables and their declarations are called the global
declarations. This declaration should be made before main( ) function.
3] main( ) function :
This is the main function of every C program. Execution of C program starts from main(). No
C program is executed without main() function. It should be written in lowercase letters and should
not be terminated by a semicolon. It calls other library functions and user defined functions.
Page No. 4
4] Braces :
Every C program uses a p[air of curly braces i.e., { and }. The left brace indicates the
beginning of main( ) function. On the other hand, the right indicates the end of the main( ) function.
The braces can also be used to indicate the beginning and end of user-defined functions and
compound statements.
5] Declaration :
It is a part of the C program where all the variables, arrays, functions etc., used in the C
program are declared and may be initialized with their basic data types.
6] Statements :
These are instructions to the computer to perform specific operations. They may be input-
output statements, arithmetic statements, control statements and other statements.
7] Comments :
Comments are explanatory note on some instructions. The statements to be commented on
must be enclosed with /* and */. Comment statements are not compiled and executed.
8] User-defined functions :
These are subprograms. Generally, a subprogram is a function. The user defined functions
contain a set of statements to perform a specific task. These are written by the user, hence the name
user-defined functions. They may be written before or after the main( ) function.
A Simple C Program is given below
#include<stdio.h>
main()
{
printf(“\nWelcome to C”);
\* a call to output function *\
}
The first line tells the compiler to include standard input/output header file to perform
reading and printing of the data. The second line is the main( ), the main function of a C program.
The body of C program contains the statement i.e.,
printf(“\nWelcome to C”);
When this statement is taken for execution, main( ) calls the output function printf( ). Then
printf( ) prints Welcome to C on the computer screen.”\n” is a new line character. It is used to print
the message to the new line. The statement i.e., \* a call to output function *\ is comment. Comment
describes the above statement.
Program Compilation and Execution
Compiling a C program means translating it into machine language. ‘C’ compilers are used
for this purpose. The process of generating outputs of the program on the screen is called Execution.
Press Ctrl + F9 to compile the program. Press Alt + F5 to run the program.
Starting C Program Editor
1] Click Start menu & Select Run option
2] Type command & Press Enter key
3] Press Alt+Enter key to view DOS in Full Screen
4] Type cd\tc\bin & Press Enter Key
5] Type tc & Press Enter Key
Page No. 5
C TOKENS
The basic and the smallest units of a C program are called C Tokens. There are six types of
tokens in C.
1] Keywords 2] Identifiers
3] Constants 4] String
5] Operators 6] Special Symbols
KEYWORDS
Every word in a C program is either a keyword or an identifier. All keywords (reserve words)
are basically the sequence of characters that have one or more fixed meaning. All C keywords must
be written in lowercase letters. Because, in C both uppercase and lowercase letters are significant.
Example for Keywords:
int Char break else if continue
do float goto long return for
short static auto union struct switch
IDENTIFIERS
Identifiers are names given to the program elements such as variables, arrays and functions.
Basically, identifiers are the sequences of alphabets and digits.
Rules for forming identifier name
The first character must be an alphabet (upper case or lowercase) or an underscore ( _ )
All succeeding characters must be either letters or digits
Uppercase and lower case identifiers are different in C
No special character or punctuation symbols are allowed except the underscore ( _ )
No two successive underscores are allowed
Keywords should not be used as identifiers.
CONSTANTS :
A Constant is an entity whose value does not change during program execution. The
following are types of constants :
1] Integer Constant :
An integer constant is a whole number. It is a sequence of digits without a decimal point. It
may prefixed with plus or minus sign.
Example : 246, 0, -3579, +25, -32028, 9999 etc.,
2] Floating Constant :
A floating constant is a number with a decimal point. It is defined as sequence of digits
preceded and followed by the decimal point. They may have prefixed plus or minus sign.
Example : -246.01, +12.25, 0.0, 0.0005, 82.0, 9999.999, -0.00123 etc.,
3] Character Constant :
A character constant is a single character enclosed within pair of apostrophes.
Example : ‘a’, ‘?’, ‘#’, ‘ ‘(blank character) etc.,
4] String Constant :
A string constant is a sequence of characters enclosed within a pair of double quotes.
Example : “Hi”, “Welcome”, “2007”, “x+2”etc.,
Page No. 6
VARIABLES
A variable is an entity used by a program to store values in the program. It is a symbolic
name used for actual memory location. Therefore variable are also called as identifiers.
Example : sum, area, num, length, age, city etc.,
Rules for forming variable names
The first character of a variable name must be an alphabet or an underscore
All succeeding characters consists of letters and digits
Both uppercase and lowercase variables are significant in C
Keywords should be used as variables
Special characters are not allowed
There is not limit on the number of characters in a variable name
Always choose an appropriate variable name that makes proper sense to the user
DECLARATION OF VARIABLES
All the variables must be declared before they are used in the program. A variable declaring
consists of a data type name followed by a list of one or more variables of that type, separated by
commas.
Syntax :
Datatype variable_name,…..,….;
Example :
int num;
char name[10];
float rate;
ASSIGNING VALUES TO VARIABLES
We know that the variables represent some memory location, where the data is stored. Each
variable is associated with one or more values. The process of giving values to variables is called
assignment of values. The assignment operator ‘=’ is used to assign a value to a variable.
Syntax :
variable_name = value;
Example :
int num=5;
float pi=3.142;
x = 10;
sum = a + b;
name = “Saraswati”
DATA TYPES
Data type indicate the type of data that a variable can hold. The data may be numeric or non-
numeric in nature. There are four fundamental C data types
Types Keyword Size(in bytes)
Integers int 2 bytes
Real(Floating point) float 4 bytes
Double Precision double 8 bytes
Character char 1 byte
Page No. 7
int :
This is keyword used to indicate an integer number. Any integer number is a sequence of
digits without a decimal point. A 8 bit computer the maximum integer that can be input is either -
128 or +127. Similarly, a 16 bit computer handles the integers from -32,768 to +32,767.
Example :
-248, 14042, 27246, +1996, 0, 32760
float :
This is a keyword used to indicate a floating-point number. The floating-point numbers are
same as real numbers. They are called floating point. These numbers may be expressed in scientific
notation.
Example :
-263.238, 2.63238E+02, 0.0263238E+04, 26323.802
char :
This is a keyword used to indicate the character type data. The data may be a character
constant or a string constant. A character constant can be defined as any single character enclosed
within a pair of apostrophes.
Example :
‘a’, ‘p’, ‘$’, ‘2’, ‘?’, ‘ ‘(blank) etc.,
double :
This is a keyword used to indicate a double precision floating point numbers. The precision is
associated with the accuracy of data. The float usually stores a maximum or 6 digits after the
decimal point. But double stored 16 significant digits after the decimal point.
Example : 234.0000000000000000, -0.0000001023999001
Page No. 8