Fundamentals of Computing and C Programming - Part 2
-
Upload
karthik-srini -
Category
Engineering
-
view
53 -
download
7
Transcript of Fundamentals of Computing and C Programming - Part 2
Presentation By Karthik Srini
Fundamentals of Computing and C ProgrammingUnit II - Basics of C language
Presentation By Karthik Srini
Seminar IIBy Karthik Srini
Presentation By Karthik Srini
Synopsis
1. Data Types
2. Type Conversion
3. Type Definition
4. Control Structures
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
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
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
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 )
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 ;
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 ;
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 ;
Presentation By Karthik Srini
Double Data Type
Double data type is used to represent larger floating numbers
For example,
double num = 43.66677 ;
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 ;
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 ’ ;
Presentation By Karthik Srini
Enumerated Data Types
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 ;
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 ;
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.
Presentation By Karthik Srini
Derived Data Types
Presentation By Karthik Srini
Derived Data TypesThey include,
(a) Pointer types,
(b) Array types,
(c) Structure types,
(d) Union types and
(e) Function types.
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] ;
Presentation By Karthik Srini
int num[5];
Memory allocation for
the above array. 0 1 2 3 4
100 34 76 56 89
Presentation By Karthik Srini
Type Conversions
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
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 )
Presentation By Karthik Srini
Explicit Type Conversion
This type of conversion are done by the user willingly.
For example,
int pi = (int) 3.4 ;
Presentation By Karthik Srini
Functions
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.
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 )
Presentation By Karthik Srini
Control Structures
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
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
Presentation By Karthik Srini
Simple if Syntax
if ( condition )
{
action block ;
}
Presentation By Karthik Srini
if else Syntaxif ( condition )
{
Action block 1 ;
}
else
{
Action block 2 ;
}
Presentation By Karthik Srini
if , else if , else Syntaxif ( condition )
{
action block 1 ;
}
else if ( condition )
{
action block 2 ;
}
else
{
action block 3 ;
}
Presentation By Karthik Srini
Switch Case Syntax switch ( condition )
{
case 1 :
action block1 ;
break ;
case 2 :
action block2 ;
break ;
default :
action block3 ;
}
Presentation By Karthik Srini
Looping Structures
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
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
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.
Presentation By Karthik Srini
for Loop Syntax
for ( initialisation exp ; test exp ; update exp )
{
action block ;
}
Presentation By Karthik Srini
While loop
While loop does not have fixed number of iterations. It is an entry controlled loop.
Presentation By Karthik Srini
while Loop Syntaxinitialisation expression ; ( if needed )
while ( condition )
{
action block ;
update expression ; ( if needed )
}
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.
Presentation By Karthik Srini
Do While Loop
It is an exit controlled loop. Gets executed at least once.
Presentation By Karthik Srini
do while Loop Syntaxinitialisation expression ;
do
{
action block ;
update expression ;
}
while ( condition ) ;
Presentation By Karthik Srini
Queries ???
Presentation By Karthik Srini
Log on to :
www.facebook.com/groups/skcetcsea
For downloading this Powerpoint presentation
Presentation By Karthik Srini
Thank You