SlideShare a Scribd company logo
C-Data Types,
Arrays and Structs
PREPARED BY SAAD SHAIKH.
Data Types
 The basic definition of a 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.
Types of Data Types
 Primary Data Types
 They are arithmetic types and are further classified into:
(a) Integer types, (b) Character and (c) Floating-point types.
 Derived Data Types:
 They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union
types and (e) Function types.
 User Defined Data Types:
 They include TypeDef and ENUM.
Integer Type
 Integers are a commonly used data type in computer programming. An
integer is a whole number (not a fraction) that can be positive, negative, or
zero.
 It is denoted by “int” .
Type Storage Size Value Range
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
Floating Point Type
 As the name implies, floating point numbers are numbers that contain
floating decimal points. For example, the numbers 5.5, 0.001, and -
2,345.6789 are floating point numbers.
 Denoted by keyword “float”.
Type Storage size Value Range Precision
float 4 byte 1.2E-38 to
3.4E+38
6 decimal places
double 8 byte 2.3E-308 to
1.7E+308
15 decimal places
long double 10 byte 3.4E-4932 to
1.1E+4932
19 decimal places
Character Data Types
 Character data type allows a variable to store only one character.
 “char” keyword is used to refer character data type.
Type 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
Example Code:
#include <stdio.h>
void main()
{
int i = 20;
float f = 3.1415;
char c;
c = 'd';
printf("This is my float: %f n", f);
printf("This is my integer: %d n", i);
printf("This is my character: %c n", c);
}
VOID DATA TYPE
 The void type has no values therefore we cannot declare it as variable as we
did in case of integer and float.
 The void data type is usually used with function to specify its type.
User Defined Data Types
 User defined data types are those data types which are created by the
user to characterize the existing data types.
 This user defined data type can later be used to declare variables.
 In other words we are redefining the name of existing data types.
User Defined Data Types-Enum:
 An enumeration is a user-defined data type consists of integral constants and
each integral constant is give a name. Keyword enum is used to defined
enumerated data type.
 Syntax: enum type_name{ value1, value2,...,valueN };
 Here, type_name is the name of enumerated data type or tag. And value1,
value2,....,valueN are values of type type_name.
 By default, value1 will be equal to 0, value2 will be 1 and so on but, the
programmer can change the default value.
Example:
#include<stdio.h>
#include<conio.h>
Enum Weekday
{
Sun,Mon,Tues,Wed,Thus,Fri,Sat
};
Int main ()
{
enum Weedkday wd;
wd = Thus;
printf(“%d”,wd):
}
4
Output
User Defined Data Types-typedef
The C programming language provides a keyword called typedef, which you can
use to give a type, a new name.
 Syntax:
typedef <type><identifier>;
 Example:
typedef int score;
Score player1,player2;
Derived Data Types: ARRAY
 Array is a kind of data structure that can store a fixed-size sequential collection
of elements of the same type.
 An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.
 All arrays consist of contiguous memory locations.
First Element Last Element
a[1]a[0] a[2] a[3] a[4] a[5] a[6] a[7] a[n]
Declaring Arrays
 To declare an array in C, a programmer specifies the type of the elements
and the number of elements required by an array as follows :
type arrayName [ arraySize ];
 For example we can declare a 10 element array like:
int marks [10];
Initializing Array
 We can initialize an array in C either one by one or using a single
