SlideShare une entreprise Scribd logo
1  sur  50
Télécharger pour lire hors ligne
DATA TYPES
in
C Programming
1
Engr. Qazi Shahzad Ali
Kyungpook National University, South Korea
Four Types of Data
1- Fundamental Data
Types
4- User Defined Data
Types
2- Data Type Modifiers
3- Derived Data Types
2
1- Fundamental Data Types
1.1- Integer
1.2- Character
1.4- Double
1.5- Void
1.3- Float
FUNDAMENTAL DATA TYPES ARE THOSE
THAT ARE NOT COMPOSED OF OTHER
DATA TYPES
3
2- Data type modifiers
2.1- Integer Type
Modifiers
2.2- Character Type
Modifiers
2.3- Floating-point Modifiers
1-They change some properties
of the data type
2-The modifiers define the
amount of storage allocated to
the variable. Like short, double,
long
4
3- Derived Data Types
3.1- Arrays
3.3- Pointers
3.4- References
3.5- Constants
3.2- Functions
5
4- User Defined Derived Data
Types
4.1- Class
4.2- Structure
4.3- Union
4.4- Enumeration
6
Web link for study of Enumeration
http://cplus.about.com/od/introductiontoprogramming/p/enumeration.htm
The Decimal & Binary Number System
Binary Decimal
0000 0 max number that can be represent by= 2n -1
0001 1 n= number of bits
0010 2 for Example if we use 1 bit then we can represent only 0 &
0011 3 1 decimal number, if 2 bit then 2 and 3, if 3 bit then max
0100 4 up to 7 (23-1=8-1=7), Similarly if we use 8 bits in case of
0101 5 “char data type” then max (28-1=256-1=255),similarly in
0110 6 case of signed 1 bit out of 8 bits are used for plus or minus
0111 7 (0 for +, & 1 for -) so (Max27-1=128-1=127) & (Min-27=-128),
1000 8
1001 9
7
Name Description Size* Range*
char Character or small integer. 1byte
signed: -128 to 127
unsigned: 0 to 255
short int (short) Short Integer. 2bytes
signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 2bytes
signed: -32768 to 32767
unsigned: 0 to 65535
long int (long) Long integer. 4bytes
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
float Floating point number. 4bytes 10 -38 to 1038 (~7 digits)
double
Double precision floating
point number.
8bytes 10 -308 to 10308 (~15 digits)
long double
Long double precision
floating point number.
10bytes 10 -4932 to 104932 (~19 digits)
8
Integer Modifier
TYPE APPROXIMAT
E SIZE
MINIMAL RANGE
SHORT 2 -32768 to 32767
UNSIGNED SHORT 2 0 to 65,535
SIGNED SHORT 2 Same as short
INT 2 -32768 to 32767
UNSIGNED INT 2 0 to 65,535
SIGNED INT 2 Same as int
LONG 4 -2,147,483,648 TO 2,147,483,647
UNSIGNED LONG 4 0 to 4,294,967,295
SIGNED LONG 4 Same as long
Note: The prefix signed makes the integer type hold negative values also.
Unsigned makes the integer not to hold negative values.
9
Char Modifier
TYPE APPROXIMATESIZE MINIMALRANGE
CHAR 1 -128 to 127
UNSIGNED CHAR 1 0 to 255
SIGNED CHAR 1 Same as char
Floating Point Modifier
TYPE APPROXIMATE SIZE DIGITS OF
PRECISION
FLOAT 4 7
DOUBLE 8 15
LONG DOUBLE 10 19
10
How to declare them
{
int Count;
Count = 5;
}
{
float Miles;
Miles = 5.6;
}
{
char Letter;
Letter = 'x';
}
Integer Declaration:
Float Declaration:
Character Declaration:
11
Signedness
• If signed, the most significant bit designates a positive or negative value leaving
the remaining bits to be used to hold a designated value. Unsigned integers can
only take non-negative values (positive or zero), while signed integers (default) can
take both positive and negative values.
• An unsigned character, depending on the code page, might access an extended
range of characters from 0 → +255, instead of that accessible by a signed char
from −128 → +127, or it might simply be used as a small integer. The standard
requires char, signed char, unsigned char to be different types. Since most
standardized string functions take pointers to plain char, many C compilers
correctly complain if one of the other character types is used for strings passed to
these functions
 unsigned int x;
 signed int y;
 int z; /* Same as "signed int" */
 unsigned char grey;
 signed char white;
