Sr.
Key DOS Windows
No.
Definition DOS stands for Disk Windows stands for Windows,
1
Operating System. no specific form.
Tasking DOS is single tasking Windows is multi-tasking OS.
2
Nature OS.
Power DOS consumes quite Windows consumes high power.
3
Consumption low power.
Memory DOS memory Windows memory requirements
4 consumption requirements are quite are quite high as compared to
low. DOS.
Networking DOS has no support for Windows supports networking.
5
support networking.
Usage DOS is complex in Windows usages is user-friendly
usage. You need to and is quite simple to use.
6
remember commands to
use DOS properly.
User interface DOS is command line Windows is GUI based OS
7
based OS.
Multimedia Multimedia is not Windows supports multimedia
8
supported in DOS. likes games, videos, audios etc.
Speed DOS command Windows operations are slower
9 execution is faster than as compared to DOS.
Windows.
Multiwindow DOS supports single Windows supports multiple
10
window at a time. window at a time.
r. Key GUI CUI
No.
Interaction User interacts with User interacts with
computer using computer using
1
Graphics like images, commands like text.
icons.
Navigation Navigation is easy. Navigation is
2
difficult.
Peripherals Keyboard, mouse or Only keyboard.
3 used any other pointing
device.
Precision GUI has low precision. CUI has high
4
precision.
5 Speed GUI is of low speed. CUI is of high speed.
Usage Usage is easy. Usage is difficult,
6
requires expertise.
Memory High memory Low memory
7
requirement requirement. requirement.
Flexibility Highly flexibile user Little flexibile user
8
interface. interface.
Customize GUI is highly CUI appearance is
9 customizable. not easily
changeable
INTRODUCTION
As is already seen, a computer cannot do anything on its own.
It has to be provided a detailed set of instructions to perform a
specific task to achieve a desired goal, this set of instructions,
which are written in the form which is understood by the
computer, is known as a computer program. The program
controls the processing of the computer.
CLASSIFICATION OF SOFTWARES
Software means a set of programs, procedures and other
associated documentation which describes what the program
does and how it is to be used.
Hardware and software both have to work together. A number
of software’s can be run on the same hardware to perform
different types of jobs. The software acts as an interface
between the user and the computer.
Software is mainly classified into:
Application Software
System Software
Application Software:
Application software is a program or a set of programs which
are written to carry out a specific [Link]. Payroll,
financial accounting etc. Nowadays special application
software or packages for specialized areas like drawing,
engineering, manufacturing, banking, and publishing are
available. The set of programs which together make an
application package are called application programs.
System Software:
The systems software controls the working of the computer
system. It helps the user to use the computer by allowing him
to communicate with the system. System software controls the
working of other software’s, hardware, hardware devices like
printers, memory, CPU etc. Thus, they make the operation of
the computer more efficient. The programs included in system
software are systems programs. Without the systems
programs, it would not be possible for the application
programs to work on the computer. The systems software is
generally provided by the manufacturer of the computer
hardware. Without the systems software the hardware would
not work.
Utility Software:
These are a set of programs or tools which are used in
program development or for performing limited tasks, eg.
scandisk.
Firmware:
With the advances in technology it is now possible to make
the software available on ROM (Read Only Memory) chips.
These chips, which form a part of the software, have the
programs in them. Thus programs available on hardware are
called Firmware. Today not only systems software, but even
some dedicated application programs are being made
available on firmware.
PROGRAMMING LANGUAGES
A programming language is a means of communication for
the user to communicate with the computer system. The
programming language is a set of instructions which tells the
computer what to do. This is a language which is understood
by both man and machine. There are a number of
programming languages. However all these languages are
designed to perform at least certain basic instructions and
operations; These instructions and operations are :
- Input/output operations,
- Arithmetic operations - mathematical operations like
addition, subtraction,
- Logical operations - Comparison for equality, inequality etc.
- Movement of instructions and data to and from the CPU.
To use the programming language, the programmer has to
strictly follow the rules of the language including all commas,
punctuation marks etc. otherwise the program will not be
understood by the computer. Thus, although the programming
languages are much smaller and simpler than the natural
languages, they have to be used with the greatest accuracy.
Types of programming languages
a) Machine Language
b) Assembly Language
c) High Level Language
a) Machine Language:
This is the only language which is understood by the
computer. This is the language nearest to the machine. In this
language the programs are written in binary code i.e. the
instructions are made only by a combination of binary digits 0
and 1. Machine language may vary from machine to machine
depending upon the computer architecture. Machine
languages execute the fastest since they are immediately
understood by the computer. No translation of the programs is
required. Also they make efficient use of primary memory.
But it is very difficult to program in this binary or machine
language. It is also very tedious and time consuming, since all
the instructions have to represent as a series of Os and 1 s.
Therefore there is always a possibility of errors.
b) Assembly Language:
The Os and 1 s of the machine language were substituted by
letters and symbols in assembly languages. The assembly
languages use mnemonics (memory aid) in place of operation
codes. The language uses symbols instead of numbers to write
programs. A program written using such symbols in the
assembly language is called the source program. The program
written in assembly language has to be converted into
machine language for use by the computer. This is achieved
with the help of the assembler.
The assembler is a system program which is supplied by the
manufacturer. It converts the assembly program into a
machine readable program and the resulting program is called
the object program. Thus the input to the assembler is the
source program and the output of the assembler is the object
program. The assembler translates each assembly language
instruction into a corresponding machine code.
It is relatively easy to write programs in assembly language as
compared to machine language. They can be also modified
relatively easily. However, like the machine language,
assembly languages are also hardware dependant. When the
machine changes, the program has to be changed.
Since the machine language and assembly language both are
dependent on the hardware, they are referred to as low level
programming languages. Both these languages require a deep
understanding of the internal structure of the computer.
1. High Level Languages :
Higher level languages make use of English like words and
statements and mathematical symbols for instructions. Higher
level languages make programming easier, since they are
relatively easy to learn. Less time is required to write
programs in high level languages. The programmer is not
required to know the detailed working of the computer system
in order to program in a high level language.
They are machine independent. Higher level languages are
also known as problem oriented languages. However a high
level language is not directly understood by the computer. It is
required to be translated into machine language. Therefore
they generally execute more slowly and require more memory
than the same program written in assembly language.
The programs which are used to translate programs written in
high level language into machine language are known as
translators.
The types of translators are:
I) Compiler
II) Interpreter
I) Compiler:
The compiler translates the entire source program into
machine language program at once. The source code remains
intact. Once a program is compiled it can be run as many
times as required, without being required to be recompiled. A
compiler can translate only those programs which have been
written in the language for which it is designed. Also each
machine has to have its own compiler.
A compiler is a program which normally resides on the
secondary storage device. It gets loaded into the CPU when
the source program is to be translated.
A compiler checks for errors like illegal symbols, statements
etc. during compilation and gives out a list of error messages
at the end of execution. This is a very valuable aid to the
programmer to correct the programs. However, the compiler
is incapable of detecting any logical errors in the program.
II) Interpreter:
The interpreter is the program which translates a high level
language program into machine language as follows :
- it takes one statement from the high level language program
- translates it into a machine instruction and the instruction is
immediately executed.
Since the program is translated statement by statement, the
machine level program of the source program is not stored
anywhere in memory.
Therefore, the program has to be interpreted every time when
it has to be run. Thus no object code is generated. The
interpreted programs are generally slower than compiled
programs. However, if any changes are made in the source
program it can interpret only those statements and it is not
required to compile the entire program again. Interpreters are
relatively easy to write and smaller in size as compared to
compilers.
Thus assemblers, compilers and interpreters are systems
software which translate the source program into object
program i.e. program which can be understood by the
computer. These translators are also known as language
processors.
c) Fourth Generation Languages (4 GL):
Programming languages are sometimes classified with
generations - from the lowest to the highest.
The First Generation - Machine Language
The Second Generation - Assembly Language
The Third Generation – High level Language
Unlike procedural languages which require that a programmer
writes all the steps to complete the program and generate
output, which are related to a more structural approach
towards programming. It is the first standardized language.
Algorithm in C Language
Algorithm is a step-by-step procedure, which defines a set of
instructions to be executed in a certain order to get the desired output.
Algorithms are generally created independent of underlying
languages, i.e. an algorithm can be implemented in more than one
programming language.
From the data structure point of view, following are some important
categories of algorithms −
• Search − Algorithm to search an item in a data structure.
• Sort − Algorithm to sort items in a certain order.
• Insert − Algorithm to insert item in a data structure.
• Update − Algorithm to update an existing item in a data
structure.
• Delete − Algorithm to delete an existing item from a data
structure.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should
have the following characteristics −
• Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
clear and must lead to only one meaning.
• Input − An algorithm should have 0 or more well-defined
inputs.
• Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of
steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step
directions, which should be independent of any programming
code.
How to Write an Algorithm?
There are no well-defined standards for writing algorithms. Rather, it
is problem and resource dependent. Algorithms are never written to
support a particular programming code.
As we know that all programming languages share basic code
constructs like loops (do, for, while), flow-control (if-else), etc. These
common constructs can be used to write an algorithm.
We write algorithms in a step-by-step manner, but it is not always
the case. Algorithm writing is a process and is executed after the
problem domain is well-defined. That is, we should know the
problem domain, for which we are designing a solution.
Example
Let’s try to learn algorithm-writing by using an example.
Problem − Design an algorithm to add two numbers and display
the result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Algorithms tell the programmers how to code the program.
Alternatively, the algorithm can be written as −
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − print c
Step 5 − STOP
In design and analysis of algorithms, usually the second method is
used to describe an algorithm. It makes it easy for the analyst to
analyze the algorithm ignoring all unwanted definitions.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf(“ hello world welcome to the world of programming”);
getch();
}
Data types in C Language
Data types specify how we enter data into our programs
and what type of data we enter. C language has some
predefined set of data types to handle various kinds of
data that we can use in our program. These data-types
have different storage capacities.
How the data will organized into the memory.
C language supports 2 different type of data types:
1. Primary data types:
These are fundamental data types in C namely
integer(int),
floating point(float),
character(char)
and void. Valueless 0
ex:-
int a=10;
char c;
float p=10.21;
2. Derived data types:
Derived data types are nothing but primary
datatypes but a little twisted or grouped together
like array, stucture, union and pointer. These are
discussed in details later.
Data type determines the type of data a variable will
hold. If a variable x is declared as int. it means x can
hold only integer values. Every variable which is used in
the program must be declared as what data-type it is.
Integer type int a=62536 wrong
Integers are used to store whole numbers.
Size and range of Integer type on 16-bit machine:
Type Size(bytes) Range
int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed 1 -128 to 127
short int
unsigned short int 1 0 to 255
long int or signed long 4 -2,147,483,648 to
int 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
Floating point type
Floating types are used to store real numbers.
Size and range of Integer type on 16-bit machine
Type Size(bytes) Range
Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932
Character type
Character types are used to store characters value.
Size and range of Integer type on 16-bit machine
Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255
void type
void type means no value. This is usually used to
specify the type of functions which returns nothing. We
will get acquainted to this datatype as we start learning
more advanced topics in C language, like functions,
pointers etc.
Variables in C Language
When we want to store any information(data) on our
computer/laptop, we store it in the computer's memory
space. Instead of remembering the complex address of
that memory space where we have stored our data, our
operating system provides us with an option to create
folders, name them, so that it becomes easier for us to
find it and access it.
Similarly, in C language, when we want to use some
data value in our program, we can store it in a memory
space and name the memory space so that it becomes
easier to access it.
The naming of an address is known as variable.
Variable is the name of memory location. Unlike
constant, variables are changeable, we can change
value of a variable during execution of a program. A
programmer can choose a meaningful variable name.
Example : average, height, age, total etc.
Datatype of Variable
A variable in C language must be given a type, which
defines what type of data the variable will hold.
It can be:
• char: Can hold/store a character in it.
• int: Used to hold an integer.
• float: Used to hold a float value.
• double: Used to hold a double value.
• void
Rules to name a Variable
1. Variable name must not start with a digit.
2. Variable name can consist of alphabets, digits and
special symbols like underscore _. int A1_b; 1 a
int while; int A, a;
3. Blank or spaces are not allowed in variable name.
4. Keywords are not allowed as variable name.
5. Upper and lower case names are treated as
different, as C is case-sensitive, so it is suggested
to keep the variable names in lower case.
Declaring, Defining and Initializing a variable
Declaration of variables must be done before they are
used in the program. Declaration does the following
things.
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
3. Until the variable is defined the compiler doesn't
have to worry about allocating memory space to the
variable.
4. Declaration is more like informing the compiler that
there exist a variable with following datatype which
is used in the program.
5. A variable is declared using the extern keyword,
outside the main() function.
int a;
float b;
double c, d;
int a=10, b=20;
10
a
FF01-FF02 FF03-FF04
Defining a variable means the compiler has to now
assign a storage to the variable because it will be used
in the program. It is not necessary to declare a variable
using extern keyword, if you want to use it in your
program. You can directly define a variable inside
the main() function and use it.
To define a function we must provide the datatype and
the variable name. We can even define multiple
variables of same datatype in a single line by using
comma to separate them.
int a , e , g;
float b, c;
Initializing a variable means to provide it with a value. A
variable can be initialized and defined in a single
statement, like:
int a = 10;
Let's write a program in which we will use some
variables.
#include<stdio.h>
#include<conio.h>
void main()
{
// variable definition , initialization
int a=7, b=14, c;
/* using addition operator */
c = a + b;
/* display the result */
printf("\n Sum is : %d ", c);
getch();
}
WAP multiplication of two no.s
(1110101)2 = ( )10
1*26+1*25+1*24+0*23+1*22+0*21+1*20
=64+32+16+4+1=117
Sum is : 21
You must be thinking how does this printf() works,
right?
Difference between Variable and Identifier?
An Identifier is a name given to any variable, function,
structure, pointer or any other entity in a programming
language. While a variable, is a named memory location
to store data which is used in the program.
Identifier Variable
Identifier is the name given to a While, variable is used to
variable, function etc. name a memory location
which stores data.
An identifier can be a variable, All variable names are
but not all indentifiers are identifiers.
variables.
Example:
Example:
// int variable
// a variable
int a;
int studytonight;
// float variable
// or, a function
float b;
int studytonight()
{
..
}
Another great analogy to understand the difference
between Identifier and Variable is:
You can think of an Identifier int x to be a variable's
name, but it can also be a function's name
int x() { } and still be an identifier.
Just like Obama is a name of a person, but also the
name of a foundation.
C Input and Output
Input means to provide the program with some data to
be used in the program and Output means to display
data on screen or write the data to a printer or a file.
C programming language provides many built-in
functions to read any given input and to display data on
screen when there is a need to output the result.
In this tutorial, we will learn about such functions, which
can be used in our program to take input from user and
to output the result on screen.
All these built-in functions are present in C header files,
we will also specify the name of header files in which a
particular function is defined while discussing about it.
scanf() and printf() functions
The standard input-output header file,
named stdio.h contains the definition of the
functions printf() and scanf(), which are used to
display output on screen and to take input from user
respectively.
#include<stdio.h>
void main()
{
// defining a variable
int i;
/*
displaying message on the screen
asking the user to input a value
*/
printf("Please enter a value...");
/*
reading the value entered by the
user
*/
// scanf(“ format specifier”,&variable name);
scanf("%d", &i);
/*
displaying the number as output
*/
printf( "\nYou entered: %d", i);
getch();
}
When you will compile the above code, it will ask you to
enter a value. When you will enter the value, it will
display the value you have entered on screen.
You must be wondering what is the purpose of %d inside
the scanf() or printf() functions. It is known
as format specifier and this informs
the scanf() function, what type of input to expect and
in printf() it is used to give a heads up to the
compiler, what type of output to expect.
Format Meaning
String
%d Scan or print an integer as signed decimal
number
%f Scan or print a floating point number
%c To scan or print a character
%s To scan or print a character string. The
scanning ends at whitespace.
We can also limit the number of digits or
characters that can be input or output, by adding a
number with the format string specifier,
like "%1d" or "%3s", the first one means a single
numeric digit and the second one means 3 characters,
hence if you try to input 42, while scanf() has "%1d",
it will take only 4 as input. Same is the case for output.
In C Language, computer monitor, printer etc output
devices are treated as files and the same process is
followed to write output to these devices as would have
been followed to write the output to a file.
NOTE : printf() function returns the number of
characters printed by it, and scanf() returns the
number of characters read by it.
int i = printf("studytonight");
In this program printf("studytonight"); will
return 12 as result, which will be stored in the variable i,
because study tonight has 12 characters.
getchar() & putchar() functions
The getchar() function reads a character from the
terminal and returns it as an integer. This function reads
only single character at a time. You can use this method
in a loop in case you want to read more than one
character. The putchar() function displays the
character passed to it on the screen and returns the
same character. This function too displays only a single
character at a time. In case you want to display more
than one characters, use putchar() method in a loop.
#include <stdio.h>
void main( )
{
char c, z;
printf("Enter a character");
/*
Take a character as input and
store it in variable c
*/
c = getchar(); G
/*
display the character stored
in variable c
*/
putchar(c); G
getch();
}
When you will compile the above code, it will ask you to
enter a value. When you will enter the value, it will
display the value you have entered.
gets() & puts() functions
The gets() function reads a line from stdin(standard
input) into the buffer pointed to by str pointer, until
either a terminating newline or EOF (end of file) occurs.
The puts() function writes the string str and a trailing
newline to stdout.
str → This is the pointer to an array of chars where the
C string is stored. (Ignore if you are not able to
understand this now.)
#include<stdio.h>
void main()
{
/* character array of length 100 */
char c[80];
clrscr();
printf("Enter a string");
gets( c ); hello world how are u
puts( c );
getch();
}
When you will compile the above code, it will ask you to
enter a string. When you will enter the string, it will
display the value you have entered.
Difference between scanf() and gets()
The main difference between these two functions is
that scanf() stops reading characters when it
encounters a space, but gets() reads space as
character too.
Syntax:- scanf(“% format specifier”, &variable name);
Ex:-
char a;
printf(“ enter value “);
scanf(“%c”, &a); 7852
Operator in C-language
An operator is a symbol that tells the compiler to perform
specific mathematical or logical functions. C language is rich in
built-in operators and provides the following types of operators
– an operators act upon which are known as operands
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Unary (Increment / decrement operator)
• Assignment Operators
• Misc Operators (Ternary , Sizeof)
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language.
Assume variable A = 10 and variable B = 20 then −
Show Examples
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
A=10 , B=20
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an B%A=0
integer division.
++ Increment operator increases the integer A++ = 11
value by one. A=10
-- Decrement operator decreases the integer A-- = 9
value by one. A=10
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A = 10 and variable B = 20 then −
Show Examples
Operator Description Example
== Checks if the values of two operands are equal or not. If (A == B)
yes, then the condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If (A != B)
the values are not equal, then the condition becomes is true.
true.
> Checks if the value of left operand is greater than the (A > B)
value of right operand. If yes, then the condition is not
becomes true. true.
< Checks if the value of left operand is less than the value (A < B)
of right operand. If yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or (A >= B)
equal to the value of right operand. If yes, then the is not
condition becomes true. true.
<= Checks if the value of left operand is less than or equal (A <= B)
to the value of right operand. If yes, then the condition is true.
becomes true.
Logical Operators
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then −
Show Examples
Operator Description Example
&& Called Logical AND operator. If both the operands are non- (A && B)
zero, then the condition becomes true. is false.
|| Called Logical OR Operator. If any of the two operands is (A || B) is
non-zero, then the condition becomes true. true.
! Called Logical NOT Operator. It is used to reverse the !(A && B)
logical state of its operand. If a condition is true, then is true.
Logical NOT operator will make it false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for
&, |, and ^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A'
holds 60 and variable 'B' holds 13, then −
Show Examples
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists (A & B) =
in both operands. 12, i.e.,
0000
1100
| Binary OR Operator copies a bit if it exists in either (A | B) =
operand. 61, i.e.,
0011
1101
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) =
operand but not both. 49, i.e.,
0011
0001
~ (~A ) =
Binary One's Complement Operator is unary and has ~(60),
the effect of 'flipping' bits. i.e,. -
0111101
<< Binary Left Shift Operator. The left operands value is A << 2 =
moved left by the number of bits specified by the right 240 i.e.,
operand. 1111
0000
>> Binary Right Shift Operator. The left operands value is A >> 2 =
moved right by the number of bits specified by the right 15 i.e.,
operand. 0000
1111
Assignment Operators
The following table lists the assignment operators supported by the C language −
Show Examples
Operator Description Example
= Simple assignment operator. Assigns values from right
side operands to left side operand C=A+B
will assign
C=10 , A=5 the value
of A + B
C%=A;
to C
C=C%A; C=10%5; 0
+= Add AND assignment operator. It adds the right C += A is
operand to the left operand and assign the result to the equivalent
left operand. to C = C +
A
-= Subtract AND assignment operator. It subtracts the C -= A is
right operand from the left operand and assigns the equivalent
result to the left operand. to C = C -
A
*= Multiply AND assignment operator. It multiplies the C *= A is
right operand with the left operand and assigns the equivalent
result to the left operand. to C = C *
A
/= Divide AND assignment operator. It divides the left C /= A is
operand with the right operand and assigns the result equivalent
to the left operand. to C = C /
A
%= Modulus AND assignment operator. It takes modulus C %= A is
using two operands and assigns the result to the left equivalent
operand. to C = C
%A
<<= Left shift AND assignment operator. C <<= 2
is same
as C = C
<< 2
>>= Right shift AND assignment operator. C >>= 2
is same
as C = C
>> 2
&= Bitwise AND assignment operator. C &= 2 is
same as
C=C&2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is
same as
C=C^2
|= Bitwise inclusive OR and assignment operator. C |= 2 is
same as
C=C|2
Misc Operators ↦ sizeof & ternary
Besides the operators discussed above, there are a few other important operators
including sizeof and ? : supported by the C Language.
Show Examples
Operator Description Example
sizeof() sizeof(a), where a is integer,
Returns the size of a variable.
will return 4.
& &a; returns the actual address
Returns the address of a variable.
of the variable.
* Pointer to a variable. *a;
?: Ternary , Conditional Expression. If Condition is true ? then value
X : otherwise value Y
Y=(X>5)?3:4;
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher precedence
than others; for example, the multiplication operator has a higher precedence than
the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a
higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Show Examples
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Unary operator
++ , --
Increment ++
Decrement –
Post and pre….
A=1;
// A++; ++A;
// A=A+1
// These statements must encountered in
printf(“A++”); 1 post A=3
printf(“++A”); 3 pre
printf(“A++”); 3 A=4
printf(“++A”); 5
Functions
A function is a group of statements that together
perform a task. Every C program has at least one
function, which is main (), and all the most trivial
programs can define additional functions.
You can divide up your code into separate
functions. How you divide up your code among
different functions is up to you, but logically the
division is such that each function performs a
specific task.
A function declaration tells the compiler about a
function's name, return type, and parameters. A
function definition provides the actual body of the
function.
The C standard library provides numerous built-in
functions that your program can call. For
example, strcat() to concatenate two
strings, memcpy() to copy one memory location
to another location, and many more functions.
A function can also be referred as a method or a
sub-routine or a procedure, etc.
Defining a Function
The general form of a function definition in C
programming language is as follows − syntax
return_type function_name( parameter
list ) {
body of the function
}
A function definition in C programming consists of
a function header and a function body. Here are
all the parts of a function −
• Return Type − A function may return a value.
The return_type is the data type of the value
the function returns. Some functions perform
the desired operations without returning a
value. In this case, the return_type is the
keyword void.
• Function Name − This is the actual name of
the function. The function name and the
parameter list together constitute the function
signature.
• Parameters − A parameter is like a
placeholder. When a function is invoked, you
pass a value to the parameter. This value is
referred to as actual parameter or argument.
The parameter list refers to the type, order,
and number of the parameters of a function.
Parameters are optional; that is, a function
may contain no parameters.
• Function Body − The function body contains
a collection of statements that define what the
function does.
Example
Given below is the source code for a function
called max(). This function takes two parameters
num1 and num2 and returns the maximum value
between the two −
/* function returning the max between
two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations
A function declaration tells the compiler about a
function name and how to call the function. The
actual body of the function can be defined
separately.
A function declaration has the following parts −
return_type function_name( parameter
list );
For the above defined function max(), the function
declaration is as follows −
int max(int num1, int num2);
Parameter names are not important in function
declaration only their type is required, so the
following is also a valid declaration −
int max(int, int);
Function declaration is required when you define
a function in one source file and you call that
function in another file. In such case, you should
declare the function at the top of the file calling the
function.
Calling a Function
While creating a C function, you give a definition
of what the function has to do. To use a function,
you will have to call that function to perform the
defined task.
When a program calls a function, the program
control is transferred to the called function. A
called function performs a defined task and when
its return statement is executed or when its
function-ending closing brace is reached, it
returns the program control back to the main
program.
To call a function, you simply need to pass the
required parameters along with the function name,
and if the function returns a value, then you can
store the returned value. For example −
#include <stdio.h>
#include<conio.h>
int max(int num1 , int num2 );
/* function declaration */
Void main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max
value */
ret = max(a, b);
printf( "Max value is : %d\n", ret
);
getch();
}
/* function returning the max between
two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
We have kept max() along with main() and
compiled the source code. While running the final
executable, it would produce the following result −
Max value is : 200
Function Arguments
If a function is to use arguments, it must declare
variables that accept the values of the arguments.
These variables are called the formal
parameters of the function.
Formal parameters behave like other local
variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two ways in
which arguments can be passed to a function −
[Link]. Call Type & Description
1 Call by value
This method copies the actual value of
an argument into the formal parameter
of the function. In this case, changes
made to the parameter inside the
function have no effect on the
argument.
2 Call by reference
This method copies the address of an
argument into the formal parameter.
Inside the function, the address is
used to access the actual argument
used in the call. This means that
changes made to the parameter affect
the argument.
By default, C uses call by value to pass
arguments. In general, it means the code within a
function cannot alter the arguments used to call
the function.