statement as follows:
double price[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
 The number of values between braces { } cannot be larger than the
number of elements that we declare for the array between square brackets
[ ].
Example:
#include <stdio.h>
int main ()
{
int n[ 10 ];
/* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100;
/* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %dn", j, n[j] );
}
return 0;
}
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Output
Multidimensional Arrays
 Array having more than one subscript variable is called multidimensional array.
 Multidimensional array is also called as matrix
 To declare a two-dimensional integer array of size [x][y], you would write
something as follows:
type arrayName [ x ][ y ];
 To initialize multidimensional array we specify bracketed value of each row; for
example:
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
Example:
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8} };
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
Output :
Structures
 Arrays allow to define type of variables that can hold several data items of the
same kind. Similarly structure is another user defined data type available in C
that allows to combine data items of different kinds.
 Structures are used to represent a record. Suppose you want to keep track of
your books in a library.
You might want to track the following attributes about each book like:
Title
Author
Subject
Book ID
Defining Structures
 To define a structure, you must use
the struct statement.
 The format is as follows:
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Example:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
}
book;
Accessing Structure Members
 To access any member of a structure, we use the member access operator
(.).
Example:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;};
int main( )
{
struct Books Book; /* Declare Book of type Book */
/* book specification */
strcpy( Book.title, "C Programming");
strcpy( Book.author, "Aptech");
strcpy( Book.subject, "C Programming");
Book.book_id = 6495407;
/* print Book info */
printf( "Book title : %sn", Book.title);
printf( "Book author : %sn", Book.author);
printf( "Book subject : %sn", Book.subject);
printf( "Book book_id : %dn", Book.book_id);
return 0;
}
Book title : C Programming
Book author : Aptech
Book subject : C Programming
Book book_id : 6495407
Output :
Thank you..

More Related Content

What's hot

Data types in C
Data types in CData types in C
Data types in C
Ansh Kashyap
 
Data Types In C
Data Types In CData Types In C
Data Types In C
Simplilearn
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
Samsil Arefin
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Kamal Acharya
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
 
Typedef
TypedefTypedef
Typedef
vaseemkhn
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
 
Data types
Data typesData types
Data types
Syed Umair
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 

What's hot (20)

Data types in C
Data types in CData types in C
Data types in C
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Strings in C
Strings in CStrings in C
Strings in C
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Typedef
TypedefTypedef
Typedef
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Strings
StringsStrings
Strings
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Data types
Data typesData types
Data types
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Similar to C data types, arrays and structs

Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6
REHAN IJAZ
 
Arrays
ArraysArrays
Data types
Data typesData types
Data types
Sachin Satwaskar
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
AnirudhaGaikwad4
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
HNDE Labuduwa Galle
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
Mohammad Imam Hossain
 
Session 4
Session 4Session 4
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
atifmugheesv
 
6.array
6.array6.array
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
PriyadarshiniS28
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
AqeelAbbas94
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
Mufaddal Nullwala
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 

Similar to C data types, arrays and structs (20)

Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6
 
Arrays
ArraysArrays
Arrays
 
Data types
Data typesData types
Data types
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Session 4
Session 4Session 4
Session 4
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
 
6.array
6.array6.array
6.array
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 

Recently uploaded

Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
Cherry
 
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Sérgio Sacani
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
yusufzako14
 
FAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable PredictionsFAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable Predictions
Michel Dumontier
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
Richard Gill
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
muralinath2
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
ChetanK57
 
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
Sérgio Sacani
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
subedisuryaofficial
 
Nutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technologyNutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technology
Lokesh Patil
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SELF-EXPLANATORY
 
Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
ossaicprecious19
 
Predicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdfPredicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdf
binhminhvu04
 
insect taxonomy importance systematics and classification
insect taxonomy importance systematics and classificationinsect taxonomy importance systematics and classification
insect taxonomy importance systematics and classification
anitaento25
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
sachin783648
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
muralinath2
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
Areesha Ahmad
 
ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
muralinath2
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
IvanMallco1
 
Viksit bharat till 2047 India@2047.pptx
Viksit bharat till 2047  India@2047.pptxViksit bharat till 2047  India@2047.pptx
Viksit bharat till 2047 India@2047.pptx
rakeshsharma20142015
 

Recently uploaded (20)

Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
 
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
Observation of Io’s Resurfacing via Plume Deposition Using Ground-based Adapt...
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
 
FAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable PredictionsFAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable Predictions
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
 
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
 
Nutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technologyNutraceutical market, scope and growth: Herbal drug technology
Nutraceutical market, scope and growth: Herbal drug technology
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
 
Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
 
Predicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdfPredicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdf
 
insect taxonomy importance systematics and classification
insect taxonomy importance systematics and classificationinsect taxonomy importance systematics and classification
insect taxonomy importance systematics and classification
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
 
ESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptxESR_factors_affect-clinic significance-Pathysiology.pptx
ESR_factors_affect-clinic significance-Pathysiology.pptx
 
filosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptxfilosofia boliviana introducción jsjdjd.pptx
filosofia boliviana introducción jsjdjd.pptx
 
Viksit bharat till 2047 India@2047.pptx
Viksit bharat till 2047  India@2047.pptxViksit bharat till 2047  India@2047.pptx
Viksit bharat till 2047 India@2047.pptx
 

C data types, arrays and structs

  • 1. C-Data Types, Arrays and Structs PREPARED BY SAAD SHAIKH.
  • 2. Data Types  The basic definition of a 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.
  • 3. Types of Data Types  Primary Data Types  They are arithmetic types and are further classified into: (a) Integer types, (b) Character and (c) Floating-point types.  Derived Data Types:  They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.  User Defined Data Types:  They include TypeDef and ENUM.
  • 4. Integer Type  Integers are a commonly used data type in computer programming. An integer is a whole number (not a fraction) that can be positive, negative, or zero.  It is denoted by “int” . Type Storage Size Value Range 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
  • 5. Floating Point Type  As the name implies, floating point numbers are numbers that contain floating decimal points. For example, the numbers 5.5, 0.001, and - 2,345.6789 are floating point numbers.  Denoted by keyword “float”. Type Storage size Value Range Precision float 4 byte 1.2E-38 to 3.4E+38 6 decimal places double 8 byte 2.3E-308 to 1.7E+308 15 decimal places long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
  • 6. Character Data Types  Character data type allows a variable to store only one character.  “char” keyword is used to refer character data type. Type 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
  • 7. Example Code: #include <stdio.h> void main() { int i = 20; float f = 3.1415; char c; c = 'd'; printf("This is my float: %f n", f); printf("This is my integer: %d n", i); printf("This is my character: %c n", c); }
  • 8. VOID DATA TYPE  The void type has no values therefore we cannot declare it as variable as we did in case of integer and float.  The void data type is usually used with function to specify its type.
  • 9. User Defined Data Types  User defined data types are those data types which are created by the user to characterize the existing data types.  This user defined data type can later be used to declare variables.  In other words we are redefining the name of existing data types.
  • 10. User Defined Data Types-Enum:  An enumeration is a user-defined data type consists of integral constants and each integral constant is give a name. Keyword enum is used to defined enumerated data type.  Syntax: enum type_name{ value1, value2,...,valueN };  Here, type_name is the name of enumerated data type or tag. And value1, value2,....,valueN are values of type type_name.  By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value.
  • 11. Example: #include<stdio.h> #include<conio.h> Enum Weekday { Sun,Mon,Tues,Wed,Thus,Fri,Sat }; Int main () { enum Weedkday wd; wd = Thus; printf(“%d”,wd): } 4 Output
  • 12. User Defined Data Types-typedef The C programming language provides a keyword called typedef, which you can use to give a type, a new name.  Syntax: typedef <type><identifier>;  Example: typedef int score; Score player1,player2;
  • 13. Derived Data Types: ARRAY  Array is a kind of data structure that can store a fixed-size sequential collection of elements of the same type.  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.  All arrays consist of contiguous memory locations. First Element Last Element a[1]a[0] a[2] a[3] a[4] a[5] a[6] a[7] a[n]
  • 14. Declaring Arrays  To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows : type arrayName [ arraySize ];  For example we can declare a 10 element array like: int marks [10];
  • 15. Initializing Array  We can initialize an array in C either one by one or using a single statement as follows: double price[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};  The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
  • 16. Example: #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %dn", j, n[j] ); } return 0; } Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 Output
  • 17. Multidimensional Arrays  Array having more than one subscript variable is called multidimensional array.  Multidimensional array is also called as matrix  To declare a two-dimensional integer array of size [x][y], you would write something as follows: type arrayName [ x ][ y ];  To initialize multidimensional array we specify bracketed value of each row; for example: int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };
  • 18. Example: #include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8} }; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; } a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8 Output :
  • 19. Structures  Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.  Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book like: Title Author Subject Book ID
  • 20. Defining Structures  To define a structure, you must use the struct statement.  The format is as follows: struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables]; Example: struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
  • 21. Accessing Structure Members  To access any member of a structure, we use the member access operator (.).
  • 22. Example: #include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id;}; int main( ) { struct Books Book; /* Declare Book of type Book */ /* book specification */ strcpy( Book.title, "C Programming"); strcpy( Book.author, "Aptech"); strcpy( Book.subject, "C Programming"); Book.book_id = 6495407; /* print Book info */ printf( "Book title : %sn", Book.title); printf( "Book author : %sn", Book.author); printf( "Book subject : %sn", Book.subject); printf( "Book book_id : %dn", Book.book_id); return 0; } Book title : C Programming Book author : Aptech Book subject : C Programming Book book_id : 6495407 Output :