0% found this document useful (0 votes)
16 views13 pages

Lecture 05

The document discusses C/C++ data types including integers, floating point numbers, characters, and strings. It also covers constants and how they are represented in C/C++.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Lecture 05

The document discusses C/C++ data types including integers, floating point numbers, characters, and strings. It also covers constants and how they are represented in C/C++.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1

Lecture 5

Constants, Expressions, and Data types


2

Unit Objectives
• List the various C data types
• Identify what type a constant is
• Know how to write constants in the
appropriate C++ syntax
• Know the C++ operators and their order of
operations
• Write basic output statements of text and
constants using cout
3

C/C++ Program Format/Structure


/ * Anything between slash-star and
• Comments star-slash i s ignored even across
multiple lines of t ext or code * /
– Anywhere in the code
– C-Style => "/*" and "*/" / / Anything a f t e r " / / " i s ignored on a l i n e

– C++ Style => "//" / / #includes allow access to l i b r a r y functions


• Compiler Directives #include <iostream>
#include <cmath>
– #includes tell compiler what other library using namespace std;

functions you plan on using / / Code i s organized into units called functions
– 'using namespace std;' -- Just do it for now! void printName()
{
• main() function }
cout << "Tommy Trojan" << endl;

– Starting point of execution for the program


/ / Execution always starts a t the main() function
– All code/statements in C must be inside a i n t main()
function {
cout << "Hello: " << endl;
– Statements execute one after the next and printName();
printName();
end with a semicolon (;) return 0;
– Ends with a 'return 0;' statement }

• Other functions Hello:


– printName() is a function that can be Tommy Trojan
"called"/"invoked" from main or any other Tommy Trojan
function
4

Review C Integer Data Types


– Integer Types (signed by default… unsigned with optional
leading keyword)

C Type C Type (Unsigned) Bytes Bits Signed Range Unsigned


(Signed) Range
char unsigned char 1 8 -128 to +127 0 to 255
short unsigned short 2 16 -32768 to +32767 0 to 65535
int unsigned int 4 32 -2 billion to 0 to 4 billion
+2 billion
long unsigned long 8 64 -8*1018 to +8*1018 0 to 16*1018
5

Review Text Rep.


• Text characters are usually represented with
some kind of binary code (mapping of
character to a binary number such as 'a' =
01100001 bin = 97 dec)
• ASCII = Traditionally an 8-bit code
– How many combinations (i.e., characters)?
– English only
• UNICODE = 16-bit code
– How many combinations?
– Most languages w/ an alphabet
• In C/C++ a single printing/text character
must appear between single-quotes (')
– Example: 'a', '!', 'Z'
http://www.theasciicode.com.ar/
6

Review
• Show how "Hi!\n" would be
stored in the memory below
– Use decimal to represent each byte
– Remember how we terminate a
string

0
1
2
3
4
5
6

7
7

What About Rational/Real Numbers?


• Previous binary system assumed binary point was fixed at the
far right of the number, so we can't represent decimals
– 10010. (implied binary point)
• Consider scientific notation:
– Avogadro’s Number: +6.0247 * 1023
– Planck’s Constant: +6.6254 * 10-27
• Can one representation scheme represent such a wide range?
– Yes! Floating Point
– Represents the sign, significant digits (fraction), exponent as separate
bit fields
• Decimal: ±D.DDD * 10 ±exp
• Binary: ±b.bbbb * 2±exp
Overall Sign of #
S Exp. fraction
8

C Floating Point Types


• f l o a t and double types:

C Type Bytes Bits Range


float 4 32 ±7 significant digits * 10+/-38
double 8 64 ±16 significant digits * 10+/-308

• Prefer double over f l o a t


– Many compilers will upgrade floats to doubles anyhow
• Don't use floating-point if you don't need to
– It suffers from rounding error
– Some additional time overhead to perform arithmetic operations
9

C CONSTANTS & DATA TYPES


10

Constants
• Integer: 496, 10005, -234
• Double: 1 2 . 0 , - 1 6 . , 0 . 2 3 , - 2.5E- 1 , 4e- 2
• Characters (char type): enclosed in single quotes
– Printing characters: ' a ' , ' 5 ' , ' B ' , ' ! '
– Non-printing special characters use "escape" sequence (i.e. preceded by a \ ):
' \ n ' (newline/enter), ' \ t ' (tab) , ' \ \ ' (slash), ' \ ' ' (apostrophe)
• C-Strings
– 0 or more characters between double quotes 0 104 ‘h’
1 105 ‘i’
" h i 1 \ n " , "12345", " b " , " \ t A n s . i s %d" 2 49 ‘1’

– Ends with a ' \ 0 ' =NULL character added as the last 3 10 ‘\n’

4 00 Null
byte/character to allow code to delimit the end of the string 5 17

• Boolean (C++ only): t r u e , f a l s e 6


7
59
c3
– Physical representation: 0 = false, (Non-zero) = true …
String Example
(Memory Layout)
11

You're Just My Type


• Indicate which constants are matched with
the correct type.
Constant Type Right / Wrong
4.0 int
5 int
'a' string
"abc" string
5. double
5 char
"5.0" double
'5' int

Solutions are provided at the end of the slide packet.


20

SOLUTIONS
21

You're Just My Type


• Indicate which constants are matched with
the correct type.
Constant Type Right / Wrong
4.0 int double (.0)
5 int int
'a' string char
"abc" string C-string
5. double float/double (. = non-integer)
5 char Int…but if you store 5 in a char
variable it'd be okay (char = some
number that fits in 8-bits/1-byte
"5.0" double C-string
'5' int char

You might also like