Module 1
Module 1
PROGRAMMING IN C 1BEIT105/205
MODULE-1
HISTORY OF C
C is a general purpose, procedural, structured computer programming language developed by
Dennis Ritchie in the year 1972 at AT&T Bell Labs.
C language was developed on UNIX and was invented to write UNIX system software.
C is a successor of B language.
There are different C standards: K&R C std, ANSI C, ISO C.
Characteristics of C:
• C is easy to learn.
• C is a general purpose language.
• C is a structured and procedural language.
• It is portable.
• It can extend itself
Examples of C:
• Operating system
• Language compilers
• Assemblers
• Text editors
• Databases
C Character Set:
A C character set defines the valid characters that can be used in a source program. The basic
C character set are:
1. Letters: Uppercase: A, B, C, ……, Z Lowercase: a, b, c ……, z
2. Digits: 0, 1, 2,….., 9
3. Special characters:! , . # $ ( ,), }, { etc.
4. White spaces: Blank space, Horizontal tab space, carriage return, new line character, form
feed character.
Every c program is made up of one or more pre-processor commands, global declarations, and
one or more functions.
Documentation section :consists of a set of comment line giving the name of the program, the
author, and other details. Compiler ignores these comments when it translates the program into
executable code. C uses 2 different formats
1. Block comments /*this is multi line comments*/
2. Line comments //this is single line comments
The Link section: provides instruction to the compiler to link functions from system library.
This Section is also called as pre-processor Statements.
Global Declaration section: there are some variables that are used in more than on function,
such a variable are called global variable and are declared in the global declaration section that
is outside of all functions. This section also defines user defined functions.
Every C program must have one main () function section. This section contains two parts
declaration part and executable part
Declaration part declares all the variables used in the executable part
There is at least one statement in an executable part. These two part must appear at the
beginning of the brace and ends at the closing brace. All statement in the declaration and
executable part ends with semicolon (;).
The sub program section: contains all the user defined functions that are called in the main
function although they appear in any order.
Here is a small program that displays a sentence “Welcome to C Programming for Problem
solving” on the monitor screen:
/* C program to display a welcome message */
#include<stdio.h>
void main( )
{
printf(“Welcome to C Programming for Problem solving”);
}
C Tokens: In C program the smallest logically meaning full individual units are known as c
tokens. These are also called as the basic building blocks of C program which cannot be further
broken into subparts. C has 6 Different types of tokens. C programs are written using these
tokens and syntax of the language.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
vi. Special symbols
1. Keywords: These are predefined words in C compiler which are ment for specific purpose.
These words are also called as reserved words. These words cannot be used as variable names.
These words are usually case sensitive and are usually written in lower case letters only. There
are 32 keywords in C.
2. Identifiers: These are the names given to various elements of the C program like variables,
functions, arrays, etc. These are user defined names and consist of sequence letters, digits or
underscore.
Rules to define an Identifiers
1. The first character of the identifier must always be a letter or an underscore followed by any
number of letters digits or underscore.
2. Keywords cannot be used as identifiers or variables.
3. An Identifier or a variable should not contain two consecutive underscores
4. Whitespaces and special symbols cannot be used to name the identifiers.
5. Identifiers are case sensitive (A same variable name declared in uppercase letters and lower
case letters are two different variables in C program).
Examples
food_court valid identifier
$num Invalid identifier ($ is a special symbol)
3. Constants: These are the fixed values assigned to the variables which cannot be cannot be
changed or modified in the program. Constants are broadly classified as
Numeric Constants:
Integer Constant: These contain digits or whole numbers without decimal point which can be
either positive or negative.
(i) Decimal: It is an integer constant consisting of numbers from 0-9. It can be preceded by + or –
(ii) Octal: It is an integer constant consisting of numbers from 0-7. It is preceded by o
(iii) Hexadecimal: It is an integer constant consisting of numbers from 0-9, A-F (A=10, B=11,
C=12, D=13, E=14, F=15). It is preceded by 0x
Real Constant: These contain an decimal point or an exponent or both. It can be either positive
or negative or both.
Example: 21.5, 3.142, 6.6260X10-34 , 2.15X102 → 2.15e2
Character Constants:
Single Character Constant: can be single character enclosed within single quotes or a ‘\’
(backslash) followed by any character. ‘\’ is called escape character as it alters the meaning of
character following it. Following are the complete list of escape sequence.
String Constant: String constants also termed as string literal are sequences of characters
enclosed in double quotes. The character may be letters, numbers, special characters and blank
space. a String literal always ends with a Null character (‘\0’)
Example:
M I T E ‘\0’
5. Operators: An operator is a symbol that tells the compiler to perform specific mathematical
and logical functions. The different operators supported in ‘C’ are:
(i) Arithmetic Operators
(ii) Relational Operators
(iii) Logical Operators
(iv) Assignment Operators
(v) Bitwise Operators
(vi) Unary Operators→ Increment and Decrement
(vii) Ternary/ Conditional Operator
(viii) Special Operators
(i) Arithmetic Operators: These operators are used to perform basic arithmetic operations
Operator Name Result Syntax Example (b=5, c=2)
+ Addition Sum a=b+c a=7
- Subtraction Difference a=b-c a=3
* Multiplication Product a=b*c a = 10
/ Division Quotient a=b/c a=2
% Modulus Remainder a=b%c a=1
(ii) Relational Operators: This operator compares two operands inorder to find out the
relation between them. The output will be either 0 (False) or 1 (True).
(iii) Logical Operators: These are used to test more than one condition and make decision.
The different logical operators are:
❖ Logical NOT
❖ Logical AND
❖ Logical OR
❖ Logical NOT (!) The output is true when input is false and vice versa. It accepts only
one input.
Input Output
X !X
0 1
1 0
❖ Logical AND (&&) The output is true only if both inputs are true. It accepts two or
more inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1
❖ Logical OR (||) The output is true only if any of its input is true. It accepts two or
more inputs.
Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1
(iv) Assignment Operators: The assignment operator is used to assign the values to the
variables on the left hand side. The symbol “=” is used as an assignment operator.
Example: x = 10, c = a+b
Shorthand Assignment: An expression can be written in a compact manner i.e. if the operand
on the left hand side of the assignment operator is same as the first operand of the right hand
side expression it can be written using the shorthand assignment operator
Example: x = x+2 → x+=2
Multiple Assignment: If more than one variable holds the same value we can use multiple
assignment to avoid rewriting of the same values repeatedly.
Example: a=10,b=10,c=10 → a=b=c=10
Bitwise Left Shift (<<): Shift specified number of bits to left side
X 0 1 0 0 0 1 1 0
X<<2 0 0 0 1 1 0 0 0
Bitwise Right Shift (>>): Shift specified number of bits to right side.
X 0 1 0 0 0 1 1 0
X>>2 0 0 0 1 0 0 0 1
I. Primary or built-in or primitive data type: These are the data types which are already
predefined by the compiler.
(i) Integer data type: It is used to store whole numbers and its range depends on the word
length defined for a computer. It usually occupies 2 bytes of memory, for signed integers the
value ranges from -2n-1 to +2n-1-1 and for unsigned integers the value ranges from 0 to 2n-1.
Keyword int is used to declare variables of integer data type.
(ii) Floating point data type: It is used to store decimal numbers that have single precision
floating point value. It provides 6 digits after the decimal point and occupies 4 bytes of memory.
Keyword float is used to declare variables of floating point data type.
(iii) Double data type: These are used to store real numbers that have double precision floating
point value. It provides 16 digits after the decimal point. this data type is used when performing
complex calculations to get accurate results. It occupies 8 bytes of memory. Keyword double
is used to store the variables of double data type.
(iv) Char data type: This data type basically stores character type of data. the character data
can be an Alphabet [a to z or A to Z] , digits [0 to 9] and all special characters or
symbols[@,$,&,#,...]which is enclosed with in single quotes. It occupies one byte of memory.
Keyword char is used to declare variables of character data type.
(v) Void data type: It does not store any value hence we cannot store any operation on the
variable declared as void. It has no range. Keyword void is used to specify non return data type.
II. Derived data type: These are the data types which are derived from the primitive data
types. There are mainly three derived data types
(i) Arrays: Sequence of data items having homogeneous values.
(iii) Pointers: These are used to access the memory and deal with their addresses
III. User defined data type: The type definition feature of C allows the user to define an
identifier which acts as data type using an existing basic data type. Such identifier is called as
user defined data types.
(i) Structure: It is a package of variable of different types under a single name. struct keyword
is used to define a structure.
(ii) UNION: This allows storing various data types in the same memory locations.
(iii) ENUM: Enumeration is a special data type that consists of integral constants and each of
them is assigned with a specific name. enum keyword is used to create the enumerated data
type.
Data type Modifiers: Built in data types except void data type can easily be modified by using
data type modifier. There are mainly 4 data type modifiers:
(i) Signed (ii) Unsigned (iii) Long (iv) Short
(i) Signed: it indicates that the variable is capable of storing the negative numbers. The values
will be in this range. -2n-1 to +2n-1-1. Where, n is the size of the particular data type in bits. In
declaration we have to use signed keyword. Example: signed int a;
(ii) Unsigned: it indicates that the variable is capable of storing only positive numbers The
values will be in this range. 0 to 2n-1. Where, n is the size of the particular data type in bits. In
declaration we have to use unsigned keyword. Example: unsigned int a;
(iii) Long: It is used to increase the storage capacity of the variable. long keyword can be used
as shown long int a; //long int occupies 4 bytes of memory.
(iv) Short: It is used to decrease the storage capacity of the variable (capacity is reduced to
half). short keyword can be used as shown short int a; //short int occupies 1 bytes of memory.
Format Specifiers: These are used to tell the compiler about the type of data being used
Data Type Format Specifier Meaning
%d Decimal integer
%o Octal integer
%x Hexadecimal integer
Integer (int)
%i Decimal, hex or octal int
%u Unsigned integer
%h Short integer
Floating Point %e
floating point
(float) %f
%g
%c Single character
Character (char)
%s String data
Double (double) %lf Floating point number or double
Long Integer %ld long integer value
Type Conversion: It is a process of converting an expression from one data type to another
data type
There are two types:
Implicit Type conversion
Explicit Type Conversion
Implicit Type Conversion: This type of conversion is done by the compiler, so it is called as
implicit type conversion. Without user intervention this process is carried out. Whenever we
are converting narrow operand (lower data type variable) into wide operand (higher data type
variable) then compiler will do it implicitly.
Example:
#include<stdio.h>
void main( )
{
char b= ‘A’;
int a;
a=b;
printf(“%d”,a);
}
OUTPUT: 65
Explicit Type Conversion: This type of conversion is done by the user so it is called explicit
type conversion. Whenever we are converting wider operand (higher data type variable) into a
narrower operand (lower data type variable) then its called explicit conversion.
Example:
#include<stdio.h>
void main()
{
int a=4;
float b;
b=1/(float)a;
printf(“%f”,b);
}
OUTPUT: 0.250000
=
*= /= %=
+= -= &= Assignment operators Right to left 14
^= |=
<<= >>=
, Comma operator Left to right 15
Examples:
1. If a=8, b=15 and c=4 calculate the expression
= 36 //Final Result
→ 5 <= 10 – 5 + 0 – 20 == 5 >= 1 != 20
→ 5 <= 5 + 0 – 20 == 5 >= 1 != 20
→ 5 <= 5 – 20 == 5 >= 1 != 20
→ 0 == 1 != 20
→ 0 != 20
→1
√𝑣 → sqrt(v)
| h | → abs(h)
gt → pow(g,t)
ex → exp(x)
𝑒√𝑥+𝑒√𝑦
3. 𝑃 = → P = ( exp ( sqrt ( x ) ) + exp ( sqrt ( y ) ) ) / ( x * sin ( sqrt ( y ) )
𝑥𝑠𝑖𝑛√𝑦
−𝑏+√𝑏2−4𝑎𝑐
4. 𝑋 = → X= ( ( -b ) + sqrt ( b * b – 4 * a * c ) ) / ( 2 * a )
2𝑎