c language slideshow
TRANSCRIPT
C LANGUAGEC LANGUAGECharacteristics of CCharacteristics 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 C Program Structure
Preprocessor Commands Preprocessor Commands Type definitions Type definitions Function prototypes -- declare function types Function prototypes -- declare function types
and variables passed to function and variables passed to function Variables Variables Functions Functions
Creating, Compiling and Running Creating, Compiling and Running Your Program Your Program
Use any ordinary editor with which you are Use any ordinary editor with which you are familiar to create the filefamiliar to create the file
The filename must by convention end The filename must by convention end ``.c''``.c'' (full stop, lower case c), (full stop, lower case c), e.g. myprog.ce.g. myprog.c
The C Compilation Model The C Compilation Model
Preprocessor
Compiler
Assembler
Link Editor
Source code
Assembly code
Object code
Executable code
libraries
The Preprocessor The Preprocessor
The Preprocessor accepts source code as input The Preprocessor accepts source code as input and is responsible for and is responsible for
removing comments removing comments interpreting specialinterpreting special preprocessor directivespreprocessor directives
denoted by denoted by ##. . C CompilerC Compiler The C compiler translates source to assembly The C compiler translates source to assembly
codecode AssemblerAssembler The assembler creates object codeThe assembler creates object code
Variables Variables
C typeC type Size (byte)Size (byte)
char 1char 1 Unsigned charUnsigned char 1 1 Short intShort int 2 2 Unsigned short intUnsigned short int 2 2 ((long) intlong) int 4 4 float 4float 4 double 8double 8
Arithmetic Operations Arithmetic Operations
Increment & Decrement operationsIncrement & Decrement operations
Postfix and Prefix expressionsPostfix and Prefix expressions
example:- example:- x++x++ is faster than is faster than x=x+1x=x+1. .
Order of Precedence Order of Precedence
() [ ] -> .() [ ] -> . * / %* / % + -+ - < <= >= >< <= >= > == !=== != && ^̂ &&&& |||| ,,
CONDITIONALSCONDITIONALS
IF STATEMENT IF STATEMENT ? OPERATOR? OPERATOR SWITCH STATEMENTSWITCH STATEMENT
breakbreak and and continuecontinue C provides two commands to control how we loop: C provides two commands to control how we loop: breakbreak -- exit form loop or switch. -- exit form loop or switch. continuecontinue -- skip 1 iteration of loop-- skip 1 iteration of loop
FUNCTIONSFUNCTIONS
returntype fn_name(1, parameterdef2,…)
{
localvariables functioncode
}
VOID FUNCTIONS
Structures Structures Collection of different datatypes. Collection of different datatypes. A structure is a user-defined data type.A structure is a user-defined data type.A structure is a collection of one or more variables, possiblyA structure is a collection of one or more variables, possiblyof different types, grouped together under a single nameof different types, grouped together under a single name
Eg: struct studentEg: struct student {{ int rollno;int rollno; char name[10];char name[10]; float marks;float marks; } s ;} s ;
UNIONSUNIONS
A union is a variable which may hold (at different times) objects of A union is a variable which may hold (at different times) objects of different sizes and typesdifferent sizes and types . .
union numberunion number
{ {
short shortnumber;short shortnumber;
long longnumber;long longnumber;
double float number;double float number;
}}
* POINTERS* POINTERS
C uses C uses pointerspointers a lota lot. . Why?Why?: : It is the only way to express some computations. It is the only way to express some computations. It produces compact and efficient code. It produces compact and efficient code. It provides a very powerful tool. It provides a very powerful tool.
What is a Pointer?What is a Pointer? A pointer is a variable which contains the address in memory of another A pointer is a variable which contains the address in memory of another
variable .variable .
Dynamic Memory Allocation Dynamic Memory Allocation
Dynamic allocation is a pretty unique feature to C .Dynamic allocation is a pretty unique feature to C .
It enables us to create data types and structures of any size and length to It enables us to create data types and structures of any size and length to suit our programs need suit our programs need withinwithin the program. the program.
MallocMallocCallocCallocReallocRealloc
FILESFILESFiles are the most common form of a streamFiles are the most common form of a stream
Open a file:Open a file:FILE *fopen(char *name, char *modeFILE *fopen(char *name, char *mode)) fopenfopen returns a pointer to a FILE. returns a pointer to a FILE. *name ?? *mode ??*name ?? *mode ??
Reading and writing files Reading and writing files The functions The functions fprintffprintf and and fscanffscanf a commonly used to access a commonly used to access
files. files. int fprintf(FILE *stream, char *format, args..)int fprintf(FILE *stream, char *format, args..)int fscanf(FILE *stream, char *format, args..)int fscanf(FILE *stream, char *format, args..)
Basic String Handling Functions Basic String Handling Functions
strcmpstrcmp strcatstrcat strcpystrcpy strlenstrlen strerrorstrerror strcasecmpstrcasecmp
DATA STRUCTURESDATA STRUCTURES
Definition of data structures Definition of data structures
• • Many algorithms require that we use a proper representation of data to Many algorithms require that we use a proper representation of data to achieve efficiency. achieve efficiency.
• • This representation and the operations that are allowed for it are called This representation and the operations that are allowed for it are called data structures. data structures.
• • Each data structure allows insertion, access, deletion etc. Each data structure allows insertion, access, deletion etc.
Why do we need data Why do we need data structures? structures?
• • Data structures allow us to achieve an Data structures allow us to achieve an important goal: component reuse important goal: component reuse
• • Once each data structure has been Once each data structure has been implemented once, it can be used over implemented once, it can be used over and over again in various applications. and over again in various applications.
Common data structures Common data structures areare
• • Stacks Stacks • • Queues Queues Lists Lists Trees Trees Graphs Graphs Tables Tables
SORTING TECHNIQUESSORTING TECHNIQUES
Bubble sortBubble sort Quick sortQuick sort Insertion sortInsertion sort Selection sortSelection sort Heap sortHeap sort