Chapter2-Data Types and Variables

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Data types and variables

Yongseok Son
Department of Computer Science and Engineering
Chung-Ang University
Definitions
❖ Data type
▪ Data types specify how we enter data into our programs and
what type of data we enter (e.g., integer, floating point, character,
etc)
❖ Operator
▪ Operators specifies how an object can be manipulated (e.g., num
eric vs. string operations)
▪ Operators can be unary (e.g., ++), binary (e.g., +, -, *, /), ternary (
?:)
❖ Expression
▪ An expression in a programming language is a combination of val
ues, variables, operators, and functions
❖ Variable
▪ A variable is as named link/reference to a value stored in the syst
em’s memory
slide 2
Definitions
❖ Example
▪ x, y are variables
▪ y = x + 2 is an expression
▪ + is an operator

int x = 0;
int y = 0;
y = x + 2;

slide 3
Data types in C language
❖ Data types specify how we enter data into our programs
and what type of data we enter
▪ C language has some predefined set of data types to handle
various kinds of data that we can use in our program
▪ These datatypes have different storage capacities

slide 4
Data types in C language
❖ char
▪ The most basic data type in C
▪ It stores a single character and requires a single byte of
memory
❖ int
▪ An int variable is used to store an integer
❖ float
▪ It is used to store decimal numbers (numbers with floating point
value) with single precision
❖ double
▪ It is similar to float used to store decimal numbers (numbers
with floating point value) with double precision

slide 5
Data types in C language
❖ Representation range of data types

Signed/Unsigned Data types Size Range


char 1 bytes -128(−27 ) ~ 127(27 − 1)
signed
short 2 bytes -32,768(−215 ) ~ 32,767(215 − 1)
int 4 bytes -2, 147, 483, 648(−231 ) ~ 2, 147, 483, 647(231 -1)
long 4 bytes -2, 147, 483, 648(−231 ) ~ 2, 147, 483, 647(231 -1)
unsigned char 1 bytes 0 ~ 255 (28-1)
unsigned
unsigned short 2 bytes 0 ~ 65, 535(216 -1)
unsigned int 4 bytes 0 ~ 4, 294, 967, 295(232 -1)
unsigned long 4 bytes 0 ~ 4, 294, 967, 295(232 -1)

slide 6
Data types in C language
❖ Unsigned (2bits): 0 ~ 2n-1
0 0 0

0 1 1
Data Range: 0 ~ 3 (22-1)
1 0 2

1 1 3

❖ Signed (2bits): -2n-1 ~ 2n-1-1

0 0 0

0 1 1 Data Range: -2 (-21) ~ 1 (21-1)


(-2, -1, 0, 1)
1 1 -1

1 0 -2

slide 7
Data types in C language
❖ Data types and Storage Sapce

Integer 16bits

0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 1
short:

1bytes (8bits) 1bytes (8bits)

integer 32bits

int: 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 1 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 1

1bytes (8bits) 1bytes (8bits) 1bytes (8bits) 1bytes (8bits)

slide 8
Data types in C language: signed short
Value Bits Constants and Operations
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 SHRT_MAX
32767 (215 -1)

0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 SHRT_MAX - 1
32766 (215 -2)

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 – 1 = 1 + [-1]
1
[0000 0000 0000 0001]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +[1111 1111 1111 1111]
0
[0000 0000 0000 0000]
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-1


1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 SHRT_MIN + 1
-32767 (-215 + 1)

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 SHRT_MIN
-32768 (-215 )
slide 9
Data Types
▪ Data size and value range
Type Data size Value range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes -2,147,483,648 to 2,147,483,647
-9,223,372,036,854,775,808 to
long 8 bytes
9,223,372,036,854,775,807
unsigned long 4 bytes 0 to 4,294,967,295
64
unsigned long 8 bytes 0~18,446,744,073,709,551,615 (2 -1)
float 4 bytes -3.4E38 to 3.4E38
double 8 bytes -1.7E308 to 1.7E308

slide 10
Names in C
❖ Identifiers (variable name)
▪ Must begin with a character or underscore(_)
▪ May be followed by any combination of characters, underscores,
or digits(0-9)
▪ Case sensitive
▪ Ex) summary, exit_flag, i, _id, jerry7

❖ Keywords
▪ Reserved identifiers that have predefined meaning to the C
compiler.
▪ Ex) if, else, char, int, while

