C Language
C Language
1
MEMORIES
• STORAGE DEVICE - PRIMARY STORAGE
- SECONDARY STORAGE
2
MEMORIES
• ROM (READ ONLY MEMORY)
• PROM
• EPROM
• EEPROM
3
SOFTWARE
• SYSTEM SOFTWARE (Device drivers & OS)
4
COMPUTER ENVIRONMENT TYPES
• Personal Computing Environment
• Time-Sharing Environment
5
COMPUTER ENVIRONMENT TYPES
• Client-Server Environment
6
COMPUTER LANGUAGES
7
COMPUTER LANGUAGES
8
COMPUTER LANGUAGES
9
COMPUTER LANGUAGES
• High Level Language – According to their use the high level language
is classified into many types.
• Algebraic Formula-Type Processing.
• Business Data Processing
• String and List Processing
• Object Oriented Programming Language
• Visual programming language
10
HIGH LEVEL LANGUAGES
11
HIGH LEVEL LANGUAGES
12
HIGH LEVEL LANGUAGES
• String & List Processing – Manipulation including search for
patterns, inserting and deleting characters.
Eg: • LISP (List Processing).
• ProLog (Program in Logic)
14
WHAT IS ‘C’ ?
• C is a programming language.
• Developed at AT & T‟s Bell Laboratories of USA in 1972.
• It was designed and written by Dennis Ritche.
• Dennis Ritchie is known as the founder of c language.
15
Features of ‘C’
• Machine Independent.
• Fast program Execution.
• Extendible Language.
• Structured Language
16
General Structure of ‘C’
/* Documentation section */ - Display info about program name etc
/* Link section */ - Inclusion of header files
- Defining Constants
/* Definition section */
- Global Variable declaration
/* Global declaration section */
main() main()
{ {
Declaration part Local Variable Declaration
Executable part (statements) Executable part (statements)
} }
/* Sub-program section */ /* Sub-program section */
17
First C Program
#include <stdio.h> Includes Standard I/O functions – printf()
#include <conio.h> Includes Console I/O function – getch()
void main() Main function – void refers no return value
{ {
printf("Hello C Language"); Print data on console
getch(); Asks for single character. Until you press a
} key, the screen is blocked.
}
18
C TOKENS
The smallest individual units are known as tokens. C has six types of
tokens.
1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Special Symbols
6. Operators
19
C TOKENS - IDENTIFIERS
Identifiers: Identifiers refer to the names of variables, constants, functions and arrays.
These are user-defined names is called Identifiers. These identifier are defined against a
set of rules.
1. An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) &
underscore( _).
2. The first character of an identifier can only contain alphabet( a-z , A-Z ) or
underscore ( _).
3. Identifiers are also case sensitive in C. Eg: name and Name are two different
identifier in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters other than underscore are permitted to be used Identifier.
6. C‟ compiler recognizes only the first 31 characters of an identifiers.
20
C TOKENS-Identifiers
Ex : Valid Invalid
STDNAME Return
SUB $stay
TOT_MARKS 1RECORD
_TEMP STD NAME.
Y2K
21
C TOKENS-Keywords
Keywords
22
C TOKENS-Keywords
A list of 32 keywords in c language is given below:
auto break case char
24
DATA TYPES
• There are 4 types of data types
25
PRIMARY DATA TYPES
26
PRIMARY DATA TYPES
Type Size (bytes) Range Control
String
char or signed char 1 -128 to 127 %c
27
VARIABLES
A variable is a name of memory
location. It is used to store data.
29
DECLARATION OF VARIABLES
• A variable can be used to store a value of any data type. The declaration
of variables must be done before they are used in the program.
30
ASSIGNING VALUES TO VARIABLES
• Values can be assigned to variables using the assignment operator (=).
Syntax : variable = constant;
Example: x=100;
a= 12.25;
m=‟f‟;
• We can also assign a value to a variable at the time of variable declaration
Syntax : data_type variable = constant;
Example : int x=100;
float a=12.25;
char m=“f”;
31
TYPES OF VARIABLES IN C
• Local Variable: Defined inside the function and is limited to that function
only.
• Global Variable: Defined outside the function and is not limited to specific
function.
• Input: In any programming language input means to feed some data into program,
that is entering data.
38
INPUT OUTPUT FUNCTIONS
I / O Functions
Input Output
Input Output
scanf() print()
getc() putc()
fscanf() fprintf()
getchar() putchar()
gets() puts()
getch()
getche()
39
FORMATTED I/O FUNCTIONS
printf() : output data or result of an operation can be displayed from the computer to
a standard output device using the library function printf(). This function is used to
print any combination of data.
Syntax : printf(“control string “, variable1, variable2, , variablen);
40
FORMATTED I/O FUNCTIONS
scanf() : The scanf() function is used to read information from the standard input
device (keyboard)
Each variable name (argument) must be preceded by an ampersand (&). The (&)
symbol gives the meaning “address of “ the variable.
41
UNFORMATTED I/O FUNCTIONS
The unformatted I/O functions are of two types
• Character I/O
• String I/O
Character I/O:
• getchar(): Used to read a character from the standard input
• putchar(): Used to display a character to standard output
• getche() read and display the character
• getch() only read the single character but not display
• putch(): Used to display any alpha numeric characters to standard output
42
UNFORMATTED I/O FUNCTIONS
String I/O
gets(): Used for accepting any string from the standard input.
43
OPERATORS
An operator is a Symbol that Types of Operator :
performs an operation. Arithmetic Operators.
An operator acts on some variables Relational Operators.
called operands to get the desired Logical Operators.
result.
Assignment Operators.
Unary Operators.
Ex : a+b; Conditional Operators.
Where a,b are operands and + Special Operators.
is the operator
Bitwise Operators.
Shift Operators.
44
ARITHEMATIC OPERATORS
#include <stdio.h>
An arithmetic operator performs void main()
{
mathematical operations such as int a = 9,b = 4, c;
c = a+b;
addition, subtraction and multiplication printf("a+b = %d \n",c);
c = a-b;
on numerical values printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
C Program to demonstrate the working of printf("a/b = %d \n",c);
arithmetic operators c=a%b;
printf("Remainder when a divided by b = %d \n",c);
}
45
RELATIONAL OPERATORS
• A relational operator checks the relationship between two operands. If the relation is true, it returns 1;
if the relation is false, it returns value 0.
• Operands may be variables, constants or expressions.
Operator Meaning Example Return value
== is equal to 2==3 0
46
RELATIONAL OPERATORS
// C Program to demonstrate the working of Output
relational operators #include <stdio.h>
5 == 5 = 1
int main() 5>5=0
{
5 < 10 = 1
int a = 5, b = 5, c = 10;
5 != 5 = 0
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d > %d = %d \n", a, b, a > b); //false 5 <= 5 = 1
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
return 0;
}
47
LOGICAL OPERATORS
The Logical Operators are used to combine the results of two or more conditions.
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false.
48
LOGICAL OPERATORS
Logical AND : If any one Logical OR : If any one Logical Not : This operator
condition false the complete condition true the complete reverses the value of the
condition becomes false. condition becomes true. expression it operates on i.e,
it makes a true expression
false and false expression
Op1 Op2 Op1 && Op2 Op1 Op2 Op1 // Op2 true.
true true true true true true
true false false true false true
false true false false true true
false false false Op1 Op1 !
false false false
true false
false true
49
LOGICAL OPERATORS
// C Program to demonstrate the working of logical Output
operators
#include <stdio.h> (a = b) && (c > b) equals to 1
int main()
(a = b) || (c < b) equals to 1
{
int a = 5, b = 5, c = 10, result; !(a == b) equals to 0
result = (a = b) && (c > b);
printf("(a = b) && (c > b) equals to %d \n", result);
result = (a = b) || (c < b);
printf("(a = b) || (c < b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
50
ASSIGNMENT OPERATORS
Assignment operators are used to assign a value Compound assignment operator:
(or) an expression (or) a value of a variable to
another variable. „C‟ provides compound assignment operators to
assign a value to variable in order to assign a new
value to a variable after performing a specified
Syntax : variable name=expression (or) value (or) variable operation.
51
ASSIGNMENT OPERATORS
Unary operators are having higher priority than other Pre-Increment
operators. Unary operators, meaning Pre-increment operator is used to increment the value of
variable before using in the expression. In the Pre-Increment
they only operate on a single operand.
value is first incremented and then used inside the expression.
52
ASSIGNMENT OPERATORS
• // C Program to demonstrate the working of increment Output
and decrement operators
++a = 11
#include <stdio.h> --b = 99
int main() ++c = 11.500000
{ ++d = 99.500000
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
53
ASSIGNMENT OPERATORS
Multiple increment operators inside printf Postfix and Prefix Expression in Same Statement
#include<stdio.h> #include<stdio.h>
void main() #include<conio.h>
void main()
{ {
int i = 1; int i = 0, j = 0; j = i++ + ++i;
printf("%d %d %d", i, ++i, i++); printf("%d\n", i);
printf("%d\n", j);
} }
Output : 3 3 1
Output :
2
54
ASSIGNMENT OPERATORS
Postfix and Prefix Expression in Same Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 0, j = 0; j = i++ + ++i;
printf("%d\n", i);
printf("%d\n", j);
}
Output :
2
55
CONDITIONAL (or) TERNARYOPERATORS
Conditional Operator checks the condition and executes the statement depending of the condition.
A conditional operator is a ternary operator, that is, it works on 3 operands.
56
CONDITIONAL (or) TERNARYOPERATORS
#include <stdio.h> Output
int main() If this year is leap year, enter 1. If not enter
{ any integer: 1
char February; Number of days in February = 29
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to 29.
// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
57
BIT WISE OPERATORS
Bitwise operators are used to manipulate the data Bitwise AND operator ‘&’
at bit level. It operates on integers only. It may not The output of bitwise AND is 1 if the corresponding bits of
be applied to float. two operands is 1. If either bit of an operand is 0, the result
of corresponding bit is evaluated to 0.
Operator Meaning
& Bitwise AND
Let us suppose the bitwise AND operation of two integers 12
| Bitwise OR
and 25.
^ Bitwise XOR
<< Shift left
12 = 00001100 (In Binary)
>> Shift right 25 = 00011001 (In Binary)
~ One‟s complement.
Bit Operation of 12 and 25 is
00001100
& 00011001
58
BIT WISE OPERATORS
Bitwise OR operator | 00001100
The output of bitwise OR is 1 if at least one ^ 00011001
corresponding bit of two operands is 1. In C 00010101 = 21 (In decimal)
Programming, bitwise OR operator is denoted by |.
Bitwise OR Operation of 12 and 25
Bitwise complement operator ~
00001100 Bitwise compliment operator is an unary operator (works on
| 00011001 only one operand). It changes 1 to 0 and 0 to 1. It is denoted by
~.
00011101 = 29 (In decimal)
35 = 00100011 (In Binary)
Bitwise XOR (exclusive OR) operator ^ Bitwise complement Operation of 35
The result of bitwise XOR operator is 1 if the ~ 00100011
corresponding bits of two operands are opposite. It is 11011100
denoted by ^.
Bitwise XOR Operation of 12 and 25 For any integer n, bitwise complement of n will be -(n+1).
59
BIT WISE OPERATORS
#include <stdio.h> Output
int main()
Output = 8
{
Output = 29
int a = 12, b = 25;
printf("Output = %d \n", a&b);
Output=21
printf(“Output=%d \n”, a|b); Complement of a is=-13
printf(“Output=%d \n”,a^b); Complement of b is=-26
printf(“Complement of a is=%d \n”,~a);
printf(“Complement of b is=%d \n”,~b);
return 0;
}
60
SHIFT OPERATORS
There are 2 Bitwise shift operators in C programming:
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by
<<.
61
SPECIAL OPERATORS
Comma Operator :The comma operator is used to Size of Operator : The sizeof() is a unary
separate the statement elements such as variables,
constants or expressions, and this operator is used operator, that returns the length in bytes o the
to link the related expressions together, such specified variable, and it is very useful to find
expressions can be evaluated from left to right and the bytes occupied by the specified variable in
the value of right most expressions is the value of
combined expressions the memory.
62
SPECIAL OPERATORS
#include <stdio.h> Output
int main() Size of int=4 bytes
{ Size of float=4 bytes
Size of double=8 bytes
int a, e[10]; float b; double c; char d;
Size of char=1 byte
printf("Size of int=%lu bytes\n",sizeof(a)); Size of integer type array having 10 elements = 40 bytes
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10
elements = %lu bytes\n", sizeof(e)); return 0;
}
63
Expressions
An expression is a combination of operators and Algeberiac ‘C’
operands which reduces to a single value. An
operator indicates an operation to be performed on Expressions Expressions
data that yields a value. An operand is a data item 1 : ax2+bx+c 1: a*x*x+b*x+c
on which an operation is performed. 2 : a+bx 2 : a+b*x.
3 : 4ac/b 3 : 4*a*c/b.
Example: 4 : x2/y2-1 4 : x*x/y*y-1
3+5 is a simple expression which yields a value 8, -a
is also a single expression.
A complex expression contain more than one
operator.
64
Operator Precedence
Arithmetic Operators are evaluated left to right using
the precedence(Priority) of operator when the Example:
expression is written without the parenthesis. They are a=x-y/3+z*2+p/4.
two levels of priority. x=7, y=9, z=11, p=8.
a= 7-9/3+11*2+8/4.
1 : High Priority * / %
2 : Low Priority +- 1st phase :
1 : a = 7-3+11*2+8/4
Arithmetic Expression evaluation is carried out using the 2 : a = 7-3+22+8/4
two phases from left to right. 3 : a = 7-3+22+2
2nd phase :
First phase : The highest priority operator are evaluated
in the 1st phase. 1 : a = 4+22+2
Second Phase : The lowest priority operator are 2 : a = 26+2
evaluated in the 2nd phase. 3 : a = 28
65
Operator Precedence
Operator Description Precedence Associativity
The order of evaluation can be changed by putting + Unary plus 2 R-L (right to left)
parenthesis in an expression. - Unary minus
++ Increment
-- Decrement
! Not operator
Whenever parentheses are used, the expressions ~ Complement
Pointer operator
within parentheses highest priority. *
Address
&
sizeof operator
Sizeof operator
If two or more sets of parenthesis appear one after Conditional 13 R-L (right to left)
= *= /= %= += Assignment operator 14 R-L (right to left)
another. The expression contained in the left-most -= &= ^= <<=
set is evaluated first and the right-most in the last. >>=
66
Type Casting or Type Conversion
Type conversion is used to convert variable from one data Implicit Type Conversion
type to another data type, and after type casting complier
treats the variable as of new data type.
When the type conversion is performed automatically by the
compiler without programmers intervention, such type of
Syntax: (type_name) expression; conversion is known as implicit type conversion or type
promotion.
Without Type Casting:
main()
{
int f= 9/4; int x;
printf("f : %d\n", f ); for(x=97; x<=122; x++)
printf("%c", x);
}
With Type Casting:
67
Type Casting or Type Conversion
Explicit Type Conversion Rules
All integer types to be converted to float.
The type conversion performed by the All float types to be converted to double.
programmer by posing the data type of the
All character types to be converted to integer.
expression of specific type is known as explicit
type conversion. The explicit type conversion is int x=7, y=5 ;
also known as type casting float z;
void main() z=x/y; /*Here the value of z is 1*/
68