12
INTEGER DATA TYPE
Integers are whole numbers.
Countable like # of rooms in a house
C has 3 classes/Modifier of integer
1. short int
2. int
3. long int
13
FLOAT DATA TYPE
• Decimal/Exponential Number
• Values that are measureable like length of
room 145.25 inches
• Store memory in two parts
1. Mantissa (The value of number takes 3 bytes)
2. Exponent (Power of number takes 1 byte)
Exp: 12345 will be represented as 1.2345e4 (4 is exponent & 1.2345 is
number)
• They have two advantages
1: they can represent values between integers.
2: they can represent a much greater range of values.
14
CHARACTER DATA TYPE
• Includes every printable character on the
standard English language keyboard
• We can show ASCII code with the help of
characters
• Example of characters:
– Numeric digits: 0 - 9
– Lowercase/uppercase letters: a - z and A - Z
– Space (blank)
– Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
15
DOUBLE DATA TYPE
• The data type double is also used for
handling floating-point numbers. But it is
treated as a distinct data type because, it
occupies twice as much memory as type
float, and stores floating-point numbers
with much larger range and precision. It is
slower that type float.
16
VOID DATA TYPE
• It specifies an empty set of values. It is
used as the return type for functions that
do not return a value. No object of type
void may be declared.
• It is used when program or calculation
does not require any value but the syntax
needs it.
17
C-Software Tools
In order to write a C program you will need:
• An Editor - Something to write code in.
• A Compiler - Converts the code into Machine
Language (object files).
• A Linker – Combines several object files into
one executable (written in Machine
Language).
• A Debugger – Provides supervised execution
of the code.
18
• A token is a language element that can be used in
forming higher level language constructs
• Equivalent to a ‘word’ in English language
• Several types of tokens can be used to build a
higher level C language construct such as
expressions and statements
• There are 6 kinds of tokens in C:
– Reserved words (keywords)
– Identifiers
– Constants
– String literals (constant)
– Punctuators
– Operators
Token
19
Reserved Words/Keywords
• Keywords that identify language entities such as
statements, data types, language attributes, etc.
• Have special meaning to the compiler
• cannot be used as identifiers/variable in our
program.
• Should be typed in lowercase.
• White in color
• Total numbers=32
• Example: const, double, int, main, void, while, for,
else (etc..)
20
Identifiers
• Words used to represent certain program
entities (program variables, function names,
etc).
• Example:
– int my_name;
• my_name is an identifier used as a program variable
– void CalculateTotal(int value)
• CalculateTotal is an identifier used as a function name
21
Punctuators (separators)
• Symbols used to separate different parts of the C
program.
• These punctuators include:
– [ ] ( ) { } , ; “: * #
• Usage example:
22
void main (void)
{
int num=10;
printf (“%d”,num);
}
C Constants
Primary Constants
Integer Constant,
Real Constant,
Character
Constant
Secondary
Constants
Array
Pointer
Structure
Union
Enum.etc
23
Constants
• Entities that appear in the program code as fixed
values.
• 3 types of Primary constants:
– Integer constants
• Positive or negative whole numbers with no fractional part
• Example:
– const int MAX_NUM = 10;
– const int MIN_NUM = -90;
– Floating-point constants
• Positive or negative decimal numbers with an integer part, a
decimal point and a fractional part
• Example:
– const double VAL = 0.5877e2; (stands for 0.5877 x 102)
– Character constants
• A single character enclosed in a single inverted commas
• Example:
– const char letter = ‘n’;
– const char number = ‘1’; 24
String Literal (Constant)
• A sequence of Character consisting of
Alphabets,didgits,and special character in
double quotation mark is call String Constant
Example “Pakistan” and “Lahore-58900”
25
Const Qualifier
• The data that follow the keyword “const” cannot
change its value during execution of program.
This can be replaced by Define directive
Example : const float p=3.14
Alphabets Words Sentences Paragraphs
Alphabets
Digits
Special
Symbols
Constants
Variables
Keywords
Instructions Program
Steps in learning English Language
Steps in learning C Language
26
C Development Environment
DiskPhase 2 :
Preprocessor
program
processes the
code.
DiskCompilerPhase 3 :
Compiler
creates object
code and stores
it on Disk.
Preprocessor
DiskLinkerPhase 4 :
Linker links object
code with libraries,
Creates and
stores it on Disk.
EditorPhase 1 :
Program is
created in the
Editor and
stored on Disk.
Disk
27
LoaderPhase 5 :
:
.
Primary
Memory
Loader puts
Program in
Memory
C P UPhase 6 :
:
.
Primary
Memory
CPU takes each
instruction and
executes it, storing
new data values as
the program executes.
28
From code to executables
Electrical.cpp
Electrical.obj
Electrical.exe
Electrical.bkp
Compiler
Linker
SaveSource File
cs.LIB
Library Files
29
C Development system
1.IDE( Integrated Development Environment)
2.Command Line Development System
C Directories
• TC = The Install program puts all the subdirectories and files in
this directory
• BIN=Executable files store in this sub-directory
• INCLUDE= Header Files (.h file extension)
• LIB=Following Files
a) Library Files
b) Run-Time Object Files
c) Math Libraries
which are combines during Linking process, are stored in this
sub-directory
30
Making An Application
Write the source code
Compile the code into
object files
Link several object files
into an executable
Run the application
We’ll focus on this
part
Don’t worry about
these parts
31
Variable names- Rules
• Should not be a reserved word like int etc..
• Should start with a letter or an
underscore(_)
• Can contain letters, numbers or
underscore.
• No other special characters are allowed
including space
• Variable names are case sensitive
– A and a are different.
32
Brackets
•< > Angle Brackets
•{ } Curly Brackets
•( ) Parenthesis
•[ ] Square Brackets
33
A Simple C Program Example
#include <stdio.h>
main()
{
printf("Programming in C is easy.");
}
Sample Program Output
Programming in C is easy.
Preprocessor
Directive
Header File
Program
Statement
Function (NOTE: no semicolon with
function)
NOTE: All commands in C must be lowercase.
34
1. program execution begins at main()
2. keywords are written in lower-case
3. statements are terminated with a semi-colon
4. text strings are enclosed in double quotes
5. printf() is actually a function (procedure) in C
that is used for printing variables and text
6. The text appears in double quotes "", it is
printed without modification. There are some
exceptions however.
Summary of major points
35
Preprocessor Directives
• The first thing to be checked by the compiler.
• Starts with ‘#’.
• Tell the compiler about specific options that it
needs to be aware of during compilation.
• There are a few compiler directives. But only 2 of
them will be discussed here.
– #include <stdio.h>
• Tell the compiler to include the file stdio.h during
compilation
• Anything in the header file is considered a part of the
program
– #define VALUE 10
• Tell the compiler to substitute the word VALUE with 10
during compilation
36
Header Files
• Messages to Compiler called compiler
directives
• “.h” file extension
• Tell the compiler about definition of words or
phrases used in program
• Have prototype for library function in order to
avoid program error.
Examples:
• stdio.h →(Standard Input/output)
• conio.h →(Console Input/output)
• iostream →(Input Output Stream)
37
Header files (Cont’d)
• The files that are specified in the include
section is called as header file
• These are precompiled files that has some
functions defined in them
• We can call those functions in our program
by supplying parameters
38
Four types of Header Files:
stdio.h
conio.h
ctype.h
math.h
39
Stdio.h (Standard Input/output )
40
conio.h (console input/output)
41
Math.h
42
ctype .h
43
main () Functions
• A C program consists of one or more functions
that contain a group of statements which perform
a specific task.
• C program must at least have one function: the
function main.
• The C programs starting point is identified by the
word main()
• This informs the computer as to where the
program actually starts. The Parenthesis () that
follow the keyword main indicate that there are
no arguments supplied to this program (this will
be examined later on).
• The two braces, { and }, signify the begin and
end segments of the program.
44
main function (Cont’d)
• This is the entry point of a program
• When a file is executed, the start point is
the main function
• From main function the flow goes as per
the programmers choice.
• There may or may not be other functions
written by user in a program
• Main function is compulsory for any c
program
45
Statements
• A specification of an action to be taken by the
computer as the program executes.
• In the previous example the line that terminate with
semicolon ‘;’ is a statement.
– printf ("Programming in C is easy.");
• If there are more than one line then each line is a
statement.
46
Define & Declaration
• Declaration: specifies the type of a variable.
– Example: int local_var;
• Definition: assigning a value to the declared
variable.
– Example: local_var = 5;
• A variable can be declared globally or locally.
• A globally declared variable can be accessed
from all parts of the program.
• A locally declared variable can only be
accessed from inside the function in which
the variable is declared.
47
Example of Define & Declaration:
#include <stdio.h>
#define VALUE 10
int global_var;
void main (void)
{
/* This is the beginning of the program */
int local_var;
local_var = 5;
global_var = local_var + VALUE;
printf (“Total sum is: %dn”, global_var); // Print out the result
}
} Preprocessor
directive
Global variable declaration Comments
Local variable declaration
Variable definition
48
Comments writing in C
language ‘//’ & ‘/*’ ‘*/’
• Explanations or annotations that are included
in a program for documentation and
clarification purpose.
• Completely ignored by the compiler during
compilation and have no effect on program
execution.
• Starts with ‘/*’ and ends with ‘*/’
• Some compiler support comments starting
with ‘//’
49
THANK YOU
50

