Chapter 21-C Language1-2oP PDF
Chapter 21-C Language1-2oP PDF
Chapter 21-C Language1-2oP PDF
Computer Fundamentals
Pradeep K. Sinha
Priti Sinha
Chapter 21
Introduction to C
Programming
Language
Chapter 21: Introduction to C Programming Languages Slide 1/76
Learning Objectives
1
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Features of C
Ref. Page 424 Chapter 21: Introduction to C Programming Languages Slide 4/76
2
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
C Character Set
Uppercase alphabets A, B, C, , Z 26
Lowercase alphabets a, b, c, , z 26
Digits 0, 1, 2, , 9 10
~ `! @ # % ^ & * ( ) _
Special characters 31
+ = | \ {}[ ]:; " ' < > , . ? /
93
Ref. Page 425 Chapter 21: Introduction to C Programming Languages Slide 5/76
Constants
Ref. Page 425 Chapter 21: Introduction to C Programming Languages Slide 6/76
3
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Rules for Constructing Integer
Constants
Must have at least one digit
+ or sign is optional
No special characters (other than + and sign) are
allowed
Allowable range is:
-32768 to 32767 for integer and short integer
constants (16 bits storage)
-2147483648 to 2147483647 for long integer
constants (32 bits storage)
Examples are: 8, +17, -6
Ref. Page 426 Chapter 21: Introduction to C Programming Languages Slide 7/76
Ref. Page 426 Chapter 21: Introduction to C Programming Languages Slide 8/76
4
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Rules for Constructing Real Constants
in Exponential Form
Ref. Page 426 Chapter 21: Introduction to C Programming Languages Slide 9/76
Ref. Page 426 Chapter 21: Introduction to C Programming Languages Slide 10/76
5
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Variables
A C variable is an entity whose value may vary during
program execution
It has a name and type associated with it
Variable name specifies programmer given name to
the memory area allocated to a variable
Variable type specifies the type of values a variable
can contain
Example: In i = i + 5, i is a variable
Ref. Page 427 Chapter 21: Introduction to C Programming Languages Slide 11/76
Ref. Page 427 Chapter 21: Introduction to C Programming Languages Slide 12/76
6
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Data Types Used for Variable Type
Declaration
Ref. Page 428 Chapter 21: Introduction to C Programming Languages Slide 13/76
int count;
short index;
long principle;
float area;
double radius;
char c;
Ref. Page 427 Chapter 21: Introduction to C Programming Languages Slide 14/76
7
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Standard Qualifiers in C
Sign signed + or
unsigned + only
Ref. Page 428 Chapter 21: Introduction to C Programming Languages Slide 15/76
Ref. Page 428 Chapter 21: Introduction to C Programming Languages Slide 16/76
8
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Keywords
Keywords (or reserved words) are predefined words whose
meanings are known to C compiler
C has 32 keywords
Keywords cannot be used as variable names
Ref. Page 429 Chapter 21: Introduction to C Programming Languages Slide 17/76
Comments
Comments are enclosed within / and /
Comments are ignored by the compiler
Comment can also split over multiple lines
Example: / This is a comment statement /
Ref. Page 429 Chapter 21: Introduction to C Programming Languages Slide 18/76
9
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Operators
Operators
Operators in C are categorized into data access,
arithmetic, logical, bitwise, and miscellaneous
Associativity defines the order of evaluation when
operators of same precedence appear in an expression
a = b = c = 15, = has R L associativity
First c = 15, then b = c, then a = b is evaluated
Precedence defines the order in which calculations
involving two or more operators is performed
x + y z , is performed before +
Ref. Page 429 Chapter 21: Introduction to C Programming Languages Slide 20/76
10
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Arithmetic Operators
Operator Meaning with Example Associativity Precedence
Arithmetic Operators
+ Addition; x+y LR 4
- Subtraction; x-y LR 4
Multiplication; xy LR 3
/ Division; x/y LR 3
% Remainder (or Modulus); x%y LR 3
++ Increment;
x++ means post-increment (increment LR 1
the value of x by 1 after using its value);
Ref. Page 430 Chapter 21: Introduction to C Programming Languages Slide 21/76
Arithmetic Operators
Associativit
Operator Meaning with Example Precedence
y
Arithmetic Operators
-- Decrement;
x-- means post-decrement (decrement LR 1
the value of x by 1 after using its value);
--x means pre-decrement (decrement RL 2
the value of x by 1 before using its value)
Ref. Page 431 Chapter 21: Introduction to C Programming Languages Slide 22/76
11
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Logical Operators
Operator Meaning with Example Associativity Precedence
Logical Operators
! Reverse the logical value of a single variable; RL 2
!x means if the value of x is non-zero, make it
zero; and if it is zero, make it one
> Greater than; x>y LR 6
< Less than; x<y LR 6
>= Greater than or equal to; x >= y LR 6
<= Less than or equal to; x <= y LR 6
== Equal to; x == y LR 7
!= Not equal to; x != y LR 7
&& AND; x && y means both x and y should be LR 11
true (non-zero) for result to be true
|| OR; x || y means either x or y should be true LR 12
(non-zero) for result to be true
Ref. Page 431 Chapter 21: Introduction to C Programming Languages Slide 23/76
Bitwise Operators
Operator Meaning with Example Associativity Precedence
Bitwise Operators
~ Complement; ~x means RL 2
All 1s are changed to 0s and 0s to 1s
& AND; x & y means x AND y LR 8
| OR; x | y means x OR y LR 10
^ Exclusive OR; x ^ y means x y LR 9
<< Left shift; x << 4 means shift all bits in x LR 5
four places to the left
>> Right shift; x >> 3 means shift all bits LR 5
in x three places to the right
&= x &= y means x = x & y RL 14
|= x |= y means x = x | y RL 14
^= x ^= y means x = x ^ y RL 14
<<= x <<= 4 means shift all bits in x four places RL 14
to the left and assign the result to x
>>= x >>= 3 means shift all bits in x three RL 14
places to the right and assign the result to x
Ref. Page 431 Chapter 21: Introduction to C Programming Languages Slide 24/76
12
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 432 Chapter 21: Introduction to C Programming Languages Slide 25/76
Miscellaneous Operators
Associativit Precedenc
Operator Meaning with Example
y e
Miscellaneous Operators
Ref. Page 432 Chapter 21: Introduction to C Programming Languages Slide 26/76
13
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Statements
Statements
C program is a combination of statements written
between { and } braces
Each statement performs a set of operations
Null statement, represented by ; or empty {} braces,
does not perform any operation
A simple statement is terminated by a semicolon ;
Compound statements, called statement block, perform
complex operations combining null, simple, and other
block statements
Ref. Page 432 Chapter 21: Introduction to C Programming Languages Slide 28/76
14
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Examples of Statements
Ref. Page 432 Chapter 21: Introduction to C Programming Languages Slide 29/76
I/O Operations
15
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 433 Chapter 21: Introduction to C Programming Languages Slide 31/76
scanf() Enables input of formatted data from console (keyboard). Formatted input data
means we can specify the data type expected as input. Format specifiers for
different data types are given in Figure 21.6.
printf() Enables obtaining an output in a form specified by programmer (formatted
output). Format specifiers are given in Figure 21.6. Newline character \n is
used in printf() to get the output split over separate lines.
gets() Enables input of a string from keyboard. Spaces are accepted as part of the input
string, and the input string is terminated when Enter key is hit. Note that although
scanf() enables input of a string of characters, it does not accept multi-word
strings (spaces in-between).
puts() Enables output of a multi-word string
Ref. Page 433 Chapter 21: Introduction to C Programming Languages Slide 32/76
16
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Basic Format Specifiers for
scanf() and printf()
Format
Data Types
Specifiers
%d integer (short signed)
%u integer (short unsigned)
%ld integer (long signed)
%lu integer (long unsigned)
%f real (float)
%lf real (double)
%c character
%s string
Ref. Page 434 Chapter 21: Introduction to C Programming Languages Slide 33/76
Ref. Page 434 Chapter 21: Introduction to C Programming Languages Slide 34/76
17
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 434 Chapter 21: Introduction to C Programming Languages Slide 35/76
Preprocessor Directives
18
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Preprocessor Directives
Preprocessor is a program that prepares a program for
the C compiler
Examples of some common preprocessor directives in C
are:
Preprocessor
Use
directive
Used to look for a file and place its
#include contents at the location where this
preprocessor directives is used
Ref. Page 435 Chapter 21: Introduction to C Programming Languages Slide 37/76
#ifdef WINDOWS
.
.
.
Code specific to windows operating system
.
.
.
#else
.
.
.
Code specific to Linux operating system
.
.
.
#endif
.
.
.
Code common to both operating systems
Ref. Page 435 Chapter 21: Introduction to C Programming Languages Slide 38/76
19
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Standard Preprocessor
Directives in C
Preprocessor Directive Meaning Category
# Null directive
#error message Prints message when processed
Simple
#line linenum filename Used to update code line number and filename
#pragma name Compiler specific settings
#include filename Includes content of another file File
#define macro/string Define a macro or string substitution
#undef macro Removes a macro definition Macro
Ref. Page 436 Chapter 21: Introduction to C Programming Languages Slide 39/76
20
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Pointers
C pointers allow programmers to directly access
memory addresses where variables are stored
Pointer variable is declared by adding a symbol
before the variable name while declaring it.
If p is a pointer to a variable (e.g. int i, *p = i;)
Using p means address of the storage location of
the pointed variable
Using p means value stored in the storage location
of the pointed variable
Operator & is used with a variable to mean variables
address, e.g. &i gives address of variable i
Ref. Page 437 Chapter 21: Introduction to C Programming Languages Slide 41/76
1000 62 i
Address of i = 1000
Value of i = 62
int i = 62;
int p;
int j;
p = &i; / p becomes 1000 /
j = p; / j becomes 62 /
j = 0; / j becomes zero /
j = (&i) / j becomes 62 /
Ref. Page 437 Chapter 21: Introduction to C Programming Languages Slide 42/76
21
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Array
Ref. Page 438 Chapter 21: Introduction to C Programming Languages Slide 43/76
Ref. Page 438 Chapter 21: Introduction to C Programming Languages Slide 44/76
22
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
String
A string is a one-dimensional array of characters
terminated by a null character (\0)
It is initialized at declaration as
char name[] = PRADEEP;
Its individual elements can be accessed in the same way
as we access array elements such as name[3] = D
Strings are used for text processing
C provides a rich set of string handling library functions
Ref. Page 439 Chapter 21: Introduction to C Programming Languages Slide 45/76
Ref. Page 440 Chapter 21: Introduction to C Programming Languages Slide 46/76
23
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 439 Chapter 21: Introduction to C Programming Languages Slide 48/76
24
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Structure
It is a UDT containing a number of data types grouped
together
Its constituents data types may or may not be of different
types
It has continuous memory allocation and its minimum size
is the sum of sizes of its constituent data types
All elements (member variable) of a structure are publicly
accessible
Each member variable can be accessed using . (dot)
operator or pointer (EmpRecord.EmpID or EmpRecord
EmpID)
It can have a pointer member variable of its own type,
which is useful in crating linked list and similar data
structures
Ref. Page 439 Chapter 21: Introduction to C Programming Languages Slide 49/76
Structure (Examples)
struct Employee struct Employee
{ {
int EmpID; int EmpID;
char EmpName[20]; char EmpName[20];
}; } EmpRecord;
Ref. Page 440 Chapter 21: Introduction to C Programming Languages Slide 50/76
25
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Union
It is a UDT referring to same memory location using several
data types
It is a mathematical union of all constituent data types
Each data member begins at the same memory location
Minimum size of a union variable is the size of its largest
constituent data types
Each member variable can be accessed using , (dot)
operator
Section of memory can be treated as a variable of one type
on one occasion, and of another type on another occasion
Ref. Page 441 Chapter 21: Introduction to C Programming Languages Slide 51/76
Union Example
unionNum
{
int intNum;
unsigned
unsNum
};
union Num Number;
Ref. Page 441 Chapter 21: Introduction to C Programming Languages Slide 52/76
26
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Difference Between Structure and
Union
Both group a number of data types together
Structure allocates different memory space contiguously
to different data types in the group
Union allocates the same memory space to different
data types in the group
Ref. Page 441 Chapter 21: Introduction to C Programming Languages Slide 53/76
Control Structures
27
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Control Structures
Control structures (branch statements) are decision
points that control the flow of program execution based
on:
Some condition test (conditional branch)
Without condition test (unconditional branch)
They ensure execution of other statement/block or
cause skipping of some statement/block
Ref. Page 442 Chapter 21: Introduction to C Programming Languages Slide 55/76
Ref. Page 442 Chapter 21: Introduction to C Programming Languages Slide 56/76
28
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Examples of if Construct
if (i <= 0) if (i <= 0)
i++; i++;
else if (i >= 0)
j++;
if (i <= 0) else
i++; k++;
else
j++;
Ref. Page 442 Chapter 21: Introduction to C Programming Languages Slide 57/76
Ref. Page 443 Chapter 21: Introduction to C Programming Languages Slide 58/76
29
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 444 Chapter 21: Introduction to C Programming Languages Slide 59/76
Ref. Page 444 Chapter 21: Introduction to C Programming Languages Slide 60/76
30
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Loop Structures
Loop Structures
Ref. Page 444 Chapter 21: Introduction to C Programming Languages Slide 62/76
31
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 444 Chapter 21: Introduction to C Programming Languages Slide 63/76
Ref. Page 444 Chapter 21: Introduction to C Programming Languages Slide 64/76
32
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Posttest Loop Construct
dowhile
It has a loop condition only that is tested after each
iteration to decide whether to continue with next
iteration or terminate the loop
Example of dowhile is:
do {
printf(i = %d, i);
i++;
} while (i < 10) ;
Ref. Page 445 Chapter 21: Introduction to C Programming Languages Slide 65/76
Functions
33
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Functions
Functions (or subprograms) are building blocks of a
program
All functions must be declared and defined before use
Function declaration requires function name, argument
list, and return type
Function definition requires coding the body or logic of
function
Every C program must have a main function. It is the
entry point of the program
Ref. Page 445 Chapter 21: Introduction to C Programming Languages Slide 67/76
Example of a Function
int myfunc ( int Val, int ModVal )
{
unsigned temp;
temp = Val % ModVal;
return temp;
}
Ref. Page 446 Chapter 21: Introduction to C Programming Languages Slide 68/76
34
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
# include <stdio.h>
void main()
{
int number, remainder;
clrscr(); / clears the console screen /
printf (Enter an integer: );
scanf (%d, &number);
remainder = number % 2;
if (remainder == 0)
printf (\n The given number is even);
else
printf (\n The given number is odd);
getch();
}
Ref. Page 446 Chapter 21: Introduction to C Programming Languages Slide 69/76
# include <stdio.h>
# include <conio.h>
void main()
{
int day;
clrscr();
printf (Enter an integer in the range 1 to 7);
scanf (%d, &day);
Ref. Page 447 Chapter 21: Introduction to C Programming Languages Slide 70/76
35
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
Ref. Page 447 Chapter 21: Introduction to C Programming Languages Slide 71/76
# include <stdio.h>
# include <conio.h>
# define PI 3.1415
void main()
{
float radius, area, circum;
clrscr();
printf (Enter the radius of the circle: );
scanf (%f, &radius);
area = PI radius radius;
circum = 2 PI radius;
printf (\n Area and circumference of the circle are %f
and %f respectively, area, circum);
getch();
}
(Continued on next slide)
Ref. Page 448 Chapter 21: Introduction to C Programming Languages Slide 72/76
36
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main()
{
char input_string[50]; / maximum 50 characters /
int len;
int i = 0, cnt = 0;
clrscr();
printf (Enter a string of less than 50 characters: \n);
gets (input_string);
len = strlen (input_string);
for (i = 0; i < len; i++)
{
switch (input_string[i])
Ref. Page 448 Chapter 21: Introduction to C Programming Languages Slide 73/76
Ref. Page 448 Chapter 21: Introduction to C Programming Languages Slide 74/76
37
Computer Fundamentals : Pradeep K. Sinha & Priti Sinha
#include <stdio.h>
#define SIZE 10
Ref. Page 449 Chapter 21: Introduction to C Programming Languages Slide 75/76
Key Words/Phrases
Arithmetic operators Main function
Arrays Member element
Assignment operators Null statement
Bit-level manipulation Operator associativity
Bitwise operators Operator precedence
Branch statement Pointer
Character set Posttest loop
Comment statement Preprocessor directives
Compound statement Pretest loop
Conditional branch Primitive data types
Conditional compilation Reserved words
Constants Simple statement
Control structures Statement block
Format specifiers Strings
Formatted I/O Structure data type
Function Unconditional branch
Keywords Union data type
Library functions User-defined data types
Logical operators Variable name
Loop structures Variable type declaration
Macro expansion Variables
38