0% found this document useful (0 votes)
2 views23 pages

3 - Data Types I

Chapter 2 covers data types, variables, and constants in programming, explaining how data is stored in variables and the rules for naming them. It details various data types such as int, char, float, and their respective ranges and sizes, along with variable declaration formats and the use of operators. The chapter also discusses the scope of variables, the concept of constants, and provides examples of output formatting and exercises for practice.

Uploaded by

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

3 - Data Types I

Chapter 2 covers data types, variables, and constants in programming, explaining how data is stored in variables and the rules for naming them. It details various data types such as int, char, float, and their respective ranges and sizes, along with variable declaration formats and the use of operators. The chapter also discusses the scope of variables, the concept of constants, and provides examples of output formatting and exercises for practice.

Uploaded by

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

Chapter 2

Data Types

2/28/2017 CUIT114/111 SEST, CUT 1


Outline
• Data and variables
• Basic data types
• Constants
• Operators
• Output of processed data

2/28/2017 CUIT114/111 SEST, CUT 2


Data and Variables
• Data is stored in variables
• Variable = named area of storage that can hold a single value
• Variables
– Must be declared before use
– Variable types must be stated
• Variable type
– Represents the form of data that is stored in a variable, for
example, do you store a decimal number, a whole number, a
single character, a set of characters?
– Helps the compiler reserve adequate space for the data to be
stored.
– Helps the compiler carry out operations correctly on the stored
data, for example multiplying decimals as opposed to
multiplying whole numbers
– Maximum value (i.e. Size) is dependent on the size of the
word of a machine, e.g. 8-bit, 16-bit, 64-bit machines and
compiler/system implementations.
2/28/2017 CUIT114/111 SEST, CUT 3
Rules for Variable Names
• Meaningful as relates the problem being
solved
• One word of reasonable length
– Max of 30 characters
– Remember that you will type the words yourself
• Name
– Must begin with letter of English alphabet or _
(i.e. underscore)
– May contain lowercase letters, uppercase
letters, numerical digits, underscore characters
– Is case sensitive
2/28/2017 CUIT114/111 SEST, CUT 4
Examples of Variable names
Valid Invalid
• Salary • 81_c
• _xtend • in age
• c • printf
• in_age

2/28/2017 CUIT114/111 SEST, CUT 5


Data Types
Type Meaning Word Size Range
int A whole number 16 bits -32,768 to
32767
short int Same range or half 16 bits -32,768 to
32767
char Basic character in the 8 bits -128 to 127
character set
unsigned Unsigned values only(+) 8 bits 0 to 255
char
unsigned (+) only 16 bits 0 to 65,535
int
long Often twice word size int 32 bits -2,147,483,648
to
2,147,483,647
float Fractional numbers with 32 bits 1.17549435(10-
sign, mantissa and 38
) to
exponent 3.40282347(103
8
)
2/28/2017 CUIT114/111 SEST, CUT 6
double More accurate floating- 64 bits Approx
Variable Declaration Format

Storage_class_specifier type_specifier
variable_name;

2/28/2017 CUIT114/111 SEST, CUT 7


Storage_class_specifier
Specifier Meaning
register Variable must be accessed as quickly as possible
hence keeps value in CPU register

auto Local variable gets local lifetime

static Value of variable on exiting function is preserved


for subsequent calls to the function

typedef variable_name becomes new name for the


type_specifier
(for programmers own convenience when
programming large solutions)

extern variable already defined outside current scope and


hence brings it in for use.
(allows one to use externally defined variable)

2/28/2017 CUIT114/111 SEST, CUT 8


Examples of Variable
Declarations
• Declare integer called total to store the added total of
money
int total;
• Assign 18 to total
Use = symbol
total = 18;
– Now 18 is stored in total
• Other examples
float total_price;
double sum;
total_price = 0.0;
char initial = ‘d’; // both declaration and initialisation
typedef int ID; // here ID becomes a new data type whose
underlying structure is an integer.

2/28/2017 CUIT114/111 SEST, CUT 9


Assignment Symbol a = b
• Not equality as in Mathematics
• Means take the value in location
named b and store it in memory
location associated with a
• a and b must be compatible

2/28/2017 CUIT114/111 SEST, CUT 10


Exercise 1
• State if the following variables are
valid/allowed or not
– sum
– piece_flag
– 50cent
– exit condition
– sum$value
– Sharp●knife
– _invalid
2/28/2017 CUIT114/111 SEST, CUT 11
Exercise 2
• Declare
– an integer called sum.
– a character called letter.
– a float variable called sample.
• Declare and assign values to the following
variables as specified
– The value 12 to the integer variable count.
– The character W to the character variable letter.
– The value 23 to the float variable balance.
– The value of the integer variable loop to the
integer variable sum.

