Lecture 3
Lecture 3
Computer Programming
Lecture #3
Variables - declaration, assignment, input/output
Monsoon 2024
Amit Praseed, Saleena N, Santosh Kumar Behera
CSED NITC
1
Variables
2
Variable Declarations
• Declaration indicates the type of a variable
3
Naming Variables
• Rules for forming valid variable names
• a name consists of letters, digits and underscore( _ )
• should start with a letter or underscore
• should not be a keyword
4
Keywords
Words having special meaning to the compiler.
5
Variable Declaration: list of variables
#include <stdio.h>
int main()
{
int a, b, sum;
…
}
6
Assigning value to variable
Assignment operator =
7
Assignment statement: examples
#include <stdio.h>
int main()
{
int a,b;
a=5;
b=10;
……
}
8
Assignment updates memory location
Each variable has a location in memory
int a, b; a b
a=5; a 5 b
b=10; a 5 b 10
9
Printing the value of variable
printf(“a = %d”, a);
10
Example Program: printing the values of variables
#include <stdio.h>
int main()
{
int a,b;
a=5; b=10;
printf(“Value of variable a is %d\n”, a);
printf(“Value of variable b is %d\n”, b);
}
o/p:
Value of variable a is 5
Value of variable b is 10
11
Variable initialization
A variable can be initialized in its declaration
int a=10;
12
Example Program: initializing variables
#include <stdio.h>
int main()
{
int a=5,b=10;
printf("Value of variable a is %d\n", a);
printf("Value of variable b is %d\n", b);
}
o/p:
Value of variable a is 5
Value of variable b is 10
13
Exercise
#include <stdio.h>
int main()
{
int min=0, max=100;
min=10;
printf("min=%d max=%d\n", min,max);
}
o/p: ?
14
Exercise
#include <stdio.h>
int main()
{
int min=0, max=100;
min=10;
printf("min=%d max=%d\n", min,max);
}
15
Input: reading the value of a variable
scanf(“%d”, &a);
Variable name
Conversion specification (where to store the data once read)
(which type of data do you want & stands for “address of”
to read?)
16
Reading a value and storing in a variable
#include <stdio.h>
int main( )
{
int a;
printf("Enter a number\n");
scanf("%d",&a);
printf("The value entered is %d\n", a);
}
A sample run of the program: (first line is to prompt the user to input a value, second line is user input)
Enter a number
10
The value entered is 10
17
Exercise: complete the program to read values of variables
a, b and print the values
#include <stdio.h>
int main( )
{
int a, b;
printf("Enter value of a\n");
scanf("%d",&a);
printf("Enter value of b\n");
...
}
18
Multiple Inputs using a Single scanf()
#include <stdio.h>
int main( )
{
int a, b;
printf(“Enter two numbers\n”);
scanf(“%d%d”,&a,&b);
printf("a=%d b=%d\n", a,b);
}
int main()
{
int min=0, max=100;
printf("Enter the minimum value\n");
scanf("%d",&min);
printf("min=%d max=%d\n", min,max);
}
20
scanf(): updating memory locations
21
scanf(): updating memory locations
... a 5 b 0
22
Major References
1. B. W. Kernighan and D. M. Ritchie, The C Programming Language,
2nd ed., Pearson Education India, 2015.
2. P. J. Deitel and H. M. Deitel and, C: How to program, 9th ed.
Pearson Education, 2023.
23