Variables
Location in the memory where values can be stored
Defining variables
int a;
data type name of the variable
Defining/Declare variables
int a;
variable names
correspond to memory
locations
memory
Data types
Name of variables
A variable name in C can be any valid identifier.
An identifier is a series of characters consisting of
letters (a-z, A- Z), digits (0-9) and underscores (_)
that does not begin with a digit.
Name of variables
int a1;
int 1al
int A1;
int abcdef;
int _ab;
int A1;
Name of variables
int a1; valid
int 1al invalid
int A1; valid
int abcdef; valid
int _ab; valid : but avoid
int A1; valid
Name of variables
C is case sensitive— uppercase and lowercase letters
are different in C
int a1;
int A1;
a1, A1 two different variables
Name of variables: multiword
int print_sum;
use '_' as the word separator
Name of variables: Convention
Use all lower case letters
Assigning variables
a = 2;
2
Assign 2 to a
Assignment operator
Initializing variables
int a; //defining variable a
a=2; //assigning 2 to a
Combine variable definition and
variable assignment
int a = 2;
initializing variable a
Declaring multiple variables in
single statement
int a; int a, b;
int b; Data type same
int a; int a;
float b; Data type different float b;
Declare variables
before they are
used
Arithmetic Operation
int sum;
sum = a + b;
Arithmetic operator
Arithmetic Operation
Printing variables
printf(“Sum %d”, sum)
Conversion specifier
Printing multiple variables in
single statement
printf(“a = %d”, a);
printf(“b = %d”, b);
printf(“sum = %d”, sum);
printf(“a = %d, b = %d, c = %d”, a, b, sum);
Algebraic and C Arithmetic Expressions
𝑎+𝑏+𝑐+𝑑+𝑒
AE: 𝑚 = 5
C: 𝑚 = 𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒 Τ5
Algebraic and C Arithmetic Expressions
AE: 𝑦 = 𝑚𝑥 + 𝑐
C: 𝑦 = 𝑚 ∗ 𝑥 + 𝑐
Algebraic and C Arithmetic Expressions
𝑤
AE: 𝑎 = 𝑝𝑟 𝑚𝑜𝑑 𝑞 + − 𝑦
𝑥
C: 𝑎 = (𝑝 ∗ 𝑟) % 𝑞 + (𝑤Τ𝑥) − 𝑦
Algebraic and C Arithmetic Expressions
AE: 𝑦 = 𝑎𝑥 3 + 𝑏𝑥 2 + 𝑐𝑥 + 𝑑
C: 𝑦 = 𝑎 ∗ 𝑥 ∗ 𝑥 ∗ 𝑥 + 𝑏 ∗ 𝑥 ∗ 𝑥 + 𝑐 ∗ 𝑥 + 𝑑
Taking Input from User
printf("Enter integer a : "); Prompt
scanf("%d", &a);
Address operator
Taking Multiple Inputs in a single
statement
scanf("%d", &a);
scanf("%d", &b);
scanf("%d%d", &a, &b);