1. LANGUAGE
A language is a communication media between two parties
Programming languages: A programming language is a language
used in writing programs to direct processing steps to be carried
out by a computer.
3. Machine level Language
▪ Machine code is the fundamental language of a computer and is
normally written as strings of binary 1’s and 0’s. The circuitry of a
computer is wired in such a way that it immediately recognizes
the machine language and converts it into the electrical signals
needed to run the computer.
▪ Programs written in machine language can be executed very fast
by the computer. This is mainly because the CPU directly
understands machine instructions and no translation of the
program is required.
▪ Advantages:- If we know this language we can directly interact
with the system, with out depending on any other applications.
4. Assembly language
The language, which substitutes letters and symbols for the
numbers in the machine language program, is called an assembly
language or symbolic language. The translator program that
translates an assembly code into the computer’s machine code is
called assembler. The assembler is a system program, which is
supplied by the computer manufacturer.
Advantages of Assembly language:
▪Easier to understand and use
▪Easy to locate and correct errors
▪Easier to modify
▪No worry about addresses
▪Easily re locatable.
5. High level language
▪ Every instruction, which the programmer writes in a
high-level language, is translated into many machine
language instructions.
▪ While writing programs in any of the above
languages, a programmer has to remember all the
operation codes of the computer and now in detail
what each code does and how it affects the various
registers of the computers.
▪ These languages enable the programmer to write
instructions using English words and familiar
mathematical symbols.
6. Low level languages
They are machine dependent languages. These are easily
understood by computers. The machine language and assembly
languages are examples of low level languages.
7. Machine Languages
As the name implies, programming is done at machine level. The
vocabulary of computer consists of only two words zero and one.
e.g., 10101011
8. Assembly Language
To overcome the drawbacks of the machine level
language, computer engineers developed new
programming language which uses symbolic
instructions. This symbolic instruction oriented
programming language is known as Assembly level
language.
E.g., ADD X,Y; Add the contents of y to x
9. Language Processors
Language Processors are basically translator programs. They are
used to translate programs written in one programming language to
machine level language. The common translators are Interpreters,
Compilers and Assemblers.
10. Interpreter
It is a program which translates one statement of a
high level language program into machine language
at a time and executes it. Here the translation and
execution are taking place simultaneously. If there is
an error, then that error must be corrected. The
process of translating high level language program
into machine language statement by statement is
called interpretation. e.g., Basic Interpreters, Java
Interpreters.
11. Compiler
Compiler is a program which takes the entire high level
language program as input and translates it into machine
language at a time. All the errors must be detected and
corrected. The modified source program is recompiled.
The process of translating a HLL program into machine
language using compiler is called compilation.
12. Assembler
Assembler is a program which translates an assembly language
program into machine language. e.g., TASM(Turbo ASseMbler),
MASM(Macro AsseMbler).
13. The Compiler and Interpreter are two translator programs
used to translate high level language programs into machine
language. The program written in high level language is
called source program. The corresponding program after
translation( either by Compiler or interpreter) is called object
program.
Source Program
(Assembly program)
Assembler
Object Program
(Machine language)
Translation by an Assembler
Translator
Interpreter Compiler
Object program
(Machine language)
Source Program
(HLL Language)
Translation by Compiler or Interpreter
14. Compiler Interpreter
1. Compiler converts all
statements at a time, if errors
are there, it displays all errors
as a list.
2. After Compiling the whole
program, if the program is
error free then it will execute
the program.
3. After Compilation, it creates
an executable file, using that
executable file we can run the
program any number of
times.
4. It is fast.
1. Interpreter converts line by
line, at the time of interpreting
any error is there, it displays
that error.
2. After interpreting the first line,
if it is error free then it will
execute that line.
3. It does not create any
executable file, every time we
need to interpret the program.
4. It is slow.
Differences between Compiler
and interpreter
15. Differences between Machine level language and High level
language are :
Machine level language High level language
Uses only two symbols 0(zero) and 1(one) Uses alphabets, digits, punctuations and
some special symbols
Each instruction is a sequence of 0’s and
1’s
Each instruction is English like statement
Can be directly executed Cannot be directly executed
Machine dependent Machine independent
Difficult to understand, modify write and
debug
Easy to understand, write, modify and
debug
Efficient(Fast) Less efficient(slow)
Not standardized Standardized
Called as First generation programming
language.
Called as Third generation programming
language
16. Differences between Machine level language and High level
language are :
Assembly language High level language
Using assembly language increases the efficiency of the
machine language program resulting from it.
They are to be compiled or interpreted and are less
assembly efficient compared to assembly program.
It is difficult and time consuming to write a program It is easy to write a program in this language as it uses
English like statements
An assembly language programmer must be an expert
who knows all about the logical structure of the
computer
Only the syntax of this language is to be learnt to write
programs in this language.
It is machine dependent. The program written for one
model of computer can’t be executed on another
model. In other words, it is not portable from one
machine to another
They are designed independent of the structure of
specific computer. The compiler translates the h.l.l. into
machine level language and the program written in h.l.l.
can be executed on different computers.
All hardware features available in the processor of the
computer such as registers, stacks, queues are available
here. So, he has greater flexibility in writing programs
well matched to the computer
More device independent but syntax dependent
program to be written. Just use the available syntax of
structures within the high level language.
17. •Programming : The art of writing instructions for
computer to solve the specific task is known as
programming. The output of programming is a well
defined set of instructions. This is called a program.
•Algorithm
A sequence of language independent steps which may
be followed to solve a problem.
•Flowchart
Diagrammatic representation of step for solving the
given problem
18. Algorithm
Calculating area of a circle
1. Start
2. Input radius in cm
3. Calculate, area=3.14*(radius*radius)
4. Display area
5. Stop
21. 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 a
man named Dennies Ritche.
22. Features of C
• Portability and machine Independent.
• Fast program execution.
• An extendible language.
• It is a structured programming language.
23. Historical developments
of C
Year Language Developed by Remarks
1960 ALGOL International
committee
Too general, too
abstract
1963 CPL Cambridge University Hard to learn,
difficult to implement
1967 BCPL Martin Richards at
Cambridge university
Could deal with only
specific problems
1970 B Ken Thompson at AT &
T
Could deal with only
specific problems
1972 C Dennis Ritche at AT &
T
Lost generality of
BCPL and B restored
25. Character set of the
C language
This indicates group of characters which are useful
to
write the programming statements. It contains 3
parts.
Alphabets: C language supports upper case
letters (A to Z), lower case letters from (a to z). But
both are different. C is a case sensitive language.
Numeric digits: C language contains the numeric
digits from 0 to 9.
Special Characters: The characters other than
alphabets and numeric digits are called as special
characters.
26.
27. Constants
It is a quantity which does not change its
value during executing a program.
Types of C constants
1. primary constants
2. Secondary constants
29. Integer constants
Integer Constant: These are the numbers
without decimal point or exponent.
Ex:- int a=10;
Real Constants
Float constant: These are the numbers with
decimal point or exponent.
Ex:- float pie=3.14;
30. Character Constants
Single Character Constant: It is any one of the
single character enclosed in single quotation.
Ex:- char ch=’a’;
char section=’b’;
String constant: Group of characters enclosed
with in double quotations.
Ex:- char name[20]= “Chandra”;
char nation[10]= “India”;
31. Variables
It is a name given to the memory location
which holds a particular data value that can
be change during the execution of the
program.
Variables are 4 types.
Integer variables
Float variables
Single character variables
String variables.
32. Local & global variables
1. Local variable - A variable declared inside a function.
This variable can only be used in the function.
2. Global variable - A variable declared outside of any functions.
This variable can be used anywhere in the program.
33. Rules to write variable names
•It must not exceed 31 characters.
•A variable name includes alphabets and
numbers, but it must start with an alphabet.
•It cannot accept any special characters,
blank spaces except under score( _ ).
•It should not be a reserved word.
35. key words(or)
Reserved words
Keywords are the words whose meaning has already been
explained to the C compiler. The keywords cannot be used as
variable names.
37. Data types
Data type Range Bytes Format
signed char -128 to +127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32,768 to +32,767 2 %d
short unsigned
int
0 to 65,535 2 %u
long signed int -2147483648 to
+2147483647
4 %ld
long unsigned int 0 to 4294967295 4 %lu
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to 1.7e4932 10 %lf
38. Data types
Data type Range Bytes Format
unsigned
char
0 to 255 1 %c
int -32,768 to +32,767 2 %d
long -2147483648 to
+2147483647
4 %ld
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf
39. Input and Output functions
1. Formatted I/O functions
Input function – scanf()
Output function – printf()
2. Unformatted I/O functions
Input function – gets()
Output function – puts()
40. Formatted I/O functions
Output Function
printf( ): This function is used to display the information on the
screen. It displays the variable values and the string values. This
function belongs to stdio.h.
Syntax: printf(“control_string” ,variable_list);
41. Examples:-
int a=10;
printf(“The value of a is %d”,a);
float pie=3.14;
printf(“The value of pie is %f”,pie);
char ch=‘A’;
printf(“Ch = %c”, ch);
char name[20]=“ORBIT”
printf(“Name = %s”, name);
42. Input function
scanf( ):- It is an input function, that is used to accept the input from
the keyboard(user) at the time of executing a program. This function
belongs to stdio.h .
Syntax: scanf(“control_string”, &variable_list);
44. Unformatted I/O functions
Output function
puts()
This function is used to execute only string
values.
Syntax:- puts(string_variable);
Ex:- char name[20]=“ORBIT”;
puts(name);
46. Writing C Programs
➢ A programmer uses a text editor to create or modify
files containing C code.
➢ Code is also known as source code.
➢ A file containing source code is called as source file.
➢ After C source file has been created, the programmer
has to invoke C compiler in order to execute (run)
the program.
48. Stages of Compilation
Preprocessing
Performed by a program called the preprocessor.
Modifies the source code (in RAM) according to preprocessor directives (preprocessor
commands) embedded in the source code.
Strips comments and white spaces from the code
49. Compilation
▪Performed by a program called the compiler.
▪Translates the preprocessor-modified source code into object
code (machine code).
▪Checks for syntax errors and warnings.
▪Saves the object code to a disk file, if instructed to do so (we
will not do this).
▪If any compiler errors are received, no object code file will be
generated.
▪An object code file will be generated if only warnings, not errors,
are received.
50. Linking
▪Combines the program object code with other object code to
produce the executable file.
▪The other object code can come from the Run-Time Library,
other libraries, or object files that you have created.
▪Saves the executable code to a disk file. On the Linux system,
that file is called a.out.
▪ In case any linker errors are received, no executable file will be generated.
51. Structure of a C program
/* Comment line section */ → Documentation
optional
# header file section → when we use
predefined functions
main( )
{
Declaration part; → To declare the
variables which are
used in the program
Initialization part; → To initialize i.e. giving
values to the variables
Execution part; → To execute as output
}
52. Comment Line Section
In order to give documentation for a
program comment line is includes. It is
enclosed in between a /* */ because in
the compilation process these lines are not
includes.
Comments are two types
Single line comment - // ------------
Multi line comment - /* -----------
-----------*/
53. Header file section
‘C’ program consists of predefined
functions for that reason we should
include the files in which they are
defined. Which are saved with .h as
extension.
54. Ex:- # include <stdio.h>
stdio.h stands for standard input output
. header file
# - It is used to tell the preprocessor to
get the header file to included in the
source code.
Include - it is used to include header file
specified in your source code.
55. ▪When we write our programs, there are libraries of
functions to help us so that we do not have to write the
same code over and over.
▪Some of the functions are very complex and long.
Libraries of functions make it easier and faster to write
programs.
▪Using the functions will also make it easier to learn to
write a program.
stdio.h
56. Main( )
▪The compilation and the running process is done with
the help of Main( ). Every program must have a function
called main. This is where program execution begins.
▪main() is placed in the source code file as the first
function for readability.
▪The parentheses following the reserved word “main”
indicate that it is a function.
57. Declaration part
This area is where we declare all the variables
which are used in the program.
Initialization part
Here, we give values to the declared variables.
Executable part
The output is seen from the executable part.
By using output functions.
58. getch()
It is a predefined function available in <conio.h>
library which reads the character from the
keyboard without printing on the monitor.
getche()
It is a predefined function available in <conio.h>
library which reads the character from the
keyboard with printing on the monitor.
clrscr()
It is a predefined function available in <conio.h>
library which clears the previous contents of user
screen window.
59. Execution of C Program
Steps to be followed in writing and running a C program.
Creation of Source Program
Create a C program code in Turbo C Editor.
Compilation of the Program
Press Alt + F9 for compilation.
Program Execution
In Turbo C environment, the RUN option will do the compilation
and execution of a program.
Press Ctrl + F9 for execution of the program.
61. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations like
addition, subtraction, multiplication, division and remainder.
Operator Name Meaning Example
+ Plus Addition a+b
- Minus Subtraction a-b
* Asterisk Multiplication a*b
/ Slash Division a/b
% Modulator Reminder a%b
62. Relational operators
These operators are used to check the relation between the
values.
Note:- When we used relational operators the output is seen
in the form of 0 or 1. If the relation is true the output will be in
1. If the relation is false the output will be 0.
Ex:- a=10
b=5 Operator Example Result
> a>b 1
< a<b 0
>= a>=b 1
<= a<=b 0
= = a==b 0
!= a!=b 1
63. Logical operators
logical operators are used to combine two or more relational
expressions. C language contains 3 logical operators.
Operator Name
&& And
|| Or
! Not
64. && : (And)
Statement:- When all arguments are true then only the total
condition is true. Any one of the arguments is false the total
condition is false.
Expressinn1 && Expression2 Result
T T T
T F F
F T F
F F F
Truth table
65. || : (Or)
Statement:- When one of the argument is true the total
condition is true. If all the arguments are false then only the
condition is false.
Expression1 || Expression2 Result
T T T
T F T
F T T
F F F
Truth table
66. ! (Not)
Statement:- This is the negative operator, we can place the ‘not’
operator before any one of the condition. It returns the opposite
result of the condition. i.e. It converts true to false and false to
true.
! expression result
T F
F T
Truth table
67. Assignment operator
C language contains equal to (=)
operator to assign a value or
expression into a variable.
Ex:- a=10;
b=a+10;
68. Increment and decrement operators
1. Increment operator (+ +)
Increment operator is used to increase the value by 1.
• Pre Increment( ++a)
First the value will be incremented, and then the
new value will be executed.
Ex:- printf(“%d”,++a);
• Post Increment(a++)
First the value will be executed and then one value
will be incremented.
Ex:- printf(“%d”,a++);
69. 2) Decrement operator ( - - )
Decrement operator is used to decrease the
value by 1.
• Pre Decrement (--a)
If the value is decreased before execution it is
called as pre decrement.
Ex:- printf(“%d”,--a);
• Post Decrement (a--)
If the value is decreased after the execution it is
called as post decrement.
Ex:- printf(“%d”,a--)
70. Conditional operators
C language contains conditional operators (?, : ). By
using conditional operators we can get the output in the
form of statements also. ? and : are called as
conditional or ternary operators.
Syn:- (condition) ? printf(“value 1”) : printf(“ value 2”);
Ex:- (a>b) ? printf(“a greater than b”):printf(“a is
less than b”);
If the condition is true then value1 will be executed, if
the condition is false then value2 will be executed.
71. Size of Operator
This operator is used to find memory size of the variable or
datatype.
Syntax:- sizeof(variable or datatype);
72. Complement operator (~)
It gives the ones complement of the number.
✓ Add one to the given number
✓ Change it's sign
73. Bitwise operators
Bitwise operators compares internal binary
format of the number. These operators are
applicable in between integer variables only.
• Bitwise AND &
• Bitwise OR |
• Bitwise XOR ^
• Bitwise Left shift <<
• Bitwise Right shift >>
76. Bitwise Left shift operator (<<)
Bitwise Left shift operator shifts the number of bits
toward left side specified number of times and fills right
side bit with zero.
Or
Multiply the given number by specified number of
powers of 2.
int a=10,b;
b=a<<1; a*2=10*2=20
b=a<<2; a*2*2=10*2*2=40
b=a<<3; a*2*2*2=10*2*2*2=80
77. Bitwise Right shift operator (>>)
Bitwise right shift operator shifts the number of bits
towards right side specified number of times and fills left
side bits with zero.
Or
Divide the given number by specified number of of
powers of 2.
int a=10,b;
b=a>>1; a/2=10/2=5
b=a>>2; a/(2*2)=10/(2*2)=2
b=a>>3; a/(2*2*2)=10/(2*2*2)=1
78. Hierarchy (precedence) of
operators
▪ The priority or precedence in which the operations of an arithmetic
statement are performed is called the hierarchy of operators.
▪ This precedence is used to determine how an expression involving
more than one operator is evaluated.
▪ The operators of at the higher level of precedence are evaluated
first.
▪ The operators of the same precedence are evaluated either from
left to right or from right to left, depending on the level. This is
known as the associativity property of an operator.
79. PRECEDENCE OF OPERATORS
(Arithmetic operators only)
Operator Description Associativity
*
/
%
+
-
Multiplication
Division
Modulo
Addition
Subtraction
Left to right
“
“
“
“
80. Conditional statements
The statements which are executed
according to some condition are called
as conditional statements.
They are 3 types
•If
•If-else
•Ladder if
•Nested if-else
81. The If statement
Statement: This statement is used to perform conditional operations.
Syntax:
if(condition)
{
statement;
}
If the condition is true then statement will be
executed. If the condition is false then it will not
execute statement.
82. Simple if Flow chart
if(condition)
Statement(s);
Next statements(if any)
False
True
83. If-else
The general form of if-else statement is…
Syntax:
if(condition)
{
statement1;
}
else
{
statement2;
}
If the condition is true then statement1 will be executed, if the
condition is false then statement2 Will be executed.
85. Nested if-else
When a series of conditions are involved, we can use more than
one if-else statement in nested form.
Syntax:
if (condition)
{
statements;
}
else if (condition)
{
statements;
}
else
{
statements;
}
86. Switch statement
It is used to execute one of the options from no. of options. It is also
called as multi branching statement.
Switch (expression)
{
case value:
statements;
case value:
statements;
default:
statements;
}
The expression value may be int or character type. The switch
statement evaluates the expression. It will select one of the cases
based on expression. It will execute default statements when no
case is selected.
87. Break statement
▪This control statement can be used in loops and switch
statements.
▪It is used to terminate the loop when a specified condition is
satisfied.
▪The control comes out of the loop and the following statements
are executed.
▪In switch statements, the control comes out of the switch
statements and the following statements are executed.
89. Flow chart
switch(expression)
Case 1:
Match ? Statement(s); break;
case 2:
Match ? Statement(s); break;
case n:
Match ? Statement(s); break;
default Statement/s;
Next statement
90. The exit( ) Function
▪ The exit( ) is a function in the standard library of C.
▪ This function causes immediate termination of the program and
execution control returns to the operating system.
91. Control Statements
Unconditional control statements:
▪ goto statement
▪ break statement
▪ continue statement
Looping/Repetitive/Iteration control statements:
▪ for statement
▪ while statement
▪ do-while statement
92. Goto statement
Syntax:
goto label;
.
.
label :
Statement(s);
This is an unconditional control statement to tell the compiler
from where the next statements has to be executed.
In other words, program control directly Jumps to some
location in a program with the help of this statement.
93. Continue statement
▪ This control statements are used in the loops.
▪ During execution if a continue statement is encountered all the
following statements will be terminated and the control will go to the
next iteration of the loop without executing the following statements in
the loop.
94. Loop control structures
LOOPING:- The process of executing a
block of code repeatedly is called as looping. They are 4 types
• For loop
• While loop
• Do while loop
• Nested for loops
95. All the four loops are used for same process but
there syntax is different.
All the four loops have 3 steps is common.
•Initialization:- It is the starting point of the loop
process.
•Condition:- In this step logical test is applied. It is
true the block of code is executed. Till the
condition fails.
•Increment / Decrement:- In order to proceed or
stop the loop increment or decrement is required.
96. For loop
It is used to execute a set of statements
repeatedly as long as the given condition is true.
Syntax:-
for (initial value ; test condition ; increment /decrement)
{
statements; //body of for loop
}
Ex:- for(i=1; i<=10; i++)
97. For loop
It is used to execute a set of statements
repeatedly as long as the given condition is true.
Syntax:-
for (initial value ; test condition ; increment /decrement)
{
statements; //body of for loop
}
Ex:- for(i=1; i<=10; i++)
Initialization expression
Test expression
Body of the loop
Update expression
Next statements (if any)
False
True
Flow chart
98. While loop
It is used to execute a set of statements
repeatedly as long as the given condition is true.
Syntax:- while (condition)
{
statements; //body of while loop
}
First the condition is tested, if it is true the body of
the while loop will be executed first time, again the
control is transferred back to while loop and
checks the condition, If it is false the control will
come out of the while loop.
100. Do While Loop
This statement executes a set of statements as
long as the condition is true.
Syntax: do
{
statements;
statements;
}
while(condition);
This is similar to while loop, but the difference is
this loop will execute the statements at least once.
102. Nested for loop
In order to get the output in the form of
rows and columns we use nested for
loops. If the outer loop condition is true
it enters into the inner loop. Till the
outer loop condition is false the looping
process is continued.
103. ARRAYS
An array is contiguous area in the memory
referred by a common name. The array allows
the storage of a number of data values in one
variable. Each array element i.e. each
individual data item is referred by specifying the
array name followed by one or more subscripts.
Each subscript is enclosed with square
brackets. Each subscript indicates the position
of the element in the array. Arrays are divided
into 2 types.
1. Single dimensional array or One dimensional
array
2. Two dimensional array
104. One dimensional array
The array which is using only 1 subscript is known as one dimensional
array. In one dimensional array the elements are represented in single
row or single column.
Syntax:- Data_type variable name[subscript]
Ex:- int a[5]={40, 50, 70,90,100};
Float b[4]={20.5, 40.9, 45.7, 23.8};
Char c[5]={‘S’,’O’,’F’,’T’};
105. Multi dimensional array
The array which is using two subscripts is known
as 2 – dimensional array
Syntax:-
Data_type variable_name [subscript][subscript]
Ex:- int a[2][2]={{1,2,},{3,4}};
The above declaration states that ‘a’ is an two
dimensional Array variable in which the values
are stored in 2 rows and in 2 columns
106. String Functions
A character array is defined as a string. A group of characters is
called a string. We cannot handle with string same like integers or
real values. For example, if you want to compare an integer value
with another int value. We can do that by using = = operator. But the
same procedure cannot be applied for strings. In order to handle
with strings there are some string handling functions.
107. Strlen( )
This function is used to find the length of a string.
Syntax:- strlen(string);
Note:- When we are using string functions we have to include
a header file <string.h>
108. Strcpy( )
This function copies a string from
one string variable to another string
variable.
Syntax:-
strcpy(target_string , source_string);
109. Strcnpy( )
Copies at most maxlen characters of source to destination.
(or)
Strncpy copies up to maxlen characters from source into destination.
Syntax:-
strncpy(dest_string, src_string, maxlen);
110. Strcat( ):- (String concatenation)
This function adds a string at the end
of another string
Syntax:- strcat(string1,stirng2);
111. Strncat( )
Strncat copies at most maxlen characters of source to the end of
destination and then appends a null character.
Syntax:-
strncat(dest_string, src_string, maxlen);
112. Strcmp( ):- (String comparison)
This compares one string with
another string.
Syntax:-
strcmp(string1, string2);
113. Stricmp( )
This function is similar to strcmp, but
this function ignores the case
sensitivity.
Syntax:-
stricmp(string1, string2);
114. Strchr( )
Scans a string in the forward direction, looking for a specific character.
Syntax:-
strchr(string, character);
116. Strupr( )
This function converts the given string
into upper case (capital letters)
Syntax:- strupr(string);
117. Strlwr( )
This function converts the given string
into lower case.
Syntax:- strlwr(string);
118. Single character
1) toupper( ) :- This function converts a single
character to upper case.
Syntax:- toupper(character);
2) tolower( ) :- This function converts a single
character to lower case.
Syntax:- tolower(character);
•Note:- when we are using single character
functions we have to include a header file
<ctype.h>
119. Functions
A function is a set of statements that
performs a specific task. Functions
are 2 types
1. Pre defined functions or Standard
functions
2. User defined functions
120. Pre defined Functions
These functions which are already
defined (in built) along with C are
called as pre defined functions.
Ex:-
printf( ), scanf( ), pow( ), strlen( ),
strcpy( ), toupper( ),……
121. User defined functions
These functions defined by the user in order to work with
complex program and to develop the efficiency of the program
are called as user defined functions. In order to define a function
we have 3 types.
• Function definition
• Function prototype
• Invoking a function
123. a. Return type:- Return is called as data type mention according
to the type of value returned from the function.
Note:- When there is no return statement we should mention
void as a default return type.
b. Function name:- Here we mention the name of a function. It
is just like a variable name
124. C. Specification of parameters:- The specify
the number of values are variables taken by
the called function from main program.
Note:-The parameters mentioned in the
main program for calling a function are
called as actual parameters.
The parameters mentioned in the function
which are received from the main program
are called formal parameters.
125. ➢ Block of code
The code which is written with in the braces of a function is
called as block of code.
➢ Return statement
This is the last statement of a function it is to return a value from
the user defined function to the main program.
126. It is declared in the main program about the function.
Note:- Function prototype is optional if the function is defined before
the main. It is compulsory if the function is defined after the main.
Function Prototype
127. Invoking a Function
From here we call the function. A
function as no live when it is not
Invoked.
128. Uses of functions
▪Dividing the task into sub tasks.
▪If a particular task is repeated number of times in a
program we can create a function and call that function
wherever we want.
▪Whenever a function is called in a statement the control
will be transferred to that called function. After
executing all statements it returns back to the calling
function.
▪The length of the source program will be reduced by
using functions at appropriate places.
▪A function can be used in other programs also.
129. Syntax of a function / function definition
Return_type function_name (argument list)
{
declarations;
statements;
-------------
return value;
}
Function declaration
Return_type function_name (argument list);
131. Pointers
Pointer is a memory location or a variable which
stores the memory address of another variable in
memory
Pointers are much more commonly used in C & C++
than in many other languages (such as BASIC,
Pascal, and certainly Java, which has no pointers).
132. What are pointers for?
Here are some common uses:
• Accessing array elements
• Passing arguments to a function when the
function needs to modify the original argument
• Passing arrays and strings to functions
• Obtaining memory from the system
• Creating data structures such as linked lists
Problem solving using C++
133. Memory map
i
i=10
65524
‘i’ - Location name
10 - Value at location
65524 – Location number or address.
Declaration of a pointer:
Int a, *x;
Float b, *y;
Char c, *z;
Pointer variable is same as normal variable. But a *
symbol is added before the variable.
10
134. Usage of pointers
As pointers work with addresses accessing through
addresses first then through a normal variable. When
the values are called with the help of address by a
function the modifications done to the variables
shows the reflection.
Indirectly we can get the value of a variable by using
*. So * is called as indirection operator. Whatever the
data type may be a pointer variable occupies only 2
bytes of memory.
135. Parameter passing mechanism
•Call by value
•Call by address
•Call by value:- When the function calls the
parameters by taking values the modifications done
to the parameters by the function doesn’t reflect.
•Call by address:- When the parameters are called
by address the changes done to the values within
the function shows the reflection.
136. Structures
Structure is a user defined data type. A structure is a
collection of variables that is referenced under a single
name. A structure contains a number of data types
grouped together. These data types may or may not be
of the same type.
Definition of a structure: - Structure is a collection of
variables which are of dissimilar data types.
137. Declaring a structure:-
struct <structure name>
{
structure element 1;
structure element 2;
---
structure element n;
};
structure variable;
138. Ex:- struct book
{
int pages;
char author[20];
float price;
};
•In the above declaration book is called Tag
name and pages, author, price are called as
data members of the structure
139. Declaring a variable for a structure
Ex:- struct book b;
Here, ‘b’ is a variable of a structure to
which we can give 3 details.
The data members of the structure are
called by the variable using ( . ) operator.
For example:- b.pages
b.author
b.price
140. Features of a structure
With the help of structure we can maintain a data in a systematic
order. We can handle a structure within a structure. And also we can
call the structure variable through functions and Arrays also can be
implemented through structures. Pointer to structure is also possible.
141. Unions
A union is similar to structure, but the difference is
1. The size of the structure variable is sum of all the
members size, but union size is the maximum
member's size.
2. In structure we can see all members at
a time, but in union we can see only one
member at a time because when we store
the second member, first value will be
over written.
142. Files
File is a collection of data to be stored
permanently in the disk space. The disk may
be either hard disk or floppy disk or
compact disk
143. Difference between Array and File
1.File is a collection of data. The data may be of
similar or dissimilar type.
Array is collection of elements of similar
datatype.
2. File size is unlimited.
Array size is limited.
3.File data is stored permanently in the disk space.
Array data is stored temporarily in the RAM.
144. To Create a File pointer
FILE is a macro which is used to create
a file pointer which holds the address of
some file.
Syntax:- FILE * file_pointer;
Example:- FILE *fp;
145. To create or open the file
fopen()
It is a predefined function available in
<stdio.h> which is used to create or
open the file.
Syntax:-
file_pointer=fopen(filename,"Modes");
146. Modes are nothing but permissions to create or open the file.There are 6
modes.
r:- fp=fopen(“Filename.extn","r");
If the file is already exist then it opens the
file with read permission.
r+ :- fp=fopen(“Filename.extn","r+");
If the file is already exist then it opens the
file with read and write permission.
147. W :- fp=fopen(“Filename.extn","w");
If the file is already exist then it opens the file with write
permission by deleting previous data else creates the new file.
w+:- fp=fopen(“Filename.extn","w+");
If the file is already exist then it opens the file with read and write
permission else creates the new file.
148. a :- fp=fopen(“Filename.extn","a");
If the file is already exist then it opens
the file with add permission else creates
the new file.
a+ :- fp=fopen(“Filename.extn","a+");
It creats or opens the file with read write
and append permission.
149. To close the file
fclose()
It is a predefined function available in <stdio.h> which is used
to close the specified file.
Syntax
fclose(file_pointer);
Example
fclose(fp);
150. getchar()
getchar() is a macro that gets a character
from stdin
Syntax: - int getchar(void);
putchar()
putchar() is a macro that outputs a
character on stdout
Syntax: -int putchar(int c);
151. fwrite()
fwrite() appends a specified number of equal-sized data items to
an output file.
Syntax:-
fwrite(const void *ptr, size_t size, size_t n, file_pointer);
152. fseek()
Repositions the file pointer of a stream
fseek() sets the file pointer associated with a stream to a new
position.
Syntax: -
fseek(file_pointer, long offset, int whence);
153. getc()
getc() is a macro that gets one character
from a stream
Syntax: - getc(file_pointer);
putc()
putc() is a macro that outputs a character
to a stream
Syntax: - putc(character, file_pointer);
154. fputs()
fputs copies the null-terminated string s to the
given output stream. It does not append a newline
character, and the terminating null character is not
copied.
Syntax: - fputs(character, file_pointer);
fgets()
fgets reads characters from stream into the string.
It stops when it reads either n - 1 characters or a
newline character, whichever comes first.
Syntax: - fgets(character, file_pointer);
155. To write the character into the file
fputc()
It is a predefined function available in <stdio.h>
which is used to write the specified character into
the file.
Syntax
fputc(charactervariable,file_pointer);
Example
char ch='A';
fputc(ch,fp);
156. To read the character from the file
fgetc()
It is a predefined function available in <stdio.h>
which is used to read the character from file.
Syntax
charactervariable=fgetc(file_pointer);
Example
char ch;
ch=fgetc(fp);
157. ftell()
If the file is binary, the offset is measured in bytes from the
beginning of the file
Syntax: - ftell(file_pointer);
158. To delete the file
remove()
It is a predefined function available in <stdio.h> which is used to
remove the specified file.
Syntax
remove(filename);
Example
remove("Sachin");
159. To rename the file
rename()
It is a predefined function available in <stdio.h> which is used to
rename the file.
Syntax
rename(old filename,new file name);
Example
rename("Sachin","Mahesh");
160. To write the different types of data into the file
fprintf()
It is a predefined function available in <stdio.h>
which is used to write the different types of data
into the file .
Syntax
fprintf(file_pointer,"formatspecifier",variable);
Example
char c='@';
int i=10;
float f=34.56
fprintf(fp,"%d%c%f",i,c,f);
161. To read the different types of data from the file
fscanf()
It is a predefined function available in <stdio.h> which
is used to read the different types of data from the file
.
Syntax
fscanf(file_pointer,"formatspecifier",Address of
variable);
Example
char c;
int i;
float f;
fscanf(fp,"%d%c%f",&i,&c,&f);
162. storage classes
They are 4 types
1) automatic storage class
2) register storage class
3) static storage class
4) external storage class
163. Automatic storage class
The features of a variable defined to have an
automatic storage class are as under
▪Storage : Memory
▪Default initial value : Garbage value
▪Scope : Local to block in which the variable is
defined
▪Life : Till the control within the block in which the
variable is defined
164. Register storage class
▪ Storage : CPU registers
▪ Default initial value : Garbage value
▪ Scope : Local to block in which the variable is defined
▪ Life : Till the control within the block in which the variable is defined
165. Static storage class
▪ Storage : Memory
▪ Default initial value : Zero
▪ Scope : Local to block in which the variable is defined
▪ Life : Value of the persists between different function calls
166. External storage class
▪ Storage : Memory
▪ Default initial value : Zero
▪ Scope : Global
▪ Life : As long as the program execution doesn't come to an end
168. Algorithm for cooking rice
Step 1 : Take a vessel and a cup of rice in it.
Step 2 : Wash the rice until it is clean.
Step 3 : Pour two cups of water into vessel.
Step 4 : Light the stove and keep the vessel in
cooker and put it on stove.
Step 5 : Close the cooker and wait until rice cooked
properly.
Step 6 : Unlight the stove.
169. Algorithm for find sum of two numbers
Step 1 : Start
Step 2 : Read a, b;
Step 3 : Sum=a+b;
Step 4 : Display sum
Step 5 : Stop
170. Algorithm for find the maximum of two numbers
Step 1 : Start
Step 2 : Read a, b
Step 3 : If a>b then go to step-4 else go to step-5
Step 4 : Display A and go to step-6
Step 5 : Display B
Step 6 : Stop
171. Algorithm to find total and average marks of three subjects
Step 1 : Start
Step 2 : Read a,b,c
Step 3 : Sum=a+b+c
Step 4 : Average =sum/3
Step 5 : Display sum and average
Step 6 : Stop
172. Algorithm to find maximum of 3 numbers
Step 1 : Start
Step 2 : Read a,b,c
Step 3 : If a>b and a>c then go to step-4 else go to
step-5
Step 4 : Display A
Step 5 : If b>c then go to step-6 else go to step-7
Step 6 : Display B and go to step-8
Step 7 : Display C
Step 8 : Stop
173. Algorithm to find whether the given number is prime number
or not
Step 1 : Start
Step 2 : Read a
Step 3 : i=1 and count=0
Step 4 : If i<=a goto step-5
Step 5 : If a%i==0 then count+1 else goto step-6
Step 6 : i=i+1 and goto step-4
Step 7 : If count==2 goto step-8 else goto step-9
Step 8 : Display ( It is prime number)
Step 9 : Display (It is not a prime number )
174. Flow Chart
Graphical representation to solve a certain
problem is said to be flow chart.
Some of the graphical symbols are given below.
1) Terminator 4) Input/Output
2) Process 5) Connector
3) Decision 6) Flow
175. Draw a flow chart to display some of two numbers?
Start
Read a,b
Sum=a+b
Display sum
Stop
176. Draw a flow chart to find total and average marks of 3 subjects
Start
Read a,b,c
Sum=a+b+c
Avg=sum/3
Display sum & avg
Stop
177. Draw a flow chart to find maximum of two numbers
Start
Read a,b
If a>b
Display A Display B
Stop
Yes No
178. Draw a flow chart to find the maximum of three numbers
Start
Read a,b,c
Display A
Stop
If
a>b
a>c
If
a>b
a>c
Display B Display C
Yes
Yes
No
No
179. Start
Read avg
Display A
Stop
If
Avg>75
Display B Display C
If
Avg>65
Avg<75
Yes No
No
Yes
Draw a flow chart to find grade of the student based on the following
conditions – avg>75→A, avg>65 && avg<75→B, avg<65→C
180. Draw a flow chart to find whether the given number is prime or not
Start
Read a
i=1
Count=0
if
i<=a
Displays
A is prime
Displays
A is not prime
i=i+1
Count=
count+1
Stop
Yes
Yes
No Yes
No
No
if
count==2
if
a%i=0
181. Pseudo code
To overcome some of the disadvantages present
in algorithm and flow chart Pseudo code was
introduced.
Pseudo code means extending some of the
confusing factors present in the algorithm. It is a
combination of both general syntax and English
words.
182. Addition of two numbers
Begin
Define : a,b,c
Read : a,b
Compute : c=a+b
Display : c
End
183. Write a Pseudo code to find maximum of two numbers
Begin
Define : a,b
Read : a,b
If : a>b
Display : a
Else
Display : b
End if
End
184. Write a Pseudo code to find grade of the student based on
average
Begin
Define : avg
Read : avg
If : avg>75
Display : ‘A’
Else
If : avg>65 && avg<75
Display : ‘B’
Else
Display : ‘C’
End if
End if
end
185. Write a Pseudo code to find grade of the student based
on average.
Begin
Define : a,I,count
Read : a
SET : i=1, count=0
For : I from 1 to a
IF : a%i=0
Compute : count = count+1
End IF
Compute : i=i+1
End For
IF : count==2
Display : ‘a’ is prime
Else
Display : ‘a’ is not prime
End IF
End