Contenu connexe

Tendances

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
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
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointersSamiksha Pun
 
Storage class in c
Storage class in cStorage class in c
Storage class in ckash95
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programmingRumman Ansari
 

Tendances (20)

USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
C tokens
C tokensC tokens
C tokens
 
C functions
C functionsC functions
C functions
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
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++
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
C program
C programC program
C program
 
Function in C
Function in CFunction in C
Function in C
 

Similaire à Data Type in C Programming

Similaire à Data Type in C Programming (20)

programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
C material
C materialC material
C material
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
C#
C#C#
C#
 

Dernier

The relationship between iot and communication technology
The relationship between iot and communication technologyThe relationship between iot and communication technology
The relationship between iot and communication technologyabdulkadirmukarram03
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesDIPIKA83
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfNaveenVerma126
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxrealme6igamerr
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid BodyAhmadHajasad2
 
Nodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxNodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxwendy cai
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Akarthi keyan
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfJulia Kaye
 
EPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxEPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxJoseeMusabyimana
 
Test of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxTest of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxHome
 
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...soginsider
 
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchrohitcse52
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxjasonsedano2
 
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxVertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxLMW Machine Tool Division
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging systemgokuldongala
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
Multicomponent Spiral Wound Membrane Separation Model.pdf
Multicomponent Spiral Wound Membrane Separation Model.pdfMulticomponent Spiral Wound Membrane Separation Model.pdf
Multicomponent Spiral Wound Membrane Separation Model.pdfGiovanaGhasary1
 

