0% found this document useful (0 votes)
14 views23 pages

Lecture 3

Intro to computing science

Uploaded by

www.fahadkmty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

Lecture 3

Intro to computing science

Uploaded by

www.fahadkmty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CS1001E

Computer Programming

Lecture #3
Variables - declaration, assignment, input/output

Monsoon 2024
Amit Praseed, Saleena N, Santosh Kumar Behera
CSED NITC

1
Variables

• Variable name denotes a memory location where the program can


store value
• Every variable in C has an associated data type
• indicates the type of values that can be stored in the variable and the set of
operations that can be performed on it
• e.g. Integer data type
• to store integer values
• declared using the keyword int

2
Variable Declarations
• Declaration indicates the type of a variable

• Every variable must be declared prior to its use


e.g.: int a;
declares a as int type variable

• Declaration reserves memory space, but does not assign an initial


value

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

• Some valid variable names:


marks, grade, employee_id, total, x1, studentName

• C is case sensitive: marks and Marks are two different variable


names

4
Keywords
Words having special meaning to the compiler.

Some of the keywords:


break case char const continue default do
double else float for goto if int
long register return short signed sizeof struct
switch typedef union unsigned void while

5
Variable Declaration: list of variables
#include <stdio.h>

int main()
{
int a, b, sum;

}

The three variables a, b and sum are declared as int type

6
Assigning value to variable

Assignment operator =

Put the value on the RHS to the


variable (memory location) on
a = 5 ; the LHS

a=5; is an assignment statement

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);

• the value of a is printed in place of %d

• %d is a conversion specification - to convert the corresponding


value to decimal
• the d in %d stands for decimal integer

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;

declares a as int type variable and assigns it the initial value 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);
}

o/p: min=10 max=100

Note: the second assignment to min overwrites the earlier value 0

15
Input: reading the value of a variable

• User input can be taken using the scanf() function.

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);
}

A sample run of the program:

Enter two numbers


10
20
a=10 b=20 19
Programming Exercise
#include <stdio.h>

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

int a=0, b=0; a 0 b 0


...
scanf(“%d”, &a); if user enters 5
... a 5 b 0

21
scanf(): updating memory locations

int a=0, b=0; a 0 b 0


...
scanf(“%d”, &a); if user enters 5

... a 5 b 0

scanf(“%d”, &a); if user enters 10


a ? b ?

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

You might also like