SlideShare a Scribd company logo
1 of 29
Modelling 2A
C Lecture 2
Chapter 4
Philip Robinson
2015
Fundamental C Data Types
• Bits, Bytes and Words
• The smallest unit of memory is called a bit.
–It can hold one of two values: 0 or 1.
–The bit is the basic block of computer memory.
• A byte is 8 bits.
• A word is a natural unit of memory for a given
computer design.
–Some computers have 32-bit words and more recent
computers have 64-bit words.
–The larger a word is the more information it can store.
• The C language provides a number of fundamental
data types. 2
Fundamental C Data Types
3
Fundamental C Data Types
• The concept of types is important in that it determines
the space necessary to store the values of a
particular type, and which valid operations are defined
on certain types.
• Whenever an identifier, (variable name, function
name, formal parameter name, user-defined type
name, etc.) is declared to be of a certain type, the
type name always comes first, followed by the
identifier.
<type> <identifier> …
4
Void and Scalar data types
• The void data type indicates ‘nothing’.
• The void keyword may be used
– to indicate that a function does not return a value,
e.g. void do_it(int a)
– To indicate that no parameters are required in the
function definition, for e.g., int do_it(void)
Scalar Types
• Scalar types are largest group and are called scalar
because the types have only one dimension (one
value).
• There are three sub-groups of scalar types, namely:
arithmetic, pointer and enumerated types.
5
Arithmetic data types
• These types are numeric types on which arithmetic operations,
amongst others, can be performed.
• Integer Types
– An integer type is a whole number.
– The sizes of the integer data types are very implementation
dependant, and are therefore the types most closely associated
with the host computer architecture.
– The sizes of these types range from 8 bits to 64 bits on some
computers.
– Bit operations are allowed
– In the following table, the square brackets […] indicate an optional
word, since without it the compiler will accept it anyway as the
default.
6
Arithmetic data types
7
Type Bits Description
_Bool ? Boolean type, 0 = false, 1 = true
char 8 character type (-128→127 OR 0 →255)
signed char 8 Signed character type (-128→127)
unsigned char 8 Unsigned character type (0→255)
[signed] short [int] 16 Signed short integer type (-32767→32768
unsigned short [int] 16 Unsigned short integer type (0→65535)
[signed] int 32 Signed normal integer type (−2147483647 →
+2147483647)
Unsigned int 32 Unsigned normal integer type (0 → +4294967295)
long long [int] 64 Signed long integer type (−9223372036854775808
→ +9223372036854775808)
unsigned long long 64 Unsigned long integer type (0 →
+18446744073709551616)
Arithmetic data types
8
Floating Point Types
• A floating-point number more or less corresponds to a real
number.
• Integers are whole number, whereas floating point numbers
can represent both whole and fractional numbers.
• No bit operations are allowed on floating point types.
• The three floating point types are:
– float (1×10−37 → 1×1037)
– double (1×10−308 → 1×10308)
– long double (1×10−4932 → 1×104932)
• Floating point numbers can represent a much larger range of
values than integers, but tend to be slower than integer
operations.
Arithmetic data types
• Floating point types
– Single precision floating point number (float)
• Occupies 32 bits typically
– Double precision floating point number (double)
• Occupies 64 bits typically
– float: approx seven digits of precision and takes four bytes to store.
– double: approx. 13 digits of precision and takes eight bytes to store. 9
Enumerated Types
• In C, enumerated types indicate a user-defined type which
simply assigns names to a range of int types.
• The general format for the creation of enumerated types is:
• enum <identifier> {<enumeration list>} ;
• Example
– enum WeekDays {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
• In the above example a type with the name enum WeekDays
was created.
– The value of Sun is 0, Mon is 1, ... Sat is 6.
– enum WeekDays today = Wed;
10
Variables
• Simple variables are declared by first specifying the
type and then the name of the variable.
• Variables are declared when we want to use them at
a certain point, but the definition of the variable only
occurs later in the program (on the external level), or
it is defined in another file (on the external level).
• Attempts to make use of a variable that has not
been defined will result in an error. This includes
reading from the variable or assigning a value to the
variable.
• The general syntax and some derivations for
defining variables are shown in the following table:
11
Variables
12
<type> <name>; int count; Simple declaration
<type> *<name>; int *count; Pointer variable
declaration
<type> <name>,
<name>;
int count1, count2; Multiple variable
declaration
<type> <name>[<nr of
elements>];
int data[10]; Array variable
declaration
• Once a variable is declared it is possible to initialize that variable
with a value in the same statement using the assignment operator
– Eg. int test = 10;
• The sizeof function will return the number of bytes a given
variable occupies
Type Conversions
• Conversions between different types are commonplace in most
programming languages.
• Sometimes these conversions takes place without the
programmer being aware of it.
• This type of conversion is termed an implicit conversion.
• A forced conversion, where the programmer forces the
compiler to perform a type conversion, is called casting in C
terminology.
13
Type Conversions
Implicit Conversions
• Implicit conversion most often occur in arithmetic expressions,
where different integer types, and possibly floating point types
may be mixed.
• The simple rule is that when an arithmetic operation is
performed on two operands of different types, the one with the
smaller internal representation, will be converted to the other.
• If a double operand is multiplied by an int operand (for
example), the value of the int type operand are first converted
to a double value before the multiplication is performed.
14
Type Casting
• Often the programmer needs to force the conversion of one
type to a similar type.
• This can be accomplished by means of the prefixed cast
operator, which consists of the name of an type enclosed by
parenthesis:
• (type) <value or variable>
• Example
– int x = -100;
– double y = (double) x;
15
Constants
• Constants refer to fixed values that may not be altered by the
program.
• The C pre-processor allows for a means to define constants
which can then be used later in a program.
• The constant data types must be defined before the main
function.
• The constant name is normally written in capitals and does
not have a semi-colon at the end. The use of constants is
mainly for making your programs easier to be understood and
modified by others and yourself in the future.
• The format of a constant is shown below:
#define CONSTANTNAME value
• for example:
#define VAT 0.14
#define PI 3.14
• First comes #define, than the name followed by the value.
16
Printf() Function
• The printf function do differ from the sort of functions that you create
for yourself in that they can take a variable number of parameters.
• In the case of printf the first parameter is always a string (e.g. "Hello
World") but after that you can include as many parameters of any type
as required by your specifiers.
• The format for using the printf () function is:
printf(string,variable,variable,variable...)
• where the ... means you can carry on writing a list of variables
separated by commas as long as required.
• The string is critical because it specifies the type of each variable in
the list and how you want it printed.
• The string is usually called the control string or the format string.
• The way that this works is that printf scans the string from left to right
and prints on the screen, or any suitable output device, any
characters it encounters - except when it reaches a % character.
17
Printf() Function
• The % character is a signal that what follows it is a specification for
how the next variable in the list of variables should be printed.
• printf uses this information to convert and format the value that was
passed to the function by the variable and then moves on to process
the rest of the control string and anymore variables it might specify.
• For example:
printf("Hello World");
• only has a control string and, as this contains no % characters it
results in Hello World being displayed and doesn't need to display
any variable values.
• The specifier %d means convert the next value to a signed decimal
integer and so: printf("Total = %d",total);
• will print Total = and then the value passed by total as a decimal
integer.
• They can be variables, constants or even expressions that are
evaluated first before the value is printed.
18
ANSI C Conversion Specifications for
scanf() and printf()
19
Converstion specification Output
%c Single character
%d, i Signed decimal integer
%e Floating point number e-notation
%E Floating point number E-notation
%f, g Floating point number, shorter
decimal notation
%s Character string
%x Hexidecimal number
Character data type
• A char variable is used to store a single character.
• A character constant is formed by enclosing the character
within a pair of single quotation marks.
• C represents letters as a number which is related to the
symbol or character being stored via the ASCII table
• Example:
– char x = ‘b’;
– In this case the ASCII value of the character ‘b’ is stored in x
20
Escape Characters
• Escape characters are used when printing out characters using
printf that have a functional significance in C such as the  or %
characters.
• They are produced by first typing a  followed by the escape
character
21
Escape Sequence Produced Character
n New line
 
” ”
’ ’
t Horizontal tab
a Audible Alert
Operands and Expressions
• Every expression in C results in a value whose type depends
on the operands in the expression.
• Operands
– Num = a + b – c
assignment Operators
Expression
• Operand values in C may be converted implicitly to match the
types of other operands according to fixed rules.
• Every operator has a precedence level and associativity.
• Operators cannot perform their functions on all data types, but
have a limited number of operand types with which they can
function.
• Conditional operators return type int with either the value 0 or
1, which corresponds to FALSE and TRUE respectively. 22
Operands and Expressions
23
C Operators
24
Arithmetic Operators
• The two arithmetic operators that are used frequently are ++ and
--.
• You can place these in front or behind variables.
• ++ specifies increment,
• -- specifies decrement
• If the operator is placed in front, it is prefix if it is placed behind, it
is postfix.
• Prefix means, increment before any operations are performed,
postfix is increment afterwards. These are important
considerations when using these operators.
• C allows compound assignment operators (*=, +=, /=, -=).
– Eg: int i = 5; i *= 5; The int i would have the value 25 after the operation.
– This is identical to writing: int i = 5; i = i* 5;
25
Integer and Floating-point conversion
• When an integer is assigned to a floating point number its value
doesn’t change but it will be represented as a floating point
number in memory.
• When a floating-point number is assigned to an integer the values
that come after the decimal place will be truncated and lost.
• When an operator is applied to two operands it is important to
take note of the data type of the operands.
• If both operands are integers the result of the operation will be an
integer.
• If one of the operands is a floating-point number the result will be
a floating-point number.
• This is primarily important when performing division as when two
integers are divided an integer division will take place and the
result will be an integer with the portion of the result that falls after
the decimal being truncated.
26
Logical Operators
• Logical operators simulate boolean algebra in C.
• Examples of Logical/Boolean Operators:
– && is AND
– || is OR
– ! is NOT
– &, |, and ^.
• For example, && is used to compare two objects with AND: x != 0
&& y != 0
• Expressions involving logical operators undergo Short-Circuit
Evaluation. Take the above example into consideration. If x != 0
evaluates to false, the whole statement is false regardless of the
outcome of y != 0.
27
Relational Operators
• The relational operators from mathematics < and the > are used
for comparing two objects.
• There are six possibilities in C: <, <=, >, >=, !=, and ==. The first
four a self-explanatory, the != stands for "not equals to" and == is
"equivalent to".
• NOTE: a = b is different from a == b. Most C compilers will allow
both statements to be used in conditionals like if, but they have
two completely different meanings.
28
Bit Operators
• Bit Shifts
– << is the left shift operator
– Eg. char x = 8; (x is 000010002) x<<2; results in x having a value of 32
(001000002)
– >> is the right shift operator
• Logical Operators
– & is the AND operator
– | is the OR operator
– ~ is the complement operator
– ^ is the XOR operator
29
01001000 |
10111000 =
--------------
11111000
01001000 &
10111000 =
---------------
00001000
01110010 ^
10101010 =
--------------
11011000
~01110010
--------------
~10001101
char x = 72;
char y = 184;
y = x&y;
y will be equal to 8

More Related Content

What's hot

Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
Tony Apreku
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
Manisha Keim
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 

What's hot (19)

Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
C language
C languageC language
C language
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Data type in c
Data type in cData type in c
Data type in c
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C programming Ms. Pranoti Doke
C programming Ms. Pranoti DokeC programming Ms. Pranoti Doke
C programming Ms. Pranoti Doke
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
C – A Programming Language- I
C – A Programming Language- IC – A Programming Language- I
C – A Programming Language- I
 
Pc module1
Pc module1Pc module1
Pc module1
 
Data types
Data typesData types
Data types
 

Viewers also liked

Chapter 02 Data Types
Chapter 02   Data TypesChapter 02   Data Types
Chapter 02 Data Types
Nathan Yeung
 
1 introduction to problem solving and programming
1 introduction to problem solving and programming1 introduction to problem solving and programming
1 introduction to problem solving and programming
Rheigh Henley Calderon
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
9840596838
 
Chapter 1 Introduction To Computers
Chapter 1 Introduction To ComputersChapter 1 Introduction To Computers
Chapter 1 Introduction To Computers
norzaini
 
Digital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 unitsDigital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 units
Lekashri Subramanian
 
8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 
8085 microprocessor architecture ppt
8085 microprocessor architecture ppt8085 microprocessor architecture ppt
8085 microprocessor architecture ppt
Parvesh Gautam
 
Vlsi lab viva question with answers
Vlsi lab viva question with answersVlsi lab viva question with answers
Vlsi lab viva question with answers
Ayesha Ambreen
 
Microprocessors and microcontrollers short answer questions and answers
Microprocessors and microcontrollers short answer questions and answersMicroprocessors and microcontrollers short answer questions and answers
Microprocessors and microcontrollers short answer questions and answers
Abhijith Augustine
 

Viewers also liked (15)

Chapter 02 Data Types
Chapter 02   Data TypesChapter 02   Data Types
Chapter 02 Data Types
 
Class 3 Binary & Hexadecimal
Class 3 Binary & HexadecimalClass 3 Binary & Hexadecimal
Class 3 Binary & Hexadecimal
 
1 introduction to problem solving and programming
1 introduction to problem solving and programming1 introduction to problem solving and programming
1 introduction to problem solving and programming
 
Basic Computer Organization and Design
Basic Computer Organization and DesignBasic Computer Organization and Design
Basic Computer Organization and Design
 
8086 addressing modes
8086 addressing modes8086 addressing modes
8086 addressing modes
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Question bank
Question bankQuestion bank
Question bank
 
1. basic theories of information
1. basic theories of information1. basic theories of information
1. basic theories of information
 
Chapter 1 Introduction To Computers
Chapter 1 Introduction To ComputersChapter 1 Introduction To Computers
Chapter 1 Introduction To Computers
 
Digital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 unitsDigital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 units
 
8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware
 
Logic gates - AND, OR, NOT, NOR, NAND, XOR, XNOR Gates.
Logic gates - AND, OR, NOT, NOR, NAND, XOR, XNOR Gates.Logic gates - AND, OR, NOT, NOR, NAND, XOR, XNOR Gates.
Logic gates - AND, OR, NOT, NOR, NAND, XOR, XNOR Gates.
 
8085 microprocessor architecture ppt
8085 microprocessor architecture ppt8085 microprocessor architecture ppt
8085 microprocessor architecture ppt
 
Vlsi lab viva question with answers
Vlsi lab viva question with answersVlsi lab viva question with answers
Vlsi lab viva question with answers
 
Microprocessors and microcontrollers short answer questions and answers
Microprocessors and microcontrollers short answer questions and answersMicroprocessors and microcontrollers short answer questions and answers
Microprocessors and microcontrollers short answer questions and answers
 

Similar to Lecture 2

C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptx
Murali M
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 

Similar to Lecture 2 (20)

CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
C programming language
C programming languageC programming language
C programming language
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptx
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
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
 
C#
C#C#
C#
 
Chapter02.PPT
Chapter02.PPTChapter02.PPT
Chapter02.PPT
 
c programming 2nd chapter pdf.PPT
c programming 2nd chapter pdf.PPTc programming 2nd chapter pdf.PPT
c programming 2nd chapter pdf.PPT
 
A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...
 
Introduction to Problem Solving C Programming
Introduction to Problem Solving C ProgrammingIntroduction to Problem Solving C Programming
Introduction to Problem Solving C Programming
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
What is Data Types and Functions?
What is Data Types and Functions?What is Data Types and Functions?
What is Data Types and Functions?
 
C programming language
C programming languageC programming language
C programming language
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

Lecture 2

  • 1. Modelling 2A C Lecture 2 Chapter 4 Philip Robinson 2015
  • 2. Fundamental C Data Types • Bits, Bytes and Words • The smallest unit of memory is called a bit. –It can hold one of two values: 0 or 1. –The bit is the basic block of computer memory. • A byte is 8 bits. • A word is a natural unit of memory for a given computer design. –Some computers have 32-bit words and more recent computers have 64-bit words. –The larger a word is the more information it can store. • The C language provides a number of fundamental data types. 2
  • 4. Fundamental C Data Types • The concept of types is important in that it determines the space necessary to store the values of a particular type, and which valid operations are defined on certain types. • Whenever an identifier, (variable name, function name, formal parameter name, user-defined type name, etc.) is declared to be of a certain type, the type name always comes first, followed by the identifier. <type> <identifier> … 4
  • 5. Void and Scalar data types • The void data type indicates ‘nothing’. • The void keyword may be used – to indicate that a function does not return a value, e.g. void do_it(int a) – To indicate that no parameters are required in the function definition, for e.g., int do_it(void) Scalar Types • Scalar types are largest group and are called scalar because the types have only one dimension (one value). • There are three sub-groups of scalar types, namely: arithmetic, pointer and enumerated types. 5
  • 6. Arithmetic data types • These types are numeric types on which arithmetic operations, amongst others, can be performed. • Integer Types – An integer type is a whole number. – The sizes of the integer data types are very implementation dependant, and are therefore the types most closely associated with the host computer architecture. – The sizes of these types range from 8 bits to 64 bits on some computers. – Bit operations are allowed – In the following table, the square brackets […] indicate an optional word, since without it the compiler will accept it anyway as the default. 6
  • 7. Arithmetic data types 7 Type Bits Description _Bool ? Boolean type, 0 = false, 1 = true char 8 character type (-128→127 OR 0 →255) signed char 8 Signed character type (-128→127) unsigned char 8 Unsigned character type (0→255) [signed] short [int] 16 Signed short integer type (-32767→32768 unsigned short [int] 16 Unsigned short integer type (0→65535) [signed] int 32 Signed normal integer type (−2147483647 → +2147483647) Unsigned int 32 Unsigned normal integer type (0 → +4294967295) long long [int] 64 Signed long integer type (−9223372036854775808 → +9223372036854775808) unsigned long long 64 Unsigned long integer type (0 → +18446744073709551616)
  • 8. Arithmetic data types 8 Floating Point Types • A floating-point number more or less corresponds to a real number. • Integers are whole number, whereas floating point numbers can represent both whole and fractional numbers. • No bit operations are allowed on floating point types. • The three floating point types are: – float (1×10−37 → 1×1037) – double (1×10−308 → 1×10308) – long double (1×10−4932 → 1×104932) • Floating point numbers can represent a much larger range of values than integers, but tend to be slower than integer operations.
  • 9. Arithmetic data types • Floating point types – Single precision floating point number (float) • Occupies 32 bits typically – Double precision floating point number (double) • Occupies 64 bits typically – float: approx seven digits of precision and takes four bytes to store. – double: approx. 13 digits of precision and takes eight bytes to store. 9
  • 10. Enumerated Types • In C, enumerated types indicate a user-defined type which simply assigns names to a range of int types. • The general format for the creation of enumerated types is: • enum <identifier> {<enumeration list>} ; • Example – enum WeekDays {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; • In the above example a type with the name enum WeekDays was created. – The value of Sun is 0, Mon is 1, ... Sat is 6. – enum WeekDays today = Wed; 10
  • 11. Variables • Simple variables are declared by first specifying the type and then the name of the variable. • Variables are declared when we want to use them at a certain point, but the definition of the variable only occurs later in the program (on the external level), or it is defined in another file (on the external level). • Attempts to make use of a variable that has not been defined will result in an error. This includes reading from the variable or assigning a value to the variable. • The general syntax and some derivations for defining variables are shown in the following table: 11
  • 12. Variables 12 <type> <name>; int count; Simple declaration <type> *<name>; int *count; Pointer variable declaration <type> <name>, <name>; int count1, count2; Multiple variable declaration <type> <name>[<nr of elements>]; int data[10]; Array variable declaration • Once a variable is declared it is possible to initialize that variable with a value in the same statement using the assignment operator – Eg. int test = 10; • The sizeof function will return the number of bytes a given variable occupies
  • 13. Type Conversions • Conversions between different types are commonplace in most programming languages. • Sometimes these conversions takes place without the programmer being aware of it. • This type of conversion is termed an implicit conversion. • A forced conversion, where the programmer forces the compiler to perform a type conversion, is called casting in C terminology. 13
  • 14. Type Conversions Implicit Conversions • Implicit conversion most often occur in arithmetic expressions, where different integer types, and possibly floating point types may be mixed. • The simple rule is that when an arithmetic operation is performed on two operands of different types, the one with the smaller internal representation, will be converted to the other. • If a double operand is multiplied by an int operand (for example), the value of the int type operand are first converted to a double value before the multiplication is performed. 14
  • 15. Type Casting • Often the programmer needs to force the conversion of one type to a similar type. • This can be accomplished by means of the prefixed cast operator, which consists of the name of an type enclosed by parenthesis: • (type) <value or variable> • Example – int x = -100; – double y = (double) x; 15
  • 16. Constants • Constants refer to fixed values that may not be altered by the program. • The C pre-processor allows for a means to define constants which can then be used later in a program. • The constant data types must be defined before the main function. • The constant name is normally written in capitals and does not have a semi-colon at the end. The use of constants is mainly for making your programs easier to be understood and modified by others and yourself in the future. • The format of a constant is shown below: #define CONSTANTNAME value • for example: #define VAT 0.14 #define PI 3.14 • First comes #define, than the name followed by the value. 16
  • 17. Printf() Function • The printf function do differ from the sort of functions that you create for yourself in that they can take a variable number of parameters. • In the case of printf the first parameter is always a string (e.g. "Hello World") but after that you can include as many parameters of any type as required by your specifiers. • The format for using the printf () function is: printf(string,variable,variable,variable...) • where the ... means you can carry on writing a list of variables separated by commas as long as required. • The string is critical because it specifies the type of each variable in the list and how you want it printed. • The string is usually called the control string or the format string. • The way that this works is that printf scans the string from left to right and prints on the screen, or any suitable output device, any characters it encounters - except when it reaches a % character. 17
  • 18. Printf() Function • The % character is a signal that what follows it is a specification for how the next variable in the list of variables should be printed. • printf uses this information to convert and format the value that was passed to the function by the variable and then moves on to process the rest of the control string and anymore variables it might specify. • For example: printf("Hello World"); • only has a control string and, as this contains no % characters it results in Hello World being displayed and doesn't need to display any variable values. • The specifier %d means convert the next value to a signed decimal integer and so: printf("Total = %d",total); • will print Total = and then the value passed by total as a decimal integer. • They can be variables, constants or even expressions that are evaluated first before the value is printed. 18
  • 19. ANSI C Conversion Specifications for scanf() and printf() 19 Converstion specification Output %c Single character %d, i Signed decimal integer %e Floating point number e-notation %E Floating point number E-notation %f, g Floating point number, shorter decimal notation %s Character string %x Hexidecimal number
  • 20. Character data type • A char variable is used to store a single character. • A character constant is formed by enclosing the character within a pair of single quotation marks. • C represents letters as a number which is related to the symbol or character being stored via the ASCII table • Example: – char x = ‘b’; – In this case the ASCII value of the character ‘b’ is stored in x 20
  • 21. Escape Characters • Escape characters are used when printing out characters using printf that have a functional significance in C such as the or % characters. • They are produced by first typing a followed by the escape character 21 Escape Sequence Produced Character n New line ” ” ’ ’ t Horizontal tab a Audible Alert
  • 22. Operands and Expressions • Every expression in C results in a value whose type depends on the operands in the expression. • Operands – Num = a + b – c assignment Operators Expression • Operand values in C may be converted implicitly to match the types of other operands according to fixed rules. • Every operator has a precedence level and associativity. • Operators cannot perform their functions on all data types, but have a limited number of operand types with which they can function. • Conditional operators return type int with either the value 0 or 1, which corresponds to FALSE and TRUE respectively. 22
  • 25. Arithmetic Operators • The two arithmetic operators that are used frequently are ++ and --. • You can place these in front or behind variables. • ++ specifies increment, • -- specifies decrement • If the operator is placed in front, it is prefix if it is placed behind, it is postfix. • Prefix means, increment before any operations are performed, postfix is increment afterwards. These are important considerations when using these operators. • C allows compound assignment operators (*=, +=, /=, -=). – Eg: int i = 5; i *= 5; The int i would have the value 25 after the operation. – This is identical to writing: int i = 5; i = i* 5; 25
  • 26. Integer and Floating-point conversion • When an integer is assigned to a floating point number its value doesn’t change but it will be represented as a floating point number in memory. • When a floating-point number is assigned to an integer the values that come after the decimal place will be truncated and lost. • When an operator is applied to two operands it is important to take note of the data type of the operands. • If both operands are integers the result of the operation will be an integer. • If one of the operands is a floating-point number the result will be a floating-point number. • This is primarily important when performing division as when two integers are divided an integer division will take place and the result will be an integer with the portion of the result that falls after the decimal being truncated. 26
  • 27. Logical Operators • Logical operators simulate boolean algebra in C. • Examples of Logical/Boolean Operators: – && is AND – || is OR – ! is NOT – &, |, and ^. • For example, && is used to compare two objects with AND: x != 0 && y != 0 • Expressions involving logical operators undergo Short-Circuit Evaluation. Take the above example into consideration. If x != 0 evaluates to false, the whole statement is false regardless of the outcome of y != 0. 27
  • 28. Relational Operators • The relational operators from mathematics < and the > are used for comparing two objects. • There are six possibilities in C: <, <=, >, >=, !=, and ==. The first four a self-explanatory, the != stands for "not equals to" and == is "equivalent to". • NOTE: a = b is different from a == b. Most C compilers will allow both statements to be used in conditionals like if, but they have two completely different meanings. 28
  • 29. Bit Operators • Bit Shifts – << is the left shift operator – Eg. char x = 8; (x is 000010002) x<<2; results in x having a value of 32 (001000002) – >> is the right shift operator • Logical Operators – & is the AND operator – | is the OR operator – ~ is the complement operator – ^ is the XOR operator 29 01001000 | 10111000 = -------------- 11111000 01001000 & 10111000 = --------------- 00001000 01110010 ^ 10101010 = -------------- 11011000 ~01110010 -------------- ~10001101 char x = 72; char y = 184; y = x&y; y will be equal to 8