Fundamentals of Computing and C Programming - Part 2

49
Presentation By Karthik Srini Fundamentals of Computing and C Programming Unit II - Basics of C language

Transcript of Fundamentals of Computing and C Programming - Part 2

Page 1: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Fundamentals of Computing and C ProgrammingUnit II - Basics of C language

Page 2: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Seminar IIBy Karthik Srini

Page 3: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Synopsis

1. Data Types

2. Type Conversion

3. Type Definition

4. Control Structures

Page 4: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Data TypesIn the C programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. They are categorised as :

1. Basic Data Type

2. Enumerated Data Type

3. The void Type

4. Derived Data Type

Page 5: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Basic Data Types

The basic data types in C are :

1. Integer data type

2. Float data type

3. Character data type

Page 6: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Integer Data TypeThe integer data types are further classified as integers and characters.

Integers can be classified with respect to length and sign as,

• Unsigned Integer

• Signed Integer

• Long Integer

• Short Integer

Page 7: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Signed Integer & Unsigned Integer

The are normally identified by the sign in front of the integer value. An integer value without any sign is assumed to be positive. (i.e.) unsigned.

For example,

unsigned int num = 10 ; ( unsigned )

int num = -10 ; ( signed )

Page 8: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Long Integer

If a large integer value is to be stored in a variable, then long is prefixed before int while declaring the variable.

For example,

If you want to store a mobile number ( 10 digits ), you declare it as ,

long int mobile_number = 1234567891 ;

Page 9: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Short Integer

Short integer is normally used to reduce the size of the program, since short int occupies less memory space than int and long int .

For example,

short num = 11 ;

Page 10: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Float Data Type

Float data type is normally used to represent floating points ( or ) floating numbers, float is prefixed before the variable name.

For example,

float pi = 3.14 ;

Page 11: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Double Data Type

Double data type is used to represent larger floating numbers

For example,

double num = 43.66677 ;

Page 12: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Long Double Data Type

Long double data type is used to represent even larger ( or ) longer floating numbers.

For example,

long double num = 4167.7789056 ;

Page 13: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Character Data Type

It is usually used to represent characters. The variable name is prefixed by char . Must be enclosed within single quotes.

For example,

char alphabet = ‘ c ’ ;

Page 14: Fundamentals of Computing and C Programming - Part 2
Page 15: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Enumerated Data Types

Page 16: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Enumerated Data Types

Enumerated data types are user defined data types. They are predefined with a discrete set of values by the user.

For example,

enum month ( Jan , Feb , Mar , Apr , May , Jun , Jul , Aug , Sep , Oct , Nov , Dec ) ;

enum month first = Jan ;

Page 17: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Type DefinitionIt is an user defined data type similar to enumerated data types, the only difference is, in type definition it is not necessary to define the discrete set of values.

For example,

typedef int number ;

number num1 , num2 ;

Page 18: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

The void Data Type

Void means null ( or ) nothing. It is normally prefixed before functions.

For now, this information is enough. You will learn in detail in the forthcoming chapters.

Page 19: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Derived Data Types

Page 20: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Derived Data TypesThey include,

(a) Pointer types,

(b) Array types,

(c) Structure types,

(d) Union types and

(e) Function types.

Page 21: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

ArraysArrays are defined as the collection of variables having the same base type ( or ) data type. Arrays are further classified as :

1. Single Dimensional Array

2. Double Dimensional Array

They are normally declared as like other declarations, but the array index must be specified.

For example,

int num [5] ;

Page 22: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

int num[5];

Memory allocation for

the above array. 0 1 2 3 4

100 34 76 56 89

Page 23: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Type Conversions

Page 24: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Type Conversion

Conversion of values of one data type into another data type is called Type conversion. It is classified as :

1. Implicit Type Conversion

2. Explicit Type Conversion

Page 25: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Implicit Type Conversion

These are done by the complier itself, without the knowledge of the programmer.

For example,

int b = 7 / 2 ; ( value of b will be 4 )

Page 26: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Explicit Type Conversion

This type of conversion are done by the user willingly.

For example,

int pi = (int) 3.4 ;

Page 27: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Functions

Page 28: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Functions

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.

Page 29: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Function Prototype

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature (parameter types, and return type), but omits the function body.

For example,

int add ( int a , int b )

Page 30: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Control Structures

Page 31: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Control Structures

Control structure decide the way of flow of control in a program. Based on some test conditions, the control of the program gets jumped from one point to the other. They can be further classified as :

1. Selection Statements

2. Looping Statements

Page 32: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Selection Statements

The control of the program gets jumped from one point to the other based on a test expression. They are classified as :

1. If else

2. Switch Case

Page 33: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Simple if Syntax

if ( condition )

{

action block ;

}

Page 34: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

if else Syntaxif ( condition )

{

Action block 1 ;

}

else

{

Action block 2 ;

}

Page 35: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

if , else if , else Syntaxif ( condition )

{

action block 1 ;

}

else if ( condition )

{

action block 2 ;

}

else

{

action block 3 ;

}

Page 36: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Switch Case Syntax switch ( condition )

{

case 1 :

action block1 ;

break ;

case 2 :

action block2 ;

break ;

default :

action block3 ;

}

Page 37: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Looping Structures

Page 38: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Looping Structures

Looping is a type of control structure in which a set of instructions is repeated again and again until the test condition evaluates to true.

There are two major types,

1. Entry controlled loop

2. Exit controlled loop

Page 39: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Entry controlled loopChecks whether the test expression evaluates to true, before the statements in the loop gets executed. If the test expression evaluates to false, the loop terminates.

These have two loops in major,

1. For loop

2. While Loop

Page 40: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

For loopFor loop has fixed number of repetitions. It is an entry controlled loop. It consists of three main clauses namely,

1. Initialisation expression

2. Test Expression

3. Update Expression

A for loop can have multiple initialisation expressions and multiple update expressions but can have only one test expression.

Page 41: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

for Loop Syntax

for ( initialisation exp ; test exp ; update exp )

{

action block ;

}

Page 42: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

While loop

While loop does not have fixed number of iterations. It is an entry controlled loop.

Page 43: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

while Loop Syntaxinitialisation expression ; ( if needed )

while ( condition )

{

action block ;

update expression ; ( if needed )

}

Page 44: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Exit Controlled Loop

Checks whether the test expression evaluates to true, after the statements in the loop gets executed. It gets executed at least once.

They have one loop in major,

1. Do While loop.

Page 45: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Do While Loop

It is an exit controlled loop. Gets executed at least once.

Page 46: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

do while Loop Syntaxinitialisation expression ;

do

{

action block ;

update expression ;

}

while ( condition ) ;

Page 47: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Queries ???

Page 48: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Log on to :

www.facebook.com/groups/skcetcsea

For downloading this Powerpoint presentation

Page 49: Fundamentals of Computing and C Programming - Part 2

Presentation By Karthik Srini

Thank You