slide 11
Symbolic Constants
❖ Names given to values that cannot be changed
❖ Use preprocessor directive #define
#define N 3000
#define FALSE 0
#define PI 3.14159
#define FIGURE "triangle"

❖ Symbols which occur in the C program are replaced by


their value before actual compilation

slide 12
Declaring Variables
❖ Variable
▪ Named memory location where data value is stored
▪ Each variable has a certain type (e.g. int, char, float, …)
▪ Contents of a variable can change
▪ Variables must be declared before use in a program
▪ Declaration of variables should be done at the opening brace of
a function in C

❖ Basic declaration format


▪ data_type var1, var2, …;
• Examples
✓ int i,j,k;
✓ float length, height;

slide 13
Variable and Constant
❖ In programming, a variable is a container (storage are) to hold data
❖ To indicate the storage area, each variable should be given a unique name
(identifier)
▪ Variable names are just the symbolic representation of a memory location
▪ Example
• int playerScore = 95;
✓ Here, playerscore is a variable of integer type
✓ The variable is assigned an integer value 95
❖ A constant is a value (or an identifier) whose value cannot be altered in a
program
▪ Example
• 1, 2.5, ‘c’ etc
• Here, 1, 2.5 and ‘c’ are literal constants
• You cannot assign different values to these terms
▪ You can also create non-modifiable variables in C
• Example
✓ Const double PI = 3.14;

slide 14
Initializing C Variable
❖ Variables should be declared in the C program before use
❖ Memory space is not allowed for a variable while declaration
▪ It happens only on variable definition
❖ Variable initialization means assigning a value to the variable

Type Syntax
Datatype variable name;
Variable declaration
ex) int x, y, z; char flag, ch;
Datatype variable name = value;
Variable initialization
ex) int x = 50, y = 30; char flag = ‘x’, ch = ‘y’;

slide 15
Types of Variables in C Program
❖ Local variable
❖ Global variable

slide 16
Types of Variables in C Program
❖ Local variable
▪ The scope of local variables will be within the function only
▪ These variables are declared within the function and
cannot be accessed outside the function
▪ In the below example, m and n variables are having scope
within the main function only
• These are not visible to test function
▪ Likewise, a and b variables are having scope within the
test function only
• These are not visible to main function
#include <stdio.h> void test()
void test(); {
int main() int a = 50, b = 80;
{ }
int m = 22, n = 44;
test();
}

slide 17
Types of Variables in C Program
❖ Properties of local variable
▪ A local variable is allocated on stack
▪ Local variables are uninitialized by default and contains
garbage value
▪ Lifetime of a local variable is until the function or block
• A local variable dies once the program control reaches
outside its block
▪ Local variable is accessed using block scope access

slide 18
Types of Variables in C Program
❖ Properties of local variable
▪ The scope of local variables will be decided by the function
or block
▪ Example

#include <stdio.h>

int main()
{
{
int num1 = 10; //Declare a variable in a block
}

printf("%d\n", num1); //Compilation error.


//num1 cannot be used outside of a block
return 0;
}

slide 19
Types of Variables in C Program
❖ Global variable
▪ The scope of global variables will be throughout the
program
• These variables can be accessed from anywhere in the
program
▪ This variable is defined outside the main function
• So that, this variable is visible to main function and all other
sub functions
#include <stdio.h> void test()
void test(); {
int m = 22, n = 44; m = 5, n = 6;
int main() printf(m=%d, n=%d\n”, m, n);
{ }
m = 1;
n = 2;
printf(m=%d, n=%d\n”, m, n);
test();
}

slide 20
Types of Variables in C Program
❖ Global variables are allocated within data segment of
program instead of stack
❖ Memory for global variable is allocated once and persists
throughout the program
❖ They are accessible to all function of the same and other
programs (using extern keyword)

slide 21
Types of Variables in C Program
❖ External variables are also known as global variables
▪ These variables are defined outside the function
▪ These variables are available globally throughout the
function
▪ “Extern” keyword is used to declare and define the
external variables

<print.c> <main.c>
#include <stdio.h> #include <stdio.h>
int num1 = 10; extern int num1;

void printNumber() void printNumber()


{ {
printf("%d\n", num1); printf("%d\n", num1);
} }