2/28/2017 CUIT114/111 SEST, CUT 12


Scope of Variables: Local Versus Global

• Variable values
– Are accessible and last within { } the
brackets they are defined in.
• Local variables
– Only within a { } block
• Global variables
– Defined soon after pre-processor
directives and are accessible
throughout entire program.

2/28/2017 CUIT114/111 SEST, CUT 13


Constants
• Are fixed values that may not be altered by the program
• May be system-defined or user-defined
• Conventionally named in uppercase only
• Used by name throughout a program
• Declared in the variable declaration section
• E.g.
#define VAT 0.15

const float VAT = 0.15;


VAT is a constant with a fixed value of 0.15 (i.e. 15%)
means one can write

tax = goods_sold * VAT;

2/28/2017 CUIT114/111 SEST, CUT 14


enum
• Used to create enumerated tags
• Allows series of constant integers to be
associated with variables
• Use format
– enum identifier {enumerated-list};
• Example
– enum {linear, binary};
• // 2 variables created where value of linear is 0 and that
of binary is 1
– enum sort{quick, merge=2, hash, heap};
• // 4 variables with identifier sort where value of quick is
0, merge is 2, hash is 3 and heap is 4.

2/28/2017 CUIT114/111 SEST, CUT 15


Operators
• Allow us to
– Manipulate variables more
meaningfully to achieve desired output
– Work with complex programming
constructs
• Are mostly mathematical.

2/28/2017 CUIT114/111 SEST, CUT 16


Some basic operators
Operator Meaning Example
Assuming float v=7.2, r=0.0; int w=4, s=0;
+ Addition r = v + w;
- Subtraction r = w – v;
* Multiplication r = v * w;
/ Division s = v / w;
r = v / w;
s = w / v;
% Remainder after division, r = v % w;
i.e. modulus
++ Increment, same as +1 r++ //value of r before
increment;
++ v // value of v after
increment
-- Decrement, same as -1 See for ++
+=, *=, - Combination, shortcut v += w same as v = v + w
=, /= statements
2/28/2017
& Memory addressCUIT114/111
of/ SEST, CUT
&v //means memory 17
Values of p, q and r after following
blocks?
{ {
int p, q; int p, q;
float r; p = 6; q = 7;
char z = ‘t’; p += q;
p = 5; q = 7; r = 2.3; p *= p;
r = r * 2; q -= p;
p = q % p; }
q = q – p;
p = (q++) + z;
}
2/28/2017 CUIT114/111 SEST, CUT 18
Format Specifiers Associated with
Variables
specifier output associated data types
%c character char, unsigned char
%d, %i signed integer int, unsigned short, long, short
%f floating point/ decimal float
number
%e, %e, scientific notation float, double
%g, %G number
%s string char*, char [ ]
%lf floating point long float, double
%x, %X hexadecimal number int, short, unsigned short
%o octal number int, unsigned int, long
%u unsigned integer unsigned int, unsigned long
%hu unsigned integer (short) unsigned short
%n prints nothing
2/28/2017 CUIT114/111 SEST, CUT 19
Output of stored information
• Using printf, to output values and variables

printf(“value = %d”, 6);

• output the value 6 in the place marked %d as an integer

printf(“value = %f”,6);

• output the value 6 in the place marked %f as a decimal

printf(“age = %d and rate = %f, ”, num, prate);

• output the value in the variable num as an integer and prate


as a float

2/28/2017 CUIT114/111 SEST, CUT 20


Formatting Output
Field Specifier (flag) Meaning
– Left justify
+ Always display sign
space Display space if there is no
sign
0 Pad with leading zeros
#
•The flag is used as follows::
Use alternate form of format
specifier
% flag width . precision format specifier
•Example
printf(% – 10 d,num); to display num left justified in a
10-char space
printf(%5s,name); prints a string right justified within
a 5-char space
printf(%5.1s, name); prints only 1 character of the
string
2/28/2017 CUIT114/111 SEST, CUT 21
•Precision (dot included) is most relevant when displaying float or
Output Examples 1
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("16+ 19 = %0d , 16-19 = %d\n", 16+19, 16-19);
printf("what? %s \n", "Coming home");
printf("6th character after g = %c \n", 'g'+6);
printf("ascii value of 6th character after g = %d \n", 'g'+6);
printf("%c%c%c \n", toupper('d'), toupper('m'), toupper('n'));
printf(“ %10.2f \n“, 6.1452633);
return 0;
}
2/28/2017 CUIT114/111 SEST, CUT 22
Exercise 3
1. Write a program that computes and
outputs the number of seconds in a
year.
2. Write a program that outputs the
number of weeks and the number of
months in 268 days, assuming a
month is strictly made up of 31
days.

2/28/2017 CUIT114/111 SEST, CUT 23

You might also like