Escolar Documentos
Profissional Documentos
Cultura Documentos
3. Error prone
4. Difficulty to modify
2. Assembly language:
One of the first steps in improving the program preparation
process was to substitute letter symbols-mnemonics for the
numeric operation codes of machine language. A mnemonic
(or memory aid) is any kind of mental trick we use to help
us remember. Mnemonics come in various shapes and
sizes, all of them useful in their own way. All computers
have the power of handling letters as well as numbers.
Hence, a computer can be taught to recognize certain
combination of letters or numbers. In this way, the
computer can be trained to translate a program written with
symbols instead of numbers into the computers own
machine language. Then we can write program for the
computer using symbols instead of numbers, and have the
computer do its own translating. This makes it easier for
the programmer, because he can use letters, symbols, and
mnemonics instead of numbers for writing his programs.
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
computers machine code is called assembler. The
assembler is a system program, which is supplied by the
computer manufacturer.
Advantages of Assembly language:
1. Easier to understand and use
Introduction:
==========
Historical developments of C:
Year
1960
1963
Language Developed by
ALGOL International
committee
CPL
Cambridge University
Remarks
Too general, too
abstract
Hard to learn,
difficult to
implement
Martin Richards at
Could deal with
Cambridge university only specific
problems
Ken Thompson at AT Could deal with
&T
only specific
problems
Dennis Ritche at AT & Lost generality
T
of BCPL and B
restored
1967
BCPL
1970
1972
Single quotation
Double quotation
?
Question mark
$
Dollar
!
Exclamatory
|
Pipe or Bar
^
Cap
&
Ampersand
(
Left parenthesis
)
Right parenthesis
[
Open square bracket
]
Close square bracket
{
}
\
Open brace
Close brace
Back slash or Reverse slash
Usage
Bell sound or beep sound
New line
Tab space
Back space
Carriage return
Single quotation
Double quotation
For back slash
For question mark
break
default
extern
int
signed
typedef
while
case
do
float
long
sizeof
union
char
double
for
register
static
unsigned
const
else
goto
return
struct
void
Operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment or decrement operators
6. Conditional operators
1.arithmetic operators
Arithmetic operators are used to perform arithmetic
operations like addition, subtraction, multiplication,
division and remainder.
Operator
+
*
/
%
Name
Plus
Minus
Asterisk
Slash
Modulator
Meaning
Addition
Subtraction
Multiplication
Division
Remainder
Example
a+b
a-b
a*b
a/b
a%b
10
Name
Less than
Greater
than
Less than or
equal to
Greater
than or
equal to
Equal to
Not equal to
Example
a<b
a>b
Result
False or 0
True or 1
a<=b
False or 0
a>=b
True or 1
a= =b
a!=b
False or 0
True or 1
11
12
13
ex:-
printf(%d,++a);
14
Data type
signed char
unsigned char
short signed int
short unsigned
int
long signed int
long unsigned
int
Float
Double
long double
Range
-128 to +127
0 to 255
-32,768 to +32,767
0 to 65,535
Bytes
1
1
2
2
Format
%c
%c
%d
%u
-2147483648 to
+2147483647
0 to 4294967295
%ld
%lu
-3.4e38 to +3.4e38
-1.7e308 to +1.7e308
-1.7e4932 to 1.7e4932
4
8
10
%f
%lf
%Lf
structure of a C program
# include <header file name>
return type main( )
{
declaration part;
execution part;
}
A C program starts with the include statement. This
statement is used to link the header files into our program.
This statement should start with # symbol.
ex: # include <stdio.h>
stdio.h stands for standard input output . header file
A C program contains 1 or more functions from these
functions one function is the main( ).
15
ex: main( )
or
void main( )
The C compiler executes the program from the main().
main() function begins with { and ends with } . The
main() has declaration part and execution part. The
declaration part is used to define the variables and data
types.
Ex:int a,b,c;
float x,y,z;
char ch,name[20] ;
The remaining programming statements are called as
execution part. Each statement should be terminated with a
semicolon (;) . We have to write the C program in lower
case letters only.
output functions: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);
16
char name[20]=InterFace ;
printf(\n The name is %s,name);
input functions:
scanf():- It is a input function, that is used to accept the
values into variables at the time of executing a program.
This function belongs to stdio.h .
syntax:- scanf(control string, variable list);
17
18
19
statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}
If the condition is true then statement1 and statement2 will
be executed. If the condition is false then statement3 and
statement4 will be executed.
syntax 5:
Nested if
if(condition1)
Statement1;
else
if(condition2)
statement2;
else
statement3;
If the condition1 is true then statement1 will be executed
and the remaining statements will be skipped. If the
condition1 is false then it will check the condition2, if it is
true then statement2 will be executed. If both condition1
and condition2 are false then it will execute statement3.
Looping statements:
while statement:-
20
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
}
When the for loop is executing for the very first time the
initial value will be stored in the given variable. Then the
21
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.
switch statement:-
22
23
24
ex:-
int a[2][2];
String functions:
1) strlen():- This function is used to return the length of a
string.
syntax:- strlen(string);
note:- When we are using string functions we have to
include a header file <string.h>
2) strcpy():- This function copies a string from one string
variable to another string variable.
syntax:- strcpy(target_string , source_string);
3) strcat(): (String concatenation)
This function adds a string at the end of another string
syntax:- strcat(string1,stirng2 );
4) strcmp(): (String comparison)
This compares one string with another string, if two strings
are equal this function returns 0, else it returns the numeric
difference between the non-matching characters.
Syntax:- strcmp(string1, string2);
5) stricmp(): This function is similar to strcmp, but this
function ignores the case sensitivity.
syntax:- stricmp(string1, string2);
6) strrev(): This function reverses the given string
Syntax:- strrev(string);
25
26
Uses of functions:
a. Dividing the task into sub tasks.
b. If a particular task is repeated number of times in
a program we can create a function and call that
function wherever we require.
27
return(expression)
return(0);
return(a);
return(a+b);
return a;
return;
28
POINTERS
A pointer is a variable which holds the address of
another variable. Since addresses are whole numbers
pointers would always contains whole numbers.
Consider the following declaration
int i=10;
The declaration tells the C compiler,
1) Reserve space in memory to hold the integer value.
2) Associate the name i with this memory location.
3) Store the value 10 at this location.
Memory map:i - location name
10 - Value at location
65524 location number or address.
Dynamic memory allocation:
malloc()
The name malloc stands for "memory allocation". The
function malloc() reserves a block of memory of specified size and
return a pointer of type void which can be casted into pointer of
any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
29
30
realloc()
If the previously allocated memory is insufficient or more than
sufficient. Then, you can change memory size previously allocated
using realloc().
Syntax of realloc()
ptr=realloc(ptr,newsize);
]
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.
Declaring a structure:struct <structure name>
{
structure element 1;
structure element 2;
--structure element n;
};
structure variables;
ex:struct student
31
{
int sno;
char name[20];
int total;
};
struct address a,b,c;
or
struct student
{
int sno;
char name[20];
int total;
}a,b,c;
Accessing structure elements:We can access the structure elements by using
structure_variable.structure_element;
ex:- a.sno, a.name, a.total
a.sno=5;
a.total=432;
STORAGE CLASSES
It tells us
1. Where the variable would be stored
2. What will be the initial value of the variable, if
the initial value is not specifically assigned?
32
33
fopen()
34
File opening modes:1)w :- Writing the data into a file. If the file already exists
its contents will be over written.
2)r:- Reading data from a file.
3)a:- Adds data into an existing file.(Appending)
4)w+ :- We can write data into a file and we can read data
from the file. If the file already exists its contents will be
over written, else creates a new file.
5)r+ :- Reading existing contents , writing new contents,
modifying existing contents of a file.
6)a+ :- Reading existing contents, appending new contents
at the end of a file.
closing a file:When the file processing is over, the file
must be closed. We can close a file by using fclose( ).
syntax:- fclose(file pointer);
ex:fclose(fp);
File functions:1)getc()
This function is used to read a character from a file.
syntax :- getc(file pointer);
ex:getc(fp);
2)putc()
This function is used to write a character into a file.
syntax:- putc(character, file pointer);
ex:putc(ch,fp);
3)fprintf():- This function writes formatted data into a file.
Syntax:- fprintf(file pointer, formatted string, list of
variables)
Ex:fp=fopen(student.dat,w);
35
36
37
ex: -
fwrite(&b, sizeof(b),1,fp);
5) fread()
Syntax:-
38
ex:-
fread(&b,sizeof(b),1,fp);