INTRODUCTION TO COMPUTER PROGRAMMING ITC314 Lecture 14 Initializing








- Slides: 8

INTRODUCTION TO COMPUTER PROGRAMMING ITC-314 Lecture 14

Initializing Variables A variable may be given an initial value with a declaration. The variable can initialize by using an equal sign(=) which is called assignment operator. When initializing a variable, the variable name is placed to the left and constant value to the right side of equal sign. The syntax for initializing variables are as follow: char ch= ‘A’; int max=100; float Pi=1. 01; double min=123. 1;

Initializing Variables cont…. Or we can also initialized multiple variables in single statement like as following: int max=100, min=50, middle=75; Notice that each variable is separated by commas.

Assigning Value to a Variable A variable may be assign a value at any point in the program. This can be done by using assignment statement. Variables must declare before assigning values to them. The general syntax of assignment statement: var=value; Example: int a; char ch 1; a=21; ch 1=‘b’; Declaration Statement Assignment Statement

Assigning Single value to multiple variables In C, a single value may be assigned to multiple variables using one assignment statement. Example: x=y=z=5; a=b=c=1. 0;

Displaying variables C provides a standard, versatile and powerful function printf() for display variables. The printf() function perform basically two tasks: First it accessed the current value held by variable Next it converts the binary format into a sequence of readable characters. To display variables values, the printf() function used a specified format specifier to output the value. For example to display integer value, printf() uses %d format specifier.

Format specifier A format specifier (%d, %c) is used to control what format will be used by printf() to print out a particular variable because variables values are held as binary number inside the computer. The following is the list of format specifier that we will used to display variables %c %d %u %l %f %ld single character signed integer unsigned integer long integer float point Double float character type integer type unsigned int type long type float a double type

Program #include<conio. h> #include<stdio. h> void main(void) { int a=2; char abc=‘A’, xyz=‘B’; float e; e=2. 0; // display variables values printf(“Integer value a=%d”, a); printf(“char value ch 1=%c”, ch 1); printf(“char value ch 2=%c”, ch 2); printf(“float value e=%f”, e); getch(); }