Computer Programming & Problem Solving (CPPS-1) : Chapter # 2
Computer Programming & Problem Solving (CPPS-1) : Chapter # 2
C Building Blocks
Chapter # 2
Variables
Variables may be the most fundamental aspect of any computer language. A
variable is a space in the computer’s memory set aside for a certain kind of data
and given a name for easy reference. Therefore variables are used so that the
same space in memory can hold different values at different times.
Defining a variable in C
int num ;
If there are more than one variable to be defined then they can be defined
combined or separately. For example:
int a, b, c;
int a;
int b;
int c;
No of total Bytes
Data Type Description Syntax occupied in
memory
1. Character Character data char 1
Variable Definitions
type var-name;
Here ‘type’ is the data type and ‘var-name’ is the variable name.
For example:
int number;
A programmer can assign a unique value to the variable. The general form of an
assignment statement is
OR
var-name = value;
For example:
number = 6;
Each rectangle is
1 byte of memory
When you define a variable, the compiler sets aside an appropriate amount of
memory to store that variable. In the above case we have specified an
Integer variable, so the compiler will set aside 2 bytes of memory. This is
large enough to hold numbers from –32768 to 32767 i.e 0 - 65535 for
unsigned integers.
int number;
For situations when the normal integer is too small, the long integer
( type long or long int ) can be used. It occupies 4 bytes of memory and can
hold integers from –2,147,483,648 to 2,147,483,647.
There are also 2 kinds of floating point variables. One floating point variable
type float, occupies 4 bytes and can hold numbers from 10 e+38 to 10 e-38
with between 6 and 7 digits of precision. A double-precision floating point
variable, type double, occupies 8 bytes and can hold numbers from about
10 e+308 to 10 e-308 with about 15 digits of precision. There is also a larger
long double type. It occupies 10 bytes and can hold numbers from about
10 e+4932 to 10 e-4932.
The unsigned int type holds numbers from 0 to 65,535, rather than from
–32,768 to 32,767 as the regular int type does.
Program
{
int rollno;
char section;
float gpa = 3.25;
rollno = 100;
section = ‘A’;
clrscr( );
Output:
The printf( ) gives programmer considerable power to format the printed output.
By default floating point variable prints with 6 digits to the right of the decimal
place.
For example:
Output:
GPA = 3.250000
Field Width Specifiers controls how many characters will be printed following the
decimal point.
For example:
Output:
GPA = 3.25
Output:
GPA = 3.250
Escape Sequences
In C Language backslash symbol ( \ ) is considered an “Escape” character:
because it causes an escape from the normal interpretation of a string, so that
the next character is recognized as having a special meaning.
The following list shows the common escape sequence.
\n New line
\t Tab ( move 8 characters forward )
\b Backspace
\r Carriage Return
\f Form feed ( move to the top of the next page on the printer )
\’ Single Quote
\” Double Quote
\\ Backslash
\ xdd ASCII code in hexadecimal notation
\ ddd ASCII code in Octal notation ( each d represents a digit )
Program
{
clrscr( );
Output
In C Language, printf ( ) is the output statement where as the scanf ( ) is the input
function statement.
Program
scanf ( “ %f “, &years );
Output
Please type your age in years. 10
You are 3650 days old.
The scanf ( ) function can accepts input to several variables in one statement.
1. * ( Arithmetic Operator )
2. & ( Address Operator )
Class Assignment No 1.
1. What do you mean by variable definition and variable assignment. Give
examples.
2. What happens by writing the statement int number = 6
3. Difference between \n, \r, \f and \t.
4. Write a program that take the input of your age and then calculate the total
number of hours old.
The figure given below shows an integer with a value of 2 at the address 1212.
1211
1212
1213 10
1214
1215
Program
{
int num;
clrscr( );
num = 2;
Output:
getch ( ) vs scanf ()
{
char ch;
clrscr( );
ch = getch ( );
If you typed any wrong character, you cannot move backward to correct it.
Class Assignment No 2.
1. What is the purpose of & ?
2. Differentiate between getch ( ) and getche ( ) with example.
3. Write a program that takes a user marks for the 5 subjects and then calculate
the total marks obtained by the user.
4. Write a program using: = =, + +, % and < = operators.
Operators
Operators are words or symbol that causes a program to do something to
variables.
Arithmetic Operator
Relational Operator
Arithmetic Assignment Operator
Increment Operator
1. Arithmetic Operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
Operator Precedence
Multiplication and division operators are carried out before addition and
subtraction.
Programmers can alter the order of evaluation using parenthesis.
Remainder Operator ( % ) is also called Modulo Operator.
For Example:
Answer = 13 % 5
Answer = 3
Program
{
int num;
clrscr( );
num = 2;
These operators are specially for C programmers. With the help of these
operators, programmer can compress programming statements.
For Example:
total + = number;
Program
{
int total = 0;
int count = 10;
clrscr( );
Output:
Total = 0
Total = 10
Total = 20
3. Relational Operator
C Language uses another operator that is not common in other languages i.e
the Increment Operator.
For Example:
num + +; or num - -;
Program
{
int num = 0;
clrscr( );
Output:
Number = 0
Number = 0
Number = 1
4. Increment Operator
Program
{
int age ;
clrscr( );
age = 15;
printf ( “ Is age less than 20 ? %d \n “, age < 20 );
age = 30;
printf ( “ Is age less than 20 ? %d \n “, age < 20 );
getch ( );
Output:
Therefore the output of the relational operator will be 1 if the relation is true and
0, if the relation is false.
Output:
Answer = 1
Program
void main (void)
Output:
Answer = 5 (Wrong)
Answer = 1 ( Right )
Note
Arithmetic Operators have higher precedence i.e they are evaluated before the
Relational Operators.
Comments
It is helpful to be able to put comments into the source code that can be ready by
humans but are visible but are invisible to the compiler.
A Comments begins with the two-character symbol slash-asterisk ( /* ) and ends
with an asterisk-slash ( */ ).
For example:
Program
/* This program displays the name and roll number of the student */
{
int rollno;
rollno = 100;
clrscr( );
Output: