CSE 1111: Structured Programming
Language
Lecture 03: Variables
What is
a
variabl
A variable is symbolic names that
represent
storage locations in memory.
e?
Properties of Variables
- It is a memory location that stores some value
temporarily.
- Has a symbolic name
- Has a specific data type
- Must be declared before using
Declaration and Definition
-A variable declaration specifies a variable's type and
its name, informing the compiler about its existence
(registering it in the compiler’s symbol table).
-A variable definition both declares the variable and
allocates memory for it.
-In C, variables are usually declared and defined at
the same time.
Declaration Syntax
- Syntax for declaring variables:
data_type variable_name;
Example:
int num;
float avg;
char
section;
-Multiple variables of the same type can be
declared in the same line. Example:
int num1, num2, num3;
Rules for Variable Names
-Can only contain letters (A-Z, a-z), digits (0-
9) and underscore (_).
- Cannot start with a digit.
-The same name cannot be used for different
variables in the same scope.
- Example of variable names:
Valid: myVar, _temp, count123, MAX_LIMIT, include,
Main Invalid: 123count, total$, int, my var
* $ may be allowed by some compiler but
it’s not standard.
Rules for Variable Names
- Variable names cannot contain spaces.
-Some conventions for multi-word variable
names: snake_case
camelCas
e
PascalCas
e
kebab-case (not allowed
in C)
SCREAMING_SNAKE_CASE
Variable Initialization
-Variables in C can be initialized at the time of
declaration, assigning an initial value.
-If a variable is declared without initialization, it
contains garbage (random) values until explicitly
assigned.
-The value of a non-constant variable can be re-
assigned any time. Constant variables must be
initialized and after initialization, their value can not be
changed.
- Example of variable initialization:
int num1 = 5, num2 = num1, num3
Variable Scope and
Lifetime
- The scope of a variable defines where in the program
it is accessible. The lifetime of a variable indicates how
long it exists in memory.
Variabl Scope Description Lifetime
e
Scope
Local Declared inside a function or block, Exist only during
accessible only within that function or function execution
block
Global Declared outside any function, usually Exist for the
at the top of a file; accessible from any duration of the
function within the file (possibly other program and retain
files) their last value.
Static Retain their value across function calls Remain in memory for
when defined within a function with the entire program
Example
Code Output
#include <stdio.h> 15 10
15 0
int main()
{
int a = 5, b = 0;
if (a > 0)
{
int b = 10;
a += b;
printf("%d %d\n", a, b);
}
printf("%d %d\n", a, b);
}
Data Types
- Each variable in C has an associated data type.
-It defines the nature and size of data that can be
stored in a variable.
-It helps the compiler understand how much memory to
allocate and what kind of operations are permissible for
that variable.
-The basic data types are int, char, float and double.
Many of these supports modifiers like short, long,
unsigned.
-Along with data types, format specifiers play an
Data Type Examples
Data Type Size (bytes) Range Description
int 4 [-2147483648, Integer number
2147483647]
char 1 [-128, 127] Single character (stored as ASCII
code)
float 4 ±[3.4E-38, 3.4E+38] Floating point number
approx.
double 8 ±[1.7E-308, 1.7E+308] Floating point number (double
approx. precision)
short int 2 [-32768, 32767] Short integer
long int 4 [-2147483648, Long integer
2147483647]
long long int 8 [-9223372036854775808, Long long integer
9223372036854775807]
unsigned int 4 [0, 4294967295] Unsigned integer
Data Type Examples
Data Type Size (bytes) Range Description
unsigned 4 [0, 4294967295] Unsigned long integer
long int
unsigned 8 [0, Unsigned long long integer
long long int 18446744073709551615]
long double 10 ±[3.4E-4932, 1.1E+4932] Floating point number (higher
approx. precision than double)
- int data types can be represented by just
modifiers. Example:
long long int can be written as long
long. int can be written as signed.
Scanning and Printing
Variables
-The fundamental input/output functions in C are
scanf and printf.
-The first parameter of these two functions takes
a format string that may contain format specifiers.
-The following parameters take values (for printf) or
addresses (for scanf) matching the format specifiers.
Format Specifier Example
Format Specifier Data Type Description
%d, %i int Integer format
%c char Character format
%f float Floating point format (6 points after decimal)
%lf double Floating point format (10 points after decimal)
%hd, %hi short int Short integer format
%ld, %li long int Long integer format
%lld, %lli long long int Long long integer format
%u unsigned int Unsigned integer format
Format Specifier Example
Format Specifier Data Type Description
%lu unsigned Unsigned long integer format
long int
%llu unsigned Unsigned long long integer format
long long int
%5d int Integer (at least 5 characters)
%.2f float Floating point number (2 digits after decimal point)
%5.2lf double Floating point number (at least 5 characters including
2 digits after decimal point)
Format Specifier Example
Format Specifier Data Type Description
%o int Integer in octal format
%x int Integer in hexadecimal format with lowercase characters
%X int Integer in hexadecimal format with uppercase
characters
%e float Floating point number in scientific format with
lowercase e
%E float Floating point number in scientific format with
uppercase E
%llx long long int Long long integer in hexadecimal format
Example
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %o", &a, &b);
printf("%c", a+b);
}
Sample Input Sample Output
50 17 A