Lecture 2.
Variables & Data Types
Deepthi, Faculty
Department of CSE, NITK
Number System : Decimal
We are accustomed to using decimal number system.
• Has Ten digits : 0,1,2,3,4,5,6,7,8,9
• Every digit position has a weight which is a power of 10.
• Example:
234= 2 x 102 + 3 x 101 + 4 x 100
250.67 = 2 x 102 + 5 x 101 + 0 x 100 + 6 x 10-1 + 7 x 10-2
1
Number System : Binary
Binary number system uses digits 0 and 1.
• Example: 101, 1011, 11010
• Binary to decimal:
• 110102 = 1×24 + 1×23 + 0×22 + 1×21 + 0×20 = 16 + 8 + 0 + 2 + 0 = 2610
• Bits & Bytes
Bit : A single binary digit (0 or 1).
Nibble : A collection of four bits (say, 0110).
Byte : A collection of eight bits (say, 01000111).
Word : Typically 4 or 8 bytes (that is, 32 or 64 bits). Depends on the computer.
2
Number System
• A k-digit decimal number, can express unsigned integers in the range
0 to 10k–1
Example: For k=3, from 0 to 999.
• A k-bit binary number, can express unsigned integers in the range
0 to 2k–1
Example: For k=3, from 0 to 7.
For k=10, from 0 to 1023.
3
Variables
• A variable is a named storage location in memory that holds a value.
• The value of a variable can be changed during program execution.
• Key Characteristics of Variables: It has a name, type, value, scope, lifetime.
• Declaration: Specifies the variable’s type and name
int age;
• Initialization: Assigns a value to the variable when it is declared.
int age = 25;
4
Variables in Memory
• All the variables are stored in memory.
• How does memory look like (logically)?
• As a list of storage locations, each having a unique address.
Address 0
Address 1
Address 2
• Each block represent one byte of memory.
Address 3 • Every variable is mapped to a particular
Address 4 memory address
.
.
.
Address N-1
5
Data Type
• Data types specify the type of data a variable can hold.
• Determines a set of values that a variable might take and a set of operations that can be
applied.
• Primitive Data Types:
1. Integer: Store whole numbers (Examples: -5, 24, 0, 10).
Size: Typically 4 bytes (32 bits), depends on system. Keyword: int
Example: int age = 30;
1. Floating-point: Store numbers with fractional values (single-precision floating-point
numbers) (Examples: 3.14159, 5.0, -12345.345). Keyword: float
Size: Typically 4 bytes (32 bits).
Example: float height = 5.9; 6
Data Type
Primitive Data Types:
3. Double-precision Floating-point: Store numbers with fractional values with higher
precision than float.
Size: Typically 8 bytes (64 bits). Keyword: double
Example: double pi = 3.141592653589793;
4. Character: Store a single character (Examples: ‘A’, ‘d’, ‘4’, ‘*’, ‘ ’).
Size: 1 bytes (8 bits). Keyword: char
Example: char grade = ‘A’;
7
Example Program
#include <stdio.h>
int main() {
int age = 25;
float height = 5.7;
double pi = 3.141592653589793;
char initial = 'J';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("PI: %.15f\n", pi);
printf("Initial: %c\n", initial);
return 0;
}
8
Average of 3 Numbers Program
#include <stdio.h>
int main() {
int a;
int b;
int c;
//or int a, b, c;
float average=0;
printf(“Enter three numbers: ”);
scanf(“%d %d %d”,&a, &b, &c);
average = (a+b+c)/3;
printf(“Average of %d, %d, %d is %f \n", a, b, c, average);
return 0;
} 9
Range of Data Types
Sizes and ranges of basic data types in C for a 16-bit computer
10
Range of Data Types
Sizes and ranges of basic data types in C for a 32-bit computer
11
Specifiers and Qualifiers
Type Specifiers: Determine the size of data types in terms of memory allocation.
1. short
2. long
12
Example Program
#include <stdio.h>
int main()
{
short s = 10;
int i = 1000;
long l = 100000L;
long long ll = 10000000000LL;
char c = 'C';
printf("Size of short: %zu bytes\n", sizeof(s));
printf("Size of int: %zu bytes\n", sizeof(i));
printf("Size of long: %zu bytes\n", sizeof(l));
printf("Size of long long: %zu bytes\n", sizeof(ll));
printf("Size of char: %zu byte\n", sizeof(c));
return 0;
} 13
Specifiers and Qualifiers
Size Specifiers: Sign specifiers determine whether a variable can hold negative values, only
positive values, or zero.
They modify the range of values that a data type can store.
1. signed
2. unsigned
14
Example Program
#include <stdio.h>
int main() {
signed int si = -1234;
unsigned int ui = 1234;
signed char sc = -10;
unsigned char uc = 200;
printf("Signed int: %d\n", si);
printf("Unsigned int: %u\n", ui);
printf("Signed char: %d\n", sc);
printf("Unsigned char: %u\n", uc);
return 0;
}
15