Programming in C
Human Language: A system of sounds and words used by humans to express their thoughts and
feelings.(or) A tool for communication of ideas, thoughts and intentions.
E.g.: English, Telugu, Tamil etc.
Computer Language: A system of words, symbols and rules used in writing a program. (or) A tool to
instruct a computer for asking the solution of a given problem.
E.g.: C, C++, Java etc.
Introduction to C:
C is a general purpose structured programming language. It was developed by ‘Dennis Ritchie’ in the
year 1972, at AT&T’s Bell Telephone Laboratories of U.S.A. It is an outcome of two earlier
programming languages called BCPL (Basic Combined Programming Language) developed by ‘Martin
Richards’ at Cambridge University in 1967, and ‘B’ developed by ‘Ken Thompson’ at AT&T’s
Laboratories in 1970. Ritchie inherited the best features of BCPL and B, added some of his own and
called it as ‘C’. Ritchie’s main achievement is the restoration of the lost generality in BCPL and B, and
still keeping it powerful.
Characteristics of C:
a) C programs are efficient and fast, because of its variety of data types and powerful operators.
b) C is compact. This means that the statements in ‘C’ are short, simple, easy to write, but are very
powerful.
c) C is highly portable. This means that C programs written for one computer can be run on another
computer with a little or no modification.
d) C supports a large number of library functions, so the programs can be quickly developed.
e) C language can interact directly with the computer hardware.
f) C language recognizes the difference between lower case and upper case letters. All the statements
are entered in lower case letters and symbolic names and output strings are entered in upper case
letters.
g) C is a free-form language, i.e., C has no specific rules for the position at which a statement is to be
written.
h) Every C statement must end with a semicolon. Thus semicolon acts as a statement terminator.
The C Character Set:
C language supports the following characters that can be used in a program.
(i) Alphabet Upper Case A to Z and Lower Case a to z.
(ii) Digits 0 1 2 3 4 5 6 7 8 9
(iii) Special Characters + - * / = ( ) { } [ ] < > ‘ “ ! # % & | ^ ~ _ \ . , ; : ? @
, 2
C-Tokens
In a C program, the smallest individual units are known as C tokens. C programs are written
using these tokens and the syntax of the language.
C TOKENS
Keywords Identifiers Constants Punctuators Operators
a) Keywords:
These are explicitly reserved words that have a strict meaning as individual tokens in C. They
cannot be redefined or used in other contexts.
There are 32 keywords in C. All these keywords must be written in lower-case. They are:
Keywords
auto do goto signed unsigned
break double if sizeof void
case else int static volatilew
char enum long struct while
const extern register switch
continue float return typedef
default for short union
Some implementations may have additional keys. These will vary from one implementation or
system to another.
E.g.: Turbo C compiler supports the following keywords.
asm cdel far huge interrupt near pascal
b) Identifiers:
They are meant for identifying programming elements such as variables, functions, arrays,
strings, unions etc. which are defined by a programmer.
Rules for naming identifiers:
(i) Identifiers can be created with the combination of upper case letters (A to Z), lower case letters
(a to z), digits (0 to 9) and underscore symbol only.
(ii) A digit cannot be the first character of identifiers.
E.g.: 1stclass, 10th_rank, 123nos are invalid identifiers.
(iii) ANSI C allows maximum 31 character length identifiers.
(iv) Keywords should not be used as identifiers.
, 3
c) Constants:
Constants in C refer to fixed values that do not change during the execution of a program.
Constants can be classified into the following types:
(i) integer constant
(ii) real constant
(iii) character constant
(iv) string constant
(i) integer constant: These constants are sequence of digits without any fractional part. There are three
types of integer constants. They are:
Decimal Integers
Octal Integers
Hexa Decimal Integers
Decimal Integers: These integers consist of a set of digits, 0 through 9.
Rules for constructing a decimal constant:
i) A decimal constant must have at least one digit.
ii) It must not have a decimal point.
iii) It could be either positive or negative.
iv) If no sign precedes an integer constant, it is assumed to be positive.
v) No commas or blanks are allowed within an integer constant.
vi) The allowable range for integer constants is -32768 to +32767.
E.g.: 1234, -2356 are valid decimal integer constants.
[Note: The range of an integer constant depends upon the compiler.]
Octal Integers: An octal integer constant consists of a set of digits, 0 through 7, with a leading zero.
E.g.: 037, 0435 are valid octal constants.
Hexa decimal Integers: A sequence of digits preceded by 0X or 0x is considered as hexadecimal
integer. They may also include alphabet A through F, or a through f. The letters A through F represent
the numbers 10 through 15.
E.g.: 0X2, 0X9F, 0Xbcd are valid hexadecimal constants.
(ii) real constant (floating point constant): These constants are sequence of digits with fractional part.
These constants could be written in two forms – fractional form and exponential form.
Rules for constructing a real constant in fractional form:
a) A real constant must have at least one digit.
b) It must have a decimal point.
c) It could be either positive or negative.
d) Default sign is positive.
e) No commas or blanks are allowed within a real constant.
, 4
f) It is possible to omit digits before the decimal point or digits after the decimal point.
E.g.: 215. , .95 , -.71, +.5 are valid real constants.
Exponential form:
In exponential form of representation, the real constant is represented in two parts separated by e.
The part appearing before ‘e’ is called mantissa, the part following ‘e’ is called exponent.
Rules for constructing a real constant in exponential form:
a) The mantissa part and the exponential part should be separated by a letter e or E.
b) The mantissa part may have a +ve or –ve sign.
c) Default sign of mantissa part is +ve.
d) The exponent must have at least one digit, which must be a +ve or –ve integer. Default sign is
positive.
e) Range of real constants expressed in exponential form is -3.4e38 to +3.4e38
E.g.: +3.2e-5, - 0.2E+3 are valid real constants.
(iii) character constant: A character constant is either a single alphabet, a single digit, or a single
special symbol enclosed within single inverted commas. The maximum length of a character constant
can be 1 character.
E.g.: `A`, `5`, `; `, ` ` are valid character constants.
(iv) string constant: A string constant consists of any number of consecutive characters enclosed in
double quotation marks. The characters may be letters, numbers, special characters and blank space. The
compiler collects a string constant as a single token.
E.g.: “welcome”, “5”, “ “ are valid string constants.
d) Punctuators:
In C, there are certain characters with particular meanings. They are called punctuators. These
punctuators serve to separate language elements.
E.g.: { , } , ( , ) , ;
e) Operators:
An operator is a symbol, which tells the computer to perform certain manipulations. In C,
operators are used to manipulate data and variables. C supports, a rich set of operators. They are:
(i) Arithmetic Operators
(ii) Relational Operators
(iii) Logical Operators
(iv) Assignment Operators
(v) Bitwise Operators
(vi) Special Operators