c language slideshow

20
C LANGUAGE C LANGUAGE Characteristics of C 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.

Upload: api-3735476

Post on 10-Apr-2015

656 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: c Language Slideshow

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.

Page 2: c Language Slideshow

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

Page 3: c Language Slideshow

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

Page 4: c Language Slideshow

The C Compilation Model The C Compilation Model

Preprocessor

Compiler

Assembler

Link Editor

Source code

Assembly code

Object code

Executable code

libraries

Page 5: c Language Slideshow

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

Page 6: c Language Slideshow

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

Page 7: c Language Slideshow

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. .

Page 8: c Language Slideshow

Order of Precedence Order of Precedence

() [ ] -> .() [ ] -> . * / %* / % + -+ - < <= >= >< <= >= > == !=== != && ^̂ &&&& |||| ,,

Page 9: c Language Slideshow

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

Page 10: c Language Slideshow

FUNCTIONSFUNCTIONS

returntype fn_name(1, parameterdef2,…)

{

localvariables functioncode

}

VOID FUNCTIONS

Page 11: c Language Slideshow

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 ;

Page 12: c Language Slideshow

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;

}}

Page 13: c Language Slideshow

* 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 .

Page 14: c Language Slideshow

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

  

Page 15: c Language Slideshow

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..)  

Page 16: c Language Slideshow

Basic String Handling Functions Basic String Handling Functions

strcmpstrcmp strcatstrcat strcpystrcpy strlenstrlen strerrorstrerror strcasecmpstrcasecmp

Page 17: c Language Slideshow

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.

Page 18: c Language Slideshow

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.

Page 19: c Language Slideshow

Common data structures Common data structures areare

• • Stacks Stacks • • Queues Queues Lists Lists Trees Trees Graphs Graphs Tables Tables

Page 20: c Language Slideshow

SORTING TECHNIQUESSORTING TECHNIQUES

Bubble sortBubble sort Quick sortQuick sort Insertion sortInsertion sort Selection sortSelection sort Heap sortHeap sort