programming fundamentals lecture 6
Post on 15-Apr-2017
37 Views
Preview:
TRANSCRIPT
Concept Of
Data Types
Lecture-06
REHAN IJAZ
By
Programm
ing Fundamentals
Data Type (causes & reasons)
A Data Type is a Type of Data.
Data types are the keywords, which are used for assigning a type to a
variable.
Data Type is a Data Storage Format that can contain a Specific Type or
Range of Values.
When computer programs store data in variables, each variable must be
assigned a specific data type.
In C, variable(data) should be declared before it can be used in program.
Whenever we declare variable in Computer’s memory, Computer must
know the type of the data to be stored inside the memory.
When the compiler encounters a declaration for a variable, it sets up a
memory location for it
If we need to store the single character then the size of memory occupied
will be different than storing the single integer number.
The memory in our computers is organized in bytes.
A byte is the minimum amount of memory that we can manage in C.
Data Type (causes & reasons)
Data TypesSyntax for declaration of a variable
data_type variable_name;
int var1;int std_reg_no;char std_gender;
Classifications of Data Types• Built-in data types
– Fundamental data types (int, char, double, float, void, pointer)– Derived data types (array, string, structure)
• Programmer-defined data types– Structure– Union– Enumeration
Data TypesData Type keyword Description
Integer Data Type int Stores the Integer Value
Float Data Type float Stores the Floating Point Value
Character Data Type char Stores the Single Character Value
Long Data Type long Stores the Long range Integer Value
Double Data Type double Stores the long range Floating Value
Range of Data TypesType Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
−To
int - data typeint is used to define integer numbers.
float - data typefloat is used to define floating point numbers.
char - data typechar defines characters.
Purpose and syntax for Data Types
int rollno; rollno = 5;
float kilometre; kilometre = 5.6;
char section; section = ‘D';
double - data typedouble is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.
Purpose and syntax for Data Types
double Atoms; Atoms = 2500000;
Purpose and syntax for Data Typespointer-data type
A pointer is a special kind of variable designed for storing memory address i.e. the address of another variable. Declaring a pointer is the same as declaring a normal variable except you stick an asterisk '*' in front of the variables identifier.
There are two new operators you will need to know to work with pointers. The "address of" operator '&' and the "dereferencing" operator '*'. When you place '&' in front of a variable you will get it's address, this can
be stored in a pointer vairable. When you place an '*' in front of a pointer you will get the value at the
memory address pointed to.
Purpose and syntax for Data Types
{int *ptr, q;/* address of q is assigned to ptr */ptr = &q;/* display q’s value using ptr variable */printf(“%d”, *ptr);
Output:
A0250
Example of pointer
Data Types
An array is a collection of values, all of the same type, stored contiguously
in memory.
An array of size N is indexed by integers from 0 up to and including N-1.
There are also "arrays of unspecified size" where the number of elements
is not known by the compiler. Here is a brief
1.One dimensional array 2. Two dimensional array
array
Data TypesArray (one dimensional)
#include<stdio.h> int main() { int arr[5] = {10,20,30,40,50}; printf(“value of arr[0] is %d \n”, arr[0]); }
Output : 10
Syntax : data-type arr_name[array_size];
//or arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;
0 1 2 3 4
4 5 3 1 9
Data TypesArray (two dimensional)
int arr[2][2] = {10,20,30,40};printf(“value of arr[%d] [%d] : \n”,arr[1][0]);
Output : 30
syntax : data_type array_name[num_of_rows][num_of_column]
// orarr[0][0] = 10;arr[0][1] = 20;arr[1][0] = 30;arr[1][1] = 40;
0 1
0 10 20
1 30 40
more about Data Types
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Given below is an example to get the size of int type on any machine #include<stdio.h>
#include<limits.h> int main() {
printf("Storage size for int : %d \n", sizeof(int));
return 0; }
sizeof(type)
Type castingTypecasting concept in C language is used to modify a variable from one date type to another data type. New data type should be mentioned before the variable name or value in brackets which to be typecast.
#include <stdio.h> int main () { float x; x = (float) 7/5; printf("%f",x); }
Output: 1.400000
Type casting
S.no Typecast function Description
1 atof() Converts string to float
2 atoi() Converts string to int
3 atol() Converts string to long
4 itoa() Converts int to string
5 ltoa() Converts long to string
Thank you
top related