c language characteristics of c · small size

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

Upload: randolf-rodgers

Post on 08-Jan-2018

216 views

Category:

Documents


4 download

DESCRIPTION

C Program Structure Preprocessor Commands Type definitions Function prototypes -- declare function types and variables passed to function Variables Functions

TRANSCRIPT

Page 1: C LANGUAGE Characteristics of C · Small size

C LANGUAGECharacteristics 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 Characteristics of C · Small size

C Program Structure

• Preprocessor Commands • Type definitions • Function prototypes -- declare function

types and variables passed to function • Variables • Functions

Page 3: C LANGUAGE Characteristics of C · Small size

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

Page 4: C LANGUAGE Characteristics of C · Small size

The C Compilation Model

Preprocessor

Compiler

Assembler

Link Editor

Source code

Assembly code

Object code

Executable code

libraries

Page 5: C LANGUAGE Characteristics of C · Small size

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

Page 6: C LANGUAGE Characteristics of C · Small size

Variables C type Size (byte) char 1 Unsigned char 1 Short int 2 Unsigned short int 2 (long) int 4 float 4 double 8

Page 7: C LANGUAGE Characteristics of C · Small size

Arithmetic Operations

• Increment & Decrement operations

• Postfix and Prefix expressions

• example:- x++ is faster than x=x+1.

Page 8: C LANGUAGE Characteristics of C · Small size

Order of Precedence • () [ ] -> .• * / % • + - • < <= >= > • == != • & • ^ • && • || • ,

Page 9: C LANGUAGE Characteristics of C · Small size

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

Page 10: C LANGUAGE Characteristics of C · Small size

FUNCTIONS

returntype fn_name(1, parameterdef2,…)

{

localvariables functioncode

}

VOID FUNCTIONS

Page 11: C LANGUAGE Characteristics of C · Small size

Structures Collection of different datatypes. A structure is a user-defined data type.A structure is a collection of one or more variables, possiblyof different types, grouped together under a single name

Eg: struct student { int rollno; char name[10]; float marks; } s ;

Page 12: C LANGUAGE Characteristics of C · Small size

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; }

Page 13: C LANGUAGE Characteristics of C · Small size

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

Page 14: C LANGUAGE Characteristics of C · Small size

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.

MallocCallocRealloc

 

Page 15: C LANGUAGE Characteristics of C · Small size

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

Page 16: C LANGUAGE Characteristics of C · Small size

Basic String Handling Functions

• strcmp• strcat• strcpy• strlen• strerror• strcasecmp

Page 17: C LANGUAGE Characteristics of C · Small size

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.

Page 18: C LANGUAGE Characteristics of C · Small size

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.

Page 19: C LANGUAGE Characteristics of C · Small size

Common data structures are

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

Page 20: C LANGUAGE Characteristics of C · Small size

SORTING TECHNIQUES• Bubble sort• Quick sort• Insertion sort• Selection sort• Heap sort