slide 22
Variable in Main Memory
❖ Stack segment
▪ Stack segment is used to store local variables and is used for passing
arguments to the functions
❖ Heap segment
▪ Heap is the segment where dynamic memory
allocation takes place using malloc function
❖ Bss (block starting symbol) segment stack
▪ The bss segment contains the global variables and static variables heap
▪ Uninitialized variables
❖ Data segment bss
▪ The data segment contains the global variables and static variables
▪ Initialized variables data
❖ Text segment text
▪ The text segment of the program contains the
Segments of memory
executable instructions of the program
▪ Text segment contains machine code of the compiled program

slide 23
Variable in Main Memory
❖ Example

#include <stdio.h> stack


int a = 10; //initialized variable stored in data segment
int b; //uninitialized variable stored in bss segment heap

int main() bss


{
int a = 3; //local variable stored in stack segment
data
int *b = malloc (sizeof(int)); //heap segment
}
text
Segments of memory

slide 24
Data types and sizes
❖ sizeof ( )
▪ sizeof() is a unary operator used to find size of a type in
memory
• The unary operator “sizeof” generates the size of a variable or data
type
• It returns amount of memory that is allocated to that data types
• It returns total bytes needed in memory to represent a data type or
value or expression

slide 25
Data types and sizes
❖ Example of sizeof() function

#include <stdio.h>
int main()
{
int integerVar;
printf("Size of char = %d\n", sizeof(char)); //sizeof(type)
printf("Size of int = %d\n", sizeof(integerVar)); //sizeof(variable-name)
printf("Size of expression (3+2) = %d\n", sizeof(3 + 2)); //sizeof(expression)
return 0;
}

Size of char = 1
Size of int = 4
Size of expression (3 + 2) = 4

slide 26
Data types and sizes
❖ Example of sizeof() function
#include <stdio.h>
int main(void)
{
char a;
short b; <Results>
int c; 1, 1
long d; 2, 2
long long e; 4, 4
float f; 4, 4
double g; 8, 8
printf("%d, %d\n", sizeof(char), sizeof(a)); 4, 4
printf("%d, %d\n", sizeof(short), sizeof(b)); 8, 8
printf("%d, %d\n", sizeof(int), sizeof(c));
printf("%d, %d\n", sizeof(long), sizeof(d));
printf("%d, %d\n", sizeof(long long), sizeof(e));
printf("%d, %d\n", sizeof(float), sizeof(f));
printf("%d, %d\n", sizeof(double), sizeof(g));
return 0;
}

slide 27
Overflow
❖ Overflow
▪ Overflow occurs when an operation attempts to create a
numeric value that is outsize of the range that can be
represented with a given number of digits
▪ Either larger than the maximum or lower than the minimum
representable value

overflow

variable

slide 28
Overflow
❖ An example of overflow
#include <stdio.h>

int main(void)
{
short s_money = 32767; // Max value 32767
unsigned short u_money = 65535; // Max value 65535

s_money = s_money + 1;
printf("s_money = %d", s_money); <Result>

u_money = u_money + 1; assasa = -32768


s_money
printf("u_money = %d", u_money); u_money = 0
return 0;
}

slide 29
ASCII Code
❖ American Standard Code for Information Interchange (ASCII)
▪ ASCII is a character encoding standard
▪ ASCII is made up of 128 symbols in the character set
▪ These symbols consists of letters, number, and characters
▪ Each symbol in the character set can be represented by a decimal
value ranging from 0 to 127

slide 30
ASCII Code: A table of ASCII code

slide 31
ASCII Code
❖ Representation and storage of character data values

char c1 = ‘a’ ;
Integer 8bits
The binary number 01100001, which is the
0 1 1 0 0 0 0 1 code value for the character ‘a’, is stored
char:

1byte (8bits)

slide 32
Escape Character
❖ Starts with backslash (\)
❖ Indicate special meaning and interpretation

Escape character meaning


\b backspace
\t tab
\n newline
\" double quote
\' single quote
\\ back slash

slide 33
ASCII Code
❖ An example of ASCII code
#include <stdio.h>
int main(void)
{
char code1 = 'A'; // Character
char code2 = 65; // ASCII code (Dec)
char code3 = 0x41; // ASCII code (Hex)
printf(“code1 = %c\n", code1);
printf(“code2 = %c\n", code2);
printf(“code3 = %c\n", code3);
}

<Results>
code1 = A
code2 = A
code3 = A

slide 34

You might also like