Est102 - C - Module 2
Est102 - C - Module 2
MODULE-2
Introduction to C programming
● C is a structured, high-level, machine independent programming language.
● It allows software developers to develop programs without worrying about the
hardware platforms where they will be implemented.
● The root of all modern languages is ALGOL.
● BCPL (Basic Combined Programming Language) was developed for writing
system software.
● Using many features of BCPL, a new language was created which was named
B.
● C evolved from ALGOL, BCPL and B by Dennis Ritchie at Bell Laboratories in
1972.
● C uses many concepts from these languages and added the concept of data
types and other powerful features.
● C language became more popular with the publication of the
book ‘The C Programming Language’ by Brian Kerningham
and Dennis Ritchie.
● The book gained popularity and the language came to be
known as “K&R C”.
● To ensure that the C language standard, ANSI approved a
version of C which is called ANSI C.
● C language was again improved, enhanced and became an
ANSI/ISO approved language.
● All popular computer languages are dynamic in nature.
● They continue to improve their power and scope by new
features.
● The standardization committee of C added few features of
C++ , Java to C to enhance usefulness of the language which
is referred to as C99.
History of C Language:
Basic Structure of C programs:
● A C-program may contain one or more sections as shown below;
● The documentation section consists of a set of comment lines giving the
name of the program, the author and other details, which the programmer
would like to use later.
● The link section provides instruction to the compiler to link functions from
the system library.
● The definition section defines all symbolic constants.
● There are some variables that are used in more than one function which
are called global variables and are declared in the global declaration
section, that is outside of all the functions.
● Every C program must have one main() function section.
○ This section contains two parts;
■ Declaration part: declares all variables used in executable part.
■ Executable part: There is at least one statement in the
executable part.
○ These 2 parts appear between the opening and closing braces.
○ The program execution begins at the opening brace and ends at the
closing brace.
○ The closing brace of main() section is the logical end of the program.
○ All statements in the declaration and executable parts end with a
semicolon(;).
● The subprogram section contains all the user-defined functions
that are called in the main function.
○ These are placed immediately after the main function.
○ They can appear in any order.
● A function is a subroutine that may include one or more
statements designed to perform a specific task.
● To write a C program, we create functions and then put them
together.
Sample C-program to print a message:
}
The #include directive
● #include is a preprocessor directive.
○ C-programs are divided into modules or functions.
○ Some functions are written by users and some others stored in C library.
○ Library functions are grouped category-wise and are stored in different
files known as header files.
○ If we want to access functions stored in library, it is necessary to tell the
compiler about the files to be accessed.
○ This is achieved by using the preprocessor directive #include as follows:
#include<filename>
○ Filename is the name of the library file that contain the required function
definition.
○ Preprocessor directives are placed at the beginning of a program.
● Following are some of the commonly used header files;
○ <stdio.h> Standard I/O library functions
○ <math.h> Mathematical functions
○ <string.h> String manipulation functions
○ <time.h> Time manipulation functions
○ <ctype.h> Character testing & conversion functions
○ <stdlib.h> Utility functions
● The pre-processor directive tells the compiler to include the mentioned
header file in the program.
● stdio.h is a header file that contains the definitions of common input output
functions such as scanf() and printf() etc. It activates keyboard and
monitor.
The main() function
● main() is a part of every C-program.
● C permits different forms of main statement.
○ main()
○ int main()
○ void main()
○ main(void)
○ void main(void)
○ int main(void)
● The empty pair of parentheses indicates that the function has no arguments.
● This may be explicitly indicated by using the keyword void inside the
parentheses.
● We may also specify the keyword int or void before the word main.
● The keyword void means that the function does not return any value to the
operating system and int means that the function returns an integer value to
the operating system.
● When int is specified , the last statement in the program must be “return 0”
Comments:
● Single line comment is represented using //
● The lines beginning with /* and ending with */ are known as
comment lines.
● Comment lines enhances the readability.
● Comment lines are not executable statements and therefore
anything between /* and */ is ignored by the compiler.
● Comments cannot be nested in C.
Predefined functions:
● Functions that has already been written, compiled and linked
together with our program at the time of linking.
● Eg: printf and scanf
● Printf-causes everything between the starting and ending quotation
marks to be printed out.
● Consider the statement below:
printf(“Hello world!!”);
It produces the following output;
Hello world!!
● The information contained between the parentheses is called the
argument of the function.
Note:
Integer constants:
● Integer constant refers to a sequence of digits.
● There are 3 types of integers: decimal integers, octal integer
and hexadecimal integer.
● Decimal integers have set of digits , 0 to 9, preceded by an
optional + or - sign.
○ Valid examples of decimal integer constants are
426 +859 0 85963 +78
● Commas, embedded spaces, non-digit characters are not
permitted between digits. Eg: 15 720 20,000 $23 are
illegal numbers..
Octal Integer constants:
● Consist of any combination of digits from 0 to 7, with a
leading 0.
○ Eg: 037 0 0435 0521
data-type variable-list;
Void types:
● The void type has no values.
● Usually used to specify the type of functions.
● If a function is specified as type void, it do not return any value to
the calling function.
Character types:
● A single character can be defined as a character(char)
type data.
● Characters are usually stored in 8 bits(1 byte) of
internal storage.
● The qualifier signed or unsigned may be explicitly
applied to char.
● Unsigned chars have values between 0 and 255.
● Signed chars have values from -128 to 127.
Integer types:
● Used to hold integer quantities that do not contain a decimal
point.
● Generally, integers occupy one word of storage.
● The word size of machines do vary. So, the size of an integer that can
be stored depends on the computer.
● C has the following classes of integer storage;
○ int Signed
○ short int
○ long int
○ unsigned int
○ unsigned short int
○ unsigned long int
● short int represents fairly
small integer values and
requires half the amount
of storage as a regular int
number uses.
● unsigned integers are
always positive and all
their bits are used for
magnitude.
Floating point types:
● Floating point (real) numbers are stored in 32 bits, with 6 digits of
precision.
● They are defined by the keyword float.
● When the accuracy provided by a float number is not sufficient, the
type double can be used.
● The double data type uses 64 bits giving a precision of 14 digits.
● The data type double represents the same data type that float
represents, but with a greater precision.
● To extend precision further, we may use long double which uses 80
bits.
Console IO operations:
● These operations allow us to receive input from the input
devices like keyboard and provide output to the output
devices like the Visual Display Unit.
● The console comprises of the keyboard and the screen.
● IO operations can be classified into
○ Formatted IO functions
○ Unformatted IO functions
● The basic difference between them is that the formatted
functions allow the input read from the keyboard or the
output displayed on the VDU to be formatted as per our
requirements.
● Eg: If values of average marks and percentage marks are to be
displayed on the screen, then the details like where this output
would appear on the screen, how many spaces would be present
between the two values, the number of places after the decimal
points, etc. can be controlled using formatted functions.
● The functions printf( ), and scanf( ) fall under the category of
formatted console I/O functions.
● These functions allow us to supply the input in a fixed format
and let us obtain the output in the specified form.
scanf:
● scanf( ) allows us to enter data from keyboard that will be
formatted in a certain way.
● The general form of scanf( ) statement is as follows:
○ scanf ( "format string", list of addresses of variables ) ;
● For example: scanf ( "%d %f %c", &c, &a, &ch ) ;
● Note that we are sending addresses of variables (addresses are
obtained by using ‘&’ the ‘address of’ operator) to scanf( ) function.
● This is necessary because the values received from keyboard must
be dropped into variables corresponding to these addresses.
● The values that are supplied through the keyboard must be
separated by either blank(s), tab(s), or newline(s).
● Do not include these escape sequences in the format string.
printf:
● The general form of printf is as follows:
printf ( "format string", list of variables ) ;
● The format string can contain:
○ Characters that are simply printed as they are .
○ Conversion specifications that begin with a % sign.
○ Escape sequences that begin with a \ sign
Format Specifiers:
● The format specifiers are used in C for input and output
purposes.
● Using this concept, the compiler can understand that what type
of data is in a variable during taking input using the scanf()
function and printing using printf() function.
● %d - Signed integer
● %f - Float value
● %c - Character
● %s - String
Operators & Expressions:
● C supports a rich set of built-in operators.
● An operator is a symbol that tells the computer to perform certain
mathematical and logical manipulations.
● Operators are used in programs to manipulate data and variables.
● They usually form a part of mathematical or logical expressions.
● C operators can be classified into following categories:
○ Arithmetic operators.
○ Relational operators.
○ Logical operators.
○ Assignment operators
○ Increment & Decrement operators
○ Conditional operators
○ Bitwise operators
○ Special operators
Arithmetic Operators:
● C provides all arithmetic operators.
● Integer division truncates any fractional part.
● The modulo division operation produces the remainder of an integer
division.
● The modulo division operator cannot be used on floating point data.
●
● Examples of use of arithmetic operators are:
○ a-b
○ a+b
○ a*b
○ a/b
○ a%b
○ -a%b
● Here, a and b are variables and are known as operands.
● 3 categories of arithmetic operations:
○ Integer Arithmetic.
○ Real Arithmetic
○ Mixed-mode arithmetic.
Integer Arithmetic:
● When both operands in an arithmetic expression are integers, that
expression is called integer expression and the operation is called integer
arithmetic.
● Integer arithmetic always yields an integer value.
● Eg: If a and b are integers and a=14 and b=4, then;
○ a-b=10
○ a+b=14
○ a*b=56
○ a/b=3 (decimal part truncated)
○ a%b=2 (remainder of division)
Real Arithmetic:
● An arithmetic operation involving only real operands.
● A real operand may assume values either in decimal or exponential notation.
● Since floating point values are rounded to the number of significant digits
permissible, the final value is an approximation of the correct result.
● The operator % can’t be used with real operands.
Mixed-mode arithmetic:
● When one of the operands is real and the other is integer, the
expression is called a mixed-mode arithmetic.
● If either operand is of the real type, then only the real operation is
performed and the result is always a real number.
● Thus, 15/10.0 = 1.5
whereas
15/10 = 1
Relational Operators:
● We may come across situations where we have to compare two quantities
and depending on their relation, we may have to take a decision.
● These comparisons can be done using relational operators.
● Eg: Comparing age of two persons, compare price of two items etc.,
● Expression containing a relational operator is called a relational
expression.
● The value of relational expression is either one or zero.
● If the specified relation is true, the value is is 1.
● If the specified expression is false, the value is 0.
● Eg:
10 < 20 is true
And
20 < 10 is false
● C supports 6 relational operators:
v op=exp;
x += y+1;
or
○ ‘increment x by y+1’
Bitwise Operator:
● If the test expression is true, then the true block statement(s) , immediately
following the if statements are executed.
● Otherwise, the false block statement(s) are executed.
● In either case, either true-block or false-block will be executed, not both.
● In both the cases, the control is transferred subsequently to the statement-x.
● Eg: Consider the following program segment;
● When a series of decisions are involved, we may have to use more than
if...else statement in nested form as shown below:
printf(“\n”);
Additional features of for loop:
● The for loop in C has several capabilities that are not found in other loop
constructs.
○ More than one variable can be initialized at a time in the for statement.
■ The multiple arguments in the initialization section are separated by
commas.
○ More than one variable can be incremented in the for statement.
■ The multiple arguments in the increment section are separated by
commas.
○ The test condition may have any compound relation and the testing need
not be limited only to the loop control variable.
○ If necessary, one or more sections of for loop can be omitted.
■ However semicolons separating the sections must remain.