Dernier (20)

The relationship between iot and communication technology
The relationship between iot and communication technologyThe relationship between iot and communication technology
The relationship between iot and communication technology
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display Devices
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
 
Lecture 4 .pdf
Lecture 4                              .pdfLecture 4                              .pdf
Lecture 4 .pdf
 
Nodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxNodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptx
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part A
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
 
EPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxEPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptx
 
Test of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxTest of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptx
 
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
 
Litature Review: Research Paper work for Engineering
Litature Review: Research Paper work for EngineeringLitature Review: Research Paper work for Engineering
Litature Review: Research Paper work for Engineering
 
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
 
Lecture 2 .pptx
Lecture 2                            .pptxLecture 2                            .pptx
Lecture 2 .pptx
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptx
 
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxVertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging system
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
Multicomponent Spiral Wound Membrane Separation Model.pdf
Multicomponent Spiral Wound Membrane Separation Model.pdfMulticomponent Spiral Wound Membrane Separation Model.pdf
Multicomponent Spiral Wound Membrane Separation Model.pdf
 

Data Type in C Programming

  • 1. DATA TYPES in C Programming 1 Engr. Qazi Shahzad Ali Kyungpook National University, South Korea
  • 2. Four Types of Data 1- Fundamental Data Types 4- User Defined Data Types 2- Data Type Modifiers 3- Derived Data Types 2
  • 3. 1- Fundamental Data Types 1.1- Integer 1.2- Character 1.4- Double 1.5- Void 1.3- Float FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF OTHER DATA TYPES 3
  • 4. 2- Data type modifiers 2.1- Integer Type Modifiers 2.2- Character Type Modifiers 2.3- Floating-point Modifiers 1-They change some properties of the data type 2-The modifiers define the amount of storage allocated to the variable. Like short, double, long 4
  • 5. 3- Derived Data Types 3.1- Arrays 3.3- Pointers 3.4- References 3.5- Constants 3.2- Functions 5
  • 6. 4- User Defined Derived Data Types 4.1- Class 4.2- Structure 4.3- Union 4.4- Enumeration 6 Web link for study of Enumeration http://cplus.about.com/od/introductiontoprogramming/p/enumeration.htm
  • 7. The Decimal & Binary Number System Binary Decimal 0000 0 max number that can be represent by= 2n -1 0001 1 n= number of bits 0010 2 for Example if we use 1 bit then we can represent only 0 & 0011 3 1 decimal number, if 2 bit then 2 and 3, if 3 bit then max 0100 4 up to 7 (23-1=8-1=7), Similarly if we use 8 bits in case of 0101 5 “char data type” then max (28-1=256-1=255),similarly in 0110 6 case of signed 1 bit out of 8 bits are used for plus or minus 0111 7 (0 for +, & 1 for -) so (Max27-1=128-1=127) & (Min-27=-128), 1000 8 1001 9 7
  • 8. Name Description Size* Range* char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4bytes 10 -38 to 1038 (~7 digits) double Double precision floating point number. 8bytes 10 -308 to 10308 (~15 digits) long double Long double precision floating point number. 10bytes 10 -4932 to 104932 (~19 digits) 8
  • 9. Integer Modifier TYPE APPROXIMAT E SIZE MINIMAL RANGE SHORT 2 -32768 to 32767 UNSIGNED SHORT 2 0 to 65,535 SIGNED SHORT 2 Same as short INT 2 -32768 to 32767 UNSIGNED INT 2 0 to 65,535 SIGNED INT 2 Same as int LONG 4 -2,147,483,648 TO 2,147,483,647 UNSIGNED LONG 4 0 to 4,294,967,295 SIGNED LONG 4 Same as long Note: The prefix signed makes the integer type hold negative values also. Unsigned makes the integer not to hold negative values. 9
  • 10. Char Modifier TYPE APPROXIMATESIZE MINIMALRANGE CHAR 1 -128 to 127 UNSIGNED CHAR 1 0 to 255 SIGNED CHAR 1 Same as char Floating Point Modifier TYPE APPROXIMATE SIZE DIGITS OF PRECISION FLOAT 4 7 DOUBLE 8 15 LONG DOUBLE 10 19 10
  • 11. How to declare them { int Count; Count = 5; } { float Miles; Miles = 5.6; } { char Letter; Letter = 'x'; } Integer Declaration: Float Declaration: Character Declaration: 11
  • 12. Signedness • If signed, the most significant bit designates a positive or negative value leaving the remaining bits to be used to hold a designated value. Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. • An unsigned character, depending on the code page, might access an extended range of characters from 0 → +255, instead of that accessible by a signed char from −128 → +127, or it might simply be used as a small integer. The standard requires char, signed char, unsigned char to be different types. Since most standardized string functions take pointers to plain char, many C compilers correctly complain if one of the other character types is used for strings passed to these functions  unsigned int x;  signed int y;  int z; /* Same as "signed int" */  unsigned char grey;  signed char white; 12
  • 13. INTEGER DATA TYPE Integers are whole numbers. Countable like # of rooms in a house C has 3 classes/Modifier of integer 1. short int 2. int 3. long int 13
  • 14. FLOAT DATA TYPE • Decimal/Exponential Number • Values that are measureable like length of room 145.25 inches • Store memory in two parts 1. Mantissa (The value of number takes 3 bytes) 2. Exponent (Power of number takes 1 byte) Exp: 12345 will be represented as 1.2345e4 (4 is exponent & 1.2345 is number) • They have two advantages 1: they can represent values between integers. 2: they can represent a much greater range of values. 14
  • 15. CHARACTER DATA TYPE • Includes every printable character on the standard English language keyboard • We can show ASCII code with the help of characters • Example of characters: – Numeric digits: 0 - 9 – Lowercase/uppercase letters: a - z and A - Z – Space (blank) – Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc 15
  • 16. DOUBLE DATA TYPE • The data type double is also used for handling floating-point numbers. But it is treated as a distinct data type because, it occupies twice as much memory as type float, and stores floating-point numbers with much larger range and precision. It is slower that type float. 16
  • 17. VOID DATA TYPE • It specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared. • It is used when program or calculation does not require any value but the syntax needs it. 17
  • 18. C-Software Tools In order to write a C program you will need: • An Editor - Something to write code in. • A Compiler - Converts the code into Machine Language (object files). • A Linker – Combines several object files into one executable (written in Machine Language). • A Debugger – Provides supervised execution of the code. 18
  • 19. • A token is a language element that can be used in forming higher level language constructs • Equivalent to a ‘word’ in English language • Several types of tokens can be used to build a higher level C language construct such as expressions and statements • There are 6 kinds of tokens in C: – Reserved words (keywords) – Identifiers – Constants – String literals (constant) – Punctuators – Operators Token 19
  • 20. Reserved Words/Keywords • Keywords that identify language entities such as statements, data types, language attributes, etc. • Have special meaning to the compiler • cannot be used as identifiers/variable in our program. • Should be typed in lowercase. • White in color • Total numbers=32 • Example: const, double, int, main, void, while, for, else (etc..) 20
  • 21. Identifiers • Words used to represent certain program entities (program variables, function names, etc). • Example: – int my_name; • my_name is an identifier used as a program variable – void CalculateTotal(int value) • CalculateTotal is an identifier used as a function name 21
  • 22. Punctuators (separators) • Symbols used to separate different parts of the C program. • These punctuators include: – [ ] ( ) { } , ; “: * # • Usage example: 22 void main (void) { int num=10; printf (“%d”,num); }
  • 23. C Constants Primary Constants Integer Constant, Real Constant, Character Constant Secondary Constants Array Pointer Structure Union Enum.etc 23
  • 24. Constants • Entities that appear in the program code as fixed values. • 3 types of Primary constants: – Integer constants • Positive or negative whole numbers with no fractional part • Example: – const int MAX_NUM = 10; – const int MIN_NUM = -90; – Floating-point constants • Positive or negative decimal numbers with an integer part, a decimal point and a fractional part • Example: – const double VAL = 0.5877e2; (stands for 0.5877 x 102) – Character constants • A single character enclosed in a single inverted commas • Example: – const char letter = ‘n’; – const char number = ‘1’; 24
  • 25. String Literal (Constant) • A sequence of Character consisting of Alphabets,didgits,and special character in double quotation mark is call String Constant Example “Pakistan” and “Lahore-58900” 25 Const Qualifier • The data that follow the keyword “const” cannot change its value during execution of program. This can be replaced by Define directive Example : const float p=3.14
  • 26. Alphabets Words Sentences Paragraphs Alphabets Digits Special Symbols Constants Variables Keywords Instructions Program Steps in learning English Language Steps in learning C Language 26
  • 27. C Development Environment DiskPhase 2 : Preprocessor program processes the code. DiskCompilerPhase 3 : Compiler creates object code and stores it on Disk. Preprocessor DiskLinkerPhase 4 : Linker links object code with libraries, Creates and stores it on Disk. EditorPhase 1 : Program is created in the Editor and stored on Disk. Disk 27
  • 28. LoaderPhase 5 : : . Primary Memory Loader puts Program in Memory C P UPhase 6 : : . Primary Memory CPU takes each instruction and executes it, storing new data values as the program executes. 28
  • 29. From code to executables Electrical.cpp Electrical.obj Electrical.exe Electrical.bkp Compiler Linker SaveSource File cs.LIB Library Files 29
  • 30. C Development system 1.IDE( Integrated Development Environment) 2.Command Line Development System C Directories • TC = The Install program puts all the subdirectories and files in this directory • BIN=Executable files store in this sub-directory • INCLUDE= Header Files (.h file extension) • LIB=Following Files a) Library Files b) Run-Time Object Files c) Math Libraries which are combines during Linking process, are stored in this sub-directory 30
  • 31. Making An Application Write the source code Compile the code into object files Link several object files into an executable Run the application We’ll focus on this part Don’t worry about these parts 31
  • 32. Variable names- Rules • Should not be a reserved word like int etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive – A and a are different. 32
  • 33. Brackets •< > Angle Brackets •{ } Curly Brackets •( ) Parenthesis •[ ] Square Brackets 33
  • 34. A Simple C Program Example #include <stdio.h> main() { printf("Programming in C is easy."); } Sample Program Output Programming in C is easy. Preprocessor Directive Header File Program Statement Function (NOTE: no semicolon with function) NOTE: All commands in C must be lowercase. 34
  • 35. 1. program execution begins at main() 2. keywords are written in lower-case 3. statements are terminated with a semi-colon 4. text strings are enclosed in double quotes 5. printf() is actually a function (procedure) in C that is used for printing variables and text 6. The text appears in double quotes "", it is printed without modification. There are some exceptions however. Summary of major points 35
  • 36. Preprocessor Directives • The first thing to be checked by the compiler. • Starts with ‘#’. • Tell the compiler about specific options that it needs to be aware of during compilation. • There are a few compiler directives. But only 2 of them will be discussed here. – #include <stdio.h> • Tell the compiler to include the file stdio.h during compilation • Anything in the header file is considered a part of the program – #define VALUE 10 • Tell the compiler to substitute the word VALUE with 10 during compilation 36
  • 37. Header Files • Messages to Compiler called compiler directives • “.h” file extension • Tell the compiler about definition of words or phrases used in program • Have prototype for library function in order to avoid program error. Examples: • stdio.h →(Standard Input/output) • conio.h →(Console Input/output) • iostream →(Input Output Stream) 37
  • 38. Header files (Cont’d) • The files that are specified in the include section is called as header file • These are precompiled files that has some functions defined in them • We can call those functions in our program by supplying parameters 38
  • 39. Four types of Header Files: stdio.h conio.h ctype.h math.h 39
  • 44. main () Functions • A C program consists of one or more functions that contain a group of statements which perform a specific task. • C program must at least have one function: the function main. • The C programs starting point is identified by the word main() • This informs the computer as to where the program actually starts. The Parenthesis () that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on). • The two braces, { and }, signify the begin and end segments of the program. 44
  • 45. main function (Cont’d) • This is the entry point of a program • When a file is executed, the start point is the main function • From main function the flow goes as per the programmers choice. • There may or may not be other functions written by user in a program • Main function is compulsory for any c program 45
  • 46. Statements • A specification of an action to be taken by the computer as the program executes. • In the previous example the line that terminate with semicolon ‘;’ is a statement. – printf ("Programming in C is easy."); • If there are more than one line then each line is a statement. 46
  • 47. Define & Declaration • Declaration: specifies the type of a variable. – Example: int local_var; • Definition: assigning a value to the declared variable. – Example: local_var = 5; • A variable can be declared globally or locally. • A globally declared variable can be accessed from all parts of the program. • A locally declared variable can only be accessed from inside the function in which the variable is declared. 47
  • 48. Example of Define & Declaration: #include <stdio.h> #define VALUE 10 int global_var; void main (void) { /* This is the beginning of the program */ int local_var; local_var = 5; global_var = local_var + VALUE; printf (“Total sum is: %dn”, global_var); // Print out the result } } Preprocessor directive Global variable declaration Comments Local variable declaration Variable definition 48
  • 49. Comments writing in C language ‘//’ & ‘/*’ ‘*/’ • Explanations or annotations that are included in a program for documentation and clarification purpose. • Completely ignored by the compiler during compilation and have no effect on program execution. • Starts with ‘/*’ and ends with ‘*/’ • Some compiler support comments starting with ‘//’ 49