Professional Documents
Culture Documents
Users Are in Two Types:: Computerwares
Users Are in Two Types:: Computerwares
Translators:
Assembler:
It is translating software. It translates assembly code to binary and vice
versa.
Compiler / Interpreter:
Both are translating soft wares. Those can translate high level
language to binary language and vice versa.
Compiler can process a whole file at a time and it provides .exe
files. But, Interpreter can process a file line-by-line only. It does not
provide .exe files. This is the main difference between them.
------------------------------------------------------------------------------------Algorithm
Def: Algorithm means a step-by-step procedure for solving a
particular problem. Algorithm is designed in two ways.
1. Pseudo code: It is a dummy code for solving a problem. It
is designed in simple English language.
2. Flow chart : It is the graphical representation of solving a
problem.
------------------------------------------------------------------------------------Introduction to C
C is a programming language. That was designed by a man named
"DENNISE RITCHIE" at AT & T Bell Labs in USA in 1972.
The C programming language is a powerful programming language.
Because, it is Middle level language. That means, it is machine
dependable and program dependable. So, it is Middle level.
This is the Mother of All programming languages.
------------------------------------------------------------------------------------Learning Steps in 'C' :
Character Set ? Word ? Instruction ? Program ? Software
Character Set :
Def: All allowable symbols in this language are called "Character
set". The following characters are used in this language.
Alphabets
Digits
Special Symbols
?
?
White spaces
a...z
A...Z
0,1 ... 9
+-*/%<>=!'":;.,?|\_
(){}[]~@#$^ &
Word:
Def: Group of characters is called a 'Word'.
There are 3 types of words in C.
i.
Keyword
ii.
Variables and / or Identifiers
iii.
Constants
i. Keywords :
- Keywords are the words. Those have a specific meaning
defined in C compiler. So, those are called as 'Reserved words'.
- These are Pre-defined words.
- There are basically 32 keywords in C.
int
long
short
float
double
char
if
else
switch
case
default
break
goto
continue
return
do
while
for
far
near
auto
regitster
extern
static
struct
union
enum
typedef
void
singed
unsigned
const
ii. Variable :
Meaning: The quantity that can change their value during program
execution.
Def: It is a user-defined name. That is given to the memory location,
where the quantity is stored.
Eg.
x =35
Here, the 35 is stored in the location (65000) of memory. A
name given to that location is 'x'. Here, x is called variable name.
Rules for constructing a variable name :
1.
The name should not matched with Keyword
2.
The maximum length of a variable name is 8 chars and some
3.
4.
5.
Eg.
VALID
INVALID
data
auto
var25
var 25
interest
data,35
emp_code
$var1
hello_5
_stdno
DATA
AUTO
etc.
------------------------------------------------------------------------------------Operators
Def:
-
Special symbol.
That can perform a specific operation without changing their
meaning.
All operators are special characters. But, all special characters
are not operators.
a condition. Any condition can returns either TRUE or FALSE only. But, not both
at a time.
syn:
( Q1 relnl.opr. Q2 )
3. Logical operators :
The special characters &&, || and ! are called 'Logical operators'.
Those can perform their corresponding actions.
These operators can be used for joining more than one condition.
It returns either TRUE or FALSE only. But, not both.
4. Assignment operator :
The special character '=' is called an Assignment operator.
That means the right quantity is assigned to left side variable.
syn:
var = constant / var / exprn ;
Eg.
x=3
y=x
z = x + 10 * y
// constant assignment
// variable assignment
// expression assignment
Note :
Expression means the combination of variables and / or constants with arithmetic
operators. This is also called as an operand.
by 1.
syn.
Eg.
++ var ;
==> var = var + 1;
x=5
y = ++x
? x = 6 and y = 6
2. Post-increment operator :
After assigning to a variable, its value will be incremented
by 1.
syn.
var ++ ;
==> var = var + 1;
Eg.
x=5
y = x ++
? x = 6 and y = 5
Decrement : It means 1 is deducting from the previous value of that
var. This operator is placed in two ways.
1. Pre-decrement operator :
Before assigning to a variable its value will be decremented
by 1.
syn.
-- var ;
==> var = var - 1;
Eg.
x=5
y = -- x
? x = 4 and y = 4
2. Post-decrement operator:
After assigning to a variable, its value will be decremented
by 1.
syn.
var --;
==> var = var - 1;
Eg.
x=5
y=x
? x = 4 and y = 5
7. Bitwise operators:
The following operators can be used for performing their actions on bit
values only.
Operator
Meaning
&
|
^
~
>>
<<
>>>
Bitwise AND
Bitwise OR
Exclusive OR (XOR)
Complement
Right shift
Left shift
Right shift fill with zero
8. Special Operators :
The following special chars can be used for specific purposes in the
program.
Operator
#
.
->
,
sizeof
Meaning
Pre-processor
data accessing operator
'' (pointer )
separator
to find size of a variable
------------------------------------------------------------------------------------Tokens
Def: The smallest individual unit in a program is called Token.
C Tokens :1.
2.
3.
4.
5.
6.
Character Set
Key Words
Variables and Identifiers
Constants
Operators
Data Types
Instructions:
Def: Collection of words has returns a complete meaning and ends
with a semicolon (;) is called an Instruction.
Types of Instructions
1. Type declaration Instructions:
Any variable is used in a program that must be declaring its data type
before using it.
syn:
datatype
var1, var2 , ...;
Eg.
int
a;
float data;
char grade;
2. I/O Instructions:
To read data from key board (std. input device) using a statement. That
is called "Input instruction".
To display the information on the monitor (std. output device) using a
statement. That is called "OutputInstruction".
Syn:
scanf ( ) ;
printf ( ) ;
3. Arithmetic instructions:
Def: These instructions can perform Arithmetic calculations.
Eg.
c = a+b;
gross = basic + hra +da;
etc.
4. Control instructions:
Def: To control the flow of execution of the program using certain
statements are called "Conrol Instructions ".
Eg.
if statement, looping statements etc.
------------------------------------------------------------------------------------Program
Def: Set of instructions can solve a particular problem or performing a
specific task is called a Program.
General form of C Program:
[Comment lines]
including statements
[defining statements]
void main()
{
[variable declaration section ; ]
[clear screen section ; ]
[input / output section ; ]
[processing section ; ]
[getch() ; ]
}
[functionction definitions]
comment lines :
Def: Unreadable code used in a program that is called a comment.
It describes about the program.
It can be used for Identification only.
In this statement, we can use two types of symbols
/*
? starting comment
*/
? ending comment
Including statements :
Def: To down load any header files using a statement. That is called
"Including statement".
Header file:
- It is a pre-defined program.
- It contains function (Sub programs), variables and constants etc.
syn:
# include <headerfile>
expansion
Eg.
#define
#define
#define
etc.
PI
P
M
3.143
printf
main( )
Note: Generally Templates can be used as Uppercase letters. Easy to identify the
templates in a program.
main() :
This is a user-defined function. Any function represents as follows,
name( )
That can do some thing in a program.
Eg.
main ()
Here, the word main is followed by a pair of parentheses (). That
represents a function.
-
Each and Every C program must start its execution from this
point only.
This is defined by programmer only. so, it is user - defined
function.
Any function can return a value. but, main() function. does not
return any value. Because, it is preceded by the keyword 'void '.
Here, void is the one of the keyword.
Block :
More than one instruction placed in a pair of braces. That represents a
block of code.
Eg.
void main ( )
{
executable statements ;
----------------------------- ;
---------------------------- ;
}
getch() :
- This is library function. It reads a single character. But, does
not display on the monitor.
- Generally it can be used as the last statement in every C
program. It can also use any where in the program except
declaration section.
- It is defined in the header file, 'conio.h'.
- This function belongs to console input function.
------------------------------------------------------------------------------------Rules for constructing a c program :
printf ():
- It is a Library function. That is defined in the header file
'stdio.h'.
- It can display any type of messages on the monitor only.
syn:
1. printf("MSG");
2. printf("Formatting string " , List of variables);
scanf() :
- It is also a Library function like printf(). It is defined in the
header file 'stdio.h'.
- It can read any type of data from KB (Std. input device) only.
- It is the standard input function in C language.
syn : scanf (" formatting chars " , list of vars);
Here, each variable name must be preceded by a special symbol (&).
'&' reps. 'Address of a variable.'
Eg:
/* reading any two integer values from KB */
scanf("%d%d", &x1, &x2);
/* reading emp code, name and salary */
scanf("%d%s%f ", &code, &name, &salary);
Note: It does not allowed any spe. sym or msgs.
gotoxy() :
syn:
Eg.
gotoxy(40,12);
------------------------------------------------------------------------------------Control Statements
Def: To control the flow of sequence of execution of a program using
certain statements are called "Control Statements".
there are 3 categories of control statements.
1. Branching statements : A block of code will be executed based on
given criteria (condition).
Eg:
1.
Ternary operator statement (conditional Operator Statement)
2.
if
statements
3.
switch statement (multiple block statement)
2. Un-conditional statements : The control will moves from one
location to another location without checking any condition in a
program.
Eg.
1.
goto statement
2.
break
"
3.
continue
"
4.
return
"
3.Looping (Iterative) statements : these can be used for repeating a
block of code in specified no. of times or un till the condition returns
false.
Eg.
1. while
loop
2. do-while loop
3. for
loop
Advantages :
- To avoid the re-writing of the same code
- To reduce the length of a program
- Executing time is low
- Save memory space
- Debugging is easy.
------------------------------------------------------------------------------------condition: It is an expression. That is formed with relational operators. It returns
if statement :
Def: This is also conditional statement like ternary operator statement.
But, it can executes a block of code when a cond. returns either TRUE
or FALSE.
There are 4 types of if statements.
1. Simple if :
syn:
if (cond.)
{
//true statements
}
2. if- else statement :
syn:
if (cond.)
{
// true statements
}
else
{
//false statements
}
3. nested - if statement :
An if is placed in another if statement. That is called 'Nested - if '.
syn:
if (cond1)
{
// outer true statements
if (cond2)
{
//inner true statements
}
else
{
// inner flase statements
}
}
else
{
//outer false statements
}
4. Ladder if statement :
syn:
if (cond1)
{
// 1 true statements
}
else
if (cond2)
{
// 2 true statements
}
else
if (cond3)
{
// 3 true statements
}
else
{
// false statements
}
------------------------------------------------------------------------------------switch stt. :
It is similar to if - statement.
This is multiple blocks of statement.
It is also a selection statement.
Generally, it can used for Menu driven programs only.
Syn:
switch( var )
{
/*beginning
of
the
switch
case const1 :
------------;
------------;
break;
case const2 :
------------;
*/
------------;
break;
case const3 :
------------;
------------;
break;
default :
--------------;
} /* end of switch */
------------------------------------------------------------------------------------break statement :
It is an un-conditional control statement.
It can be used as the last statement in every case in switch
statement. But, in default case need not necessary. Because,
default case is the last case in switch statement. So, control will
be comes out from the switch automatically.
Generally, it can be used in looping statements also.
break preceded by an if statement in looping statements.
Syn: break ;
------------------------------------------------------------------------------------goto statement:
It is the un-conditional control statement.
It can transfer the control from one location to another location
in the same program without checking any condition.
The word goto is the one of the key word.
syn:
goto label ; // declaration or/and calling a label
label :
executable statements ;
--------------------------------------------------------------
Eg.
1. while
2. do-while
3. for
loop
loop
loop
Advantages :
- To avoid the re-writing of the same code
- To reduce the length of a program
- Executing time is low
- Save memory space
- Easy to debug.
------------------------------------------------------------------------While:
It is a pre-checking looping statement. That means, initially
checks the given condition. If it returns TRUE then the block of
code is executed. That the block of code is executed until
condition returns FALSE. If it returns FALSE then exit from the
loop.
Syn:
initialization ;
while (cond)
{
-----------------------;
-----------------------;
updatable statements;
}
do-while:
It is post-checking looping statement.
That means, after executing the block of code then the
condition will be checked. If it returns TRUE then only
repeat the code otherwise control transfers to the next
statement of that loop.
Syn:
Initialization;
do
{
--------------------;
--------------------;
updatable statement;
}
while(cond) ;
Note: It can execute the block of code at least once. This is the main difference
between do-while and while looping statements.
for :
Syn:
for ( initialization ; condition ; updatable statement )
{
// body of loop
}
-
Meaning
checks the character is digit
checks the character is alphabet
checks the character is lower case
checks the character is upper case
checks the character is alpha numeric
checks the character is space
converts to lower case character
converts to upper case character
------------------------------------------------------------------------------------Arrays
Def : Set of similar data elements has shared a common name called
'an Array'.
Representation:
varname [ size ]
Here, size must be a +ve int only. size represent maximum size of an
array.
Types of Arrays:
There are two types of arrays. Those are,
i.
Single Dimensional Array
ii.
Multi Dimensional Array
[1 - D array]
[n - D array]
([ ])
Datatype
varname [size];
Eg.
int
a[5];
Here, 'a' is a variable name and 5 is the maximum size of that array.
That means 'a' can holds 5 integer values.
If a variable is declared as int data type that array is called 'integer
array'. Similarly, 'float array'. But character array is called a 'string'.
Notes:
1.
Array elements are stored in sequential order in the memory.
2.
Each & every array first element is stored in 0th position
and
th
its last element is stored in (size -1) position.
3.
In single dimensional array, all elements are stored in linear
order. That means, either in row wise or column wise only.
Multi dimensional array :
A variable name followed by more than one pair of square brackets ([
]) called 'Multi dimensional array'.
syn. Datatype
2 - D Array :
A variable name followed by two pair of square brackets ([ ]) called
'2 - D array'.
syn: Datatype
varname[size1][size2];
Here, size1 ? max. rows
size2 ? max. columns
Note: 2 -D array elements are stored in matrix format. That means,
rows and columns wise.
Eg.
int
a[3][2];
Here, 3 --> rows and 2 --> columns.
------------------------------------------------------------------------------------String Handling Functionctions
There are some pre-defined functions on strings. Those are defined in
the header file string.h.
Function.
strlen()
strcpy()
strcat()
strrev()
Purpose
calculates length of a string
copies a string to another string
concatenates two strings
reversed strings of a string
strcmp()
strlwr()
strupr()
strstr()
Functionctions
Meaning: 'Do something' in a program.
Def: A self contained block of code performing a specific job of some
kind. That is called "Function".
Repesentation: Any name followed by a pair parenthesis.
syn. functionname ( )
There are 2 types of functionctions.
1. Library functionctions: Pre-defined functions. are called
'Library functions' . These are defined in the header files.
Eg.
printf ()
scanf( )
clrscr()
getch() etc.
2. User-defined functionctions: These functions. are defined
by programmer only.
Eg.
prime()
display()
getdata() etc.
Notes:
- main() is a function. It has collection of function. Each and
every C program must start its execution from this function.
only.
- Any no. of functions can be used in a program.
- A function can be used any no. of times in a program.
- Any function can return a value with return statement.
Here, return is a key word.
syn. return (var / const. / exprn ) ;
- It can be used as the last statement in every definition.
- The void function does not return any value. The word void is
also a keyword.
- A function can be called from another one.
Arguments:
There are 3 types of arguments.
1. Actual arguments :
Formal arguments:
The variables, which are declared in a pair of parentheses at
the function definition called 'Formal arguments'.
syn.
returntype
functionname(args list)
{
// body of the function.
}
Eg.
void
{
}
Here, the vars. 'x' and 'y' are called formal arguments.
3. Local variables :
The variables, which are declared in a pair of braces at the
function definition called "Local variables ".
syn.
returntype
functionname(args list)
{
declaring vars. ;
// body of the function.
}
Eg.
void
{
2.
No. of Actual arguments = No. of Formal arguments.
3.
The order, data types and returns must be matched.
------------------------------------------------------------------------------------Any function. can be defined in two ways.
1.
K & R method (Kernighon & Ritchie )
2.
ANSI method (American National Standard Institution)
Function. def. in K & R method:
syn.
returntupe
functionname(args. list)
datatype vars ;
{
//body of the function.
}
Eg.
void
add ( x , y)
int x, y;
{
//body of the function.
}
}
Note: Generally, we can use ANSI method only.
Signature.
Syn. Functionname (list of vars);
------------------------------------------------------------------------------------Structures
Def: Collection of dissimilar data elements has shared a common
name is called a Structure.
Syn:
struct ST-NAME
{
datatype var1;
datatype var2;
.
};
typedef struct
{
datatype var1;
datatype var2;
..
}ST-NAME;
or
Object construction :
Syn:
struct ST-NAME obj;
or
ST-NAME obj;
Eg.
struct EMP
{
int eid;
char name[20];
char desgn[20];
float sal;
};
struct EMP e;
Here, e ? is an object of EMP datatype.
Accessing data members :
We can access the data members of a structure using dot(.) operator or
pointer (->) operator only.
Eg.
EMP e;
e.eid
e.name
e.desgn
e.sal
-
EMP *e;
e->eid
e->name
e->desgn
e->sal
Nested structures:
Def: A structure with in a structure is called Nested structure.
Syn.
Eg.
struct INNER
struct DOB
{
{
datatype var11;
int dd;
datatype var12;
int mm;
int yy;
};
};
struct OUTER
{
datatype var1;
datatype var2;
struct INNER in;
};
struct STUDENT
{
int rno;
char name[20];
char course[20];
struct DOB date;
};
struct STUDENT s;
-------------------------------------------------------------------------------------
Files
Def: Collection of information stored in a particular medium
permanently is called a File.
File names are 2 types:
i.
Primary filename
: It is designed by user. Its max.
length is 8 chars only.
ii.
Extension filename : It is allotted by its application
by default. It can allow up to 3 chars.
Note:
- Primary filename and extension names are separated by a dot.
- Each and every file can follows 8.3 rules.
Eg.
Hello.doc
Myfile.txt
Stddata.xls
Total.c
Palindrome.cpp
Myweb.java
Types of Files :
There are many types in files.
- Text files [The files, which are created in Notepad application only]:
It can allow only chars.
- Document files [The files, which are created in Ms-word application
only]: It can allow text / diagrams/ pics. etc.
- Data base files [The files, which are created in MS-Access
application etc]
- Audio / video files
- Jpeg / jpg files
- Image files etc.
Data Base :
It is the relationship between different tables.
Table:
The data is stored in the form of rows and columns.
Row / Record :
Collection of related field items in the table.
A row of information represents a record in a table / file.
Field:
An individual data item (column) in the table is called a field.
Advantages of files :
Pre-defined structure.
Its pointer var. can search a file from the local disc.
Its var. must be declared as pointer only. Because, it can
stores the address of opened file.
Syn:
FILE *fileptr;
Eg.
FILE *fp;
- fopen() :
It can be used for creating a new file or opening an existing file.
Syn:
FILE *fileptr ;
fileptr = fopen (filename, mode) ;
- fclose():
It can be used for closing opened files.
Every opened file must be closed.
Syn:
fclose (fileptr);
- fgetc():
It reads a single character from a file.
Syn:
Var = fgetc(fileptr);
-fputc( ) / fputch( ):
It stores a single character into a file.
Syn:
fputc (var, fileptr);
- fgets():
It reads a string from a file.
Syn.
fgets( var, size, fileptr);
-fputs():
It stores /writes a string into a file.
Syn:
fputs (var, fileptr);
-EOF :
End Of File.
-feof( ):
It represents File end of File.
Syn.
feof(fileptr)
fprintf() :
It can be used for storing/ displaying the details into a file.
Syn.
fprintf (fileptr, formatting string , list of vars) ;
fscanf():
It can be used for reading / retrieving data from a file.
Syn.
fscanf(fileptr, formatting chars , list of vars.) ;
fwrite():
It can store / displays details of an object into a file.
Syn.
fwrite ( &obj, sizeof(obj), no.of objs, fileptr) ;
fread():
It can read / retrieve details of an object from a file.
Syn.
fread ( &obj, sizeof(obj), no.of objs, fileptr) ;
rewind( ):
It can move the control to the beginning of the file.
Syn.
rewind (fileptr) ;
rename ( ):
To rename a file. That means to change the name of the file.
Syn.
rename (oldname, newname) ;
remove():
It can remove a file from disc.
Syn.
remove (filename);
fseek():
To moves the control to the specific location in the file.
Syn.
fseek(location, sizof(obj), const) ;
Here, const ?
SEEK_BEG
SEEK_END
SEEK_CUR
File Opening Modes
There are mainly 6 types of modes in files. Those are,
Mode is string only. So, it must be placed in double quotes only.
Mode
r
w
a
r+
w+
a+
Meaning
Reading data from a file
Writing data into a file
Appending data to a file
Reading / writing data from / to a
file. Modification is allowed.
Writing data into a file and reads
they back. Modification is allowed.
Appending data into a file and
reads they back. But, Modification
is not allowed.