C LANGUAGE
Characteristics of C
∀• Small size
∀• Extensive use of function calls
∀• Loose typing -- unlike PASCAL
∀• Structured language
∀•Pointer implementation - extensive use of pointers for
memory, array, structures and functions.
C Program Structure
Preprocessor Commands
Type definitions
Function prototypes -- declare function types
and variables passed to function
Variables
Functions
Creating, Compiling and Running
Your Program
Use any ordinary editor with which you are
familiar to create the file
The filename must by convention end ``.c''
(full stop, lower case c), e.g. myprog.c
The C Compilation Model
Source code
Preprocessor
Compiler
Assembly code
Assembler
librarie
s Object code
Link Editor
Executable code
The Preprocessor
The Preprocessor accepts source code as input
and is responsible for
removing comments
interpreting special preprocessor directives
denoted by #.
C Compiler
The C compiler translates source to assembly
code
Assembler
The assembler creates object code
Variables
C type Size (byte)
char 1
Unsigned char 1
Short int 2
Unsigned short int 2
(long) int 4
float 4
double 8
Arithmetic Operations
Increment & Decrement operations
Postfix and Prefix expressions
example:- x++ is faster than x=x+1.
Order of Precedence
() [ ] -> .
* / %
+ -
< <= >= >
== !=
&
^
&&
||
,
CONDITIONALS
IF STATEMENT
? OPERATOR
SWITCH STATEMENT
break and continue
C provides two commands to control how we loop:
• break -- exit form loop or switch.
continue -- skip 1 iteration of loop
FUNCTIONS
returntype fn_name(1, parameterdef2,…)
localvariables
functioncode
VOID FUNCTIONS
Structures
Collection of different datatypes.
A structure is a user-defined data type.
A structure is a collection of one or more variables, possibly
of different types, grouped together under a single name
Eg: struct student
{
int rollno;
char name[10];
float marks;
}s;
UNIONS
A union is a variable which may hold (at different times) objects of
different sizes and types .
union number
{
short shortnumber;
long longnumber;
double float number;
}
* POINTERS
C uses pointers a lot. Why?:
• It is the only way to express some computations.
• It produces compact and efficient code.
• It provides a very powerful tool.
What is a Pointer?
A pointer is a variable which contains the address in memory of another
variable .
Dynamic Memory Allocation
Dynamic allocation is a pretty unique feature to C .
It enables us to create data types and structures of
any size and length to suit our programs need within
the program.
Malloc
Calloc
Realloc
FILES
Files are the most common form of a stream
Open a file:
FILE *fopen(char *name, char *mode)
fopen returns a pointer to a FILE.
*name ?? *mode ??
Reading and writing files
The functions fprintf and fscanf a commonly used to access
files.
int fprintf(FILE *stream, char *format, args..)
int fscanf(FILE *stream, char *format, args..)
Basic String Handling Functions
strcmp
strcat
strcpy
strlen
strerror
strcasecmp
DATA STRUCTURES
Definition of data structures
• Many algorithms require that we use a proper representation of data to
achieve efficiency.
• This representation and the operations that are allowed for it are called
data structures.
• Each data structure allows insertion, access, deletion etc.
Why do we need data
structures?
• Data structures allow us to achieve an
important goal: component reuse
• Once each data structure has been
implemented once, it can be used over
and over again in various applications.
Common data structures
are
• Stacks
• Queues
Lists
Trees
Graphs
Tables
SORTING TECHNIQUES
Bubble sort
Quick sort
Insertion sort
Selection sort
Heap sort