Вы находитесь на странице: 1из 10

Chapter No 2: Variables & Constants

Q1: Distinguish between constants and variables?

Constants:
The quantity whose value cannot change during the execution of the program
is called constant. It may be a numeric or a non numeric quantity.

Variables
A quantity whose value may change during execution of the program is called
variable. It may be a numeric or a non-numeric quantity. It is represented by a symbol
called variable name. The variable name consists of alphabets and digits.
A variable represents a storage or memory location in the computer memory.
Data is stored into the memory location. The name of the memory location, i.e. the
variable name, remains fixed during execution of the program but the data stored in
that location may change from time to time.

Q2: Describe the rules for writing names of variables?

Rules for writing Variable Names:


The following are the rules for writing a variable name in a program in C:
• The first character of variable name may be an alphabetic character or an
underscore (_).
• The first character of variable name cannot be a digit.
• Blank spaces are not allowed in a variable name.
• Special characters, such as arithmetic operators, #, ^, cannot be used in a
variable name.
• Reserved C words cannot be used as variable names.
• The maximum length of a variable name is up to 31 characters. However, the
maximum length depends upon the compiler.
• A variable name declared for one data type cannot be used to declare another
data type.
C is a case sensitive language. Thus variable names with same spelling but different
cases are treated as different variable names. For example, variables 'Pay' and 'pay' are
two different variables.
Following are some examples of the valid and invalid variable names.

Variable Name Valid / Invalid Remarks


Ahmed Valid
Perform Valid
double Invalid C reserved word
foxpro Valid

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

switch Invalid C reserved word


_large Valid
Float Valid The keyword float is in
lower case
Marriam Valid
int Invalid C reserved word
3taq Invalid Starts with a numeral
unsigned Invalid C reserved word
x-y Invalid Special character is not
allowed
Taq Ahd Invalid Space is not allowed

Q3: Describe various types of data in C?

Data Types in C:
The actual values used in a program are called data. There are four basic types of data
in C. these are:

int Integer
float Real Values
double Large real values
char Characters

The following table shows various data types in C.

Data Types
Type Size in Bytes Data storage Range
int 16 bits (2 bytes) -32768 to 32767
short int 16 bits (2 bytes) -32768 to 32767
long int 32 bits (4 bytes) -2147483648 to
2147483647
unsigned int 16 bits (2 bytes) 0 to 65635
unsigned long int 32 bits (4 bytes) 0 to 4294967295
float 32 bits (4 bytes) 1.1x10-38 to 3.4x10+38
long float 64 bits (8 bytes) 2.2x10-308 to 1.7x10+308
double 64 bits (8 bytes) 2.2x10-308 to 1.7x10+308
long double 80 bits (10 bytes) 3.4x10-4932 to 1.1x10-4932
char 8 bits (1 bytes) In case of strings, storage
capacity is from 1 byte to
65535 bytes.

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

Q4: Describe Integer type data?

The Integer Type Data:


An integer is a whole number, i.e. a number without a fraction or decimal
point. It may have a positive or a negative value. For example, 601, 250, -6 and 501
are integers.
Depending upon the maximum and minimum values, there are four types of
integer type data in C. these are:

1. int
2. short int
3. long int
4. unsigned int

The int:
The range of values of int data type depends upon the computer system being
used and it takes two to four bytes. In MS DOS, an integer type variable takes two
bytes in the memory and the range of values stored is from -32768 to 32767.
The maximum and minimum values for integer data types are specified in
integer.h header file. The minimum integer value is defined by INT_MIN and
maximum integer value is defined by INT_MAX.

The short int:


A short int type data takes two bytes in memory. It can store integer values
from -32768 to 32767.
The minimum integer value is defined by SHER_MIN and maximum integer
value is defined by SHRT_MAX.

The long int:


A long int type data takes four bytes in memory. It can store integer values
from -2147483648 to 2147483647.
The minimum integer value is defined by LONG_MIN and maximum integer
value is defined by LONG_MAX.

The unsigned int:


The unsigned int can store only positive whole numbers. It takes two bytes in
memory. It can store integer values from 0 to 65,535.

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

The minimum unsigned integer value is 0 and maximum unsigned integer


value is defined by UNIT_MAX.

Q5: Describe Real Type Data?


The real numbers consists of an integer and a non integer part. These are also
called floating point numbers. The data consisting of real numbers is called float type
data.
Depending upon the maximum and minimum values, there are two types of
real type data in C. these are:

1. float
2. double

The float Type Data:


The float data is represented in decimal or exponential form. It may be signed
or unsigned. For example, 23.26, 16.21, -10.5 are floating type data.
The float type data takes four bytes in the memory and it can store real values
from 1.1 x 10-38 to 3.4 x 10+38. it is accurate up to 6 decimal places.
The maximum and minimum values for float data types are specified in float.h
header file. The minimum float value is defined by FLT_MIN and maximum float
value is defined by FLT_MAX.

The double Type Data:


The double data type is also a real type data. It is used to store large real
values. A double type variable takes 8 bytes in the memory and it can store real values
from 1.7 x 10-308 to 1.7 x 10+308. It is accurate up to 12 decimal places. The minimum
double value is defined by DBL_MIN and maximum double value is define by
DBL_MAX.

The long double Type Data:


The long double type data are used to store very large real data values. A long
double variable takes ten bytes in the memory. A long double variable can store real
values from 3.4 x 10-4932 to 1.1 x 10+4932. It is accurate up to 18 decimal places.

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

Q6: Explain how real numbers are expressed in scientific or exponential notation?

The Exponential Notation


The general form of a real number is:
±n . m
where
n represents the integer part of real number.
m represents the fractional part of real number

A real type data can also be represented in exponential form. In this form, a real
number is divided into two parts i.e. a mantissa and an exponent. The general form of
a real number in exponential form is:
±mE ±n or ±me ±n

where
m represent the mantissa. It has an absolute value greater than or equal to
1.0 and less than 10.0
n represents the value of exponent. It is a real number.

For example, the real number 15000000.0 can be written in scientific form as: 1.5 x
106. In exponential form it can be written as +1.5e+6 or 1.5e6
Where 1.5 represents mantissa (m) and 6 shows the value of exponent (n).
The following table gives a real number and its equivalent scientific and exponential
forms.

Real Number Scientific Form Exponential Form


0.00005 5.0x10-5 5.0e-5
50000000.0 5.0x107 5.0E7
-0.00000162 -1.62x10-6 -1.620E-6
550.0 5.5x102 5.5e2
-3.66 -3.66x100 -3.66e0

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

578.9 5.789x102 5.759e2


-66.607 -6.6607x101 -6.6607e1

The following exponential forms of real numbers are invalid:

6E6.2 Exponent must be an integer value


7762 It is an integer value
2.31-e2 Sign is in wrong place
e6 Mantissa does not exist
0.87e9 *mantissa is less than 1
1.0e Exponent value missing
22.2e15 Mantissa is more than 10

Q7: Distinguish between precision and range of a real numbers?


Precision & Range:
The number of digits used by the computer for the decimal portion of the
mantissa specify the precision or accuracy of the real value. For example the speed of
light is 299792000 m/s. It is represented in the computer memory as 2.99792e8. Thus
it requires a precision of at least 5 digits in the decimal portion of the mantissa to
accurately represent this quantity. Similarly, the number of digits allowed for the
exponent specify the exponent range of the real value. For example the exponent
range of a float value is from -38 to 38. any real value that requires a precision of 6 or
less digits and the exponent range is from -38 to 38, can be declared as of float type. If
a value requires a precision of more than 6 digits, it may be declared as double or long
double type.

Q8: Describe Character type Data?


The char Type Data:
Character type data consists of alphabetic characters, numeric digits and
special characters. A single character takes 8 bits or one byte in the memory. The
maximum and minimum values for character data type are specified in integer.h
header file. The minimum char value is defined by CHAR_MIN and maximum char
value is defined by CHAR_MAX. Arithmetic operations can also be performed on
char type variables.

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

Q9: What are different types of constants in C?


Constants
A quantity that cannot change its value during execution of the program is
called constant. There are four types of constants in C. these are:
integer constants
floating point constants
string constants
character constants

Integer Constants
A numerical value without a decimal is called integer constant. The minus (-)
sign is used to indicate a negative integer constant. The use of (+) sign with a positive
integer constant is optional.
Integer constants are used in expressions for calculations. The Integer
constants are written without ".", "e", or "f". For example:
1000 -87
The long integer constants are written with L appended. For example:
8000000L -86316L

Floating Point Constants


Numeric values that have an integer as well as a decimal part are called
floating point values. These values can also be written in exponential notation. The
floating point constants are written with "f" or "F" appended. If f or F is not appended,
the constant is treated as double. For example:
1000.f -9.87f 7.86F
The double constants are written with "." or "e". For example:
8000000.0 -8.63165e12
The long double constants are written with "." or "e" and L appended. For example:
8000000.L -8.631650e12L

Character Constants
A single character enclosed in single quotation marks is called character
constant. For example 'a', '/', and '+' represent character constants.

String Constants
A sequence of characters consisting of alphabets, digits and/or special
characters enclosed in double quotation marks is called string constants. For example,
"Pakistan" and "Lahore" are examples of string constants.

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

Q10: What is meant by declaration of variables?


Declaration of Variables
Specifying the name of a variable and the data type it can hold is called
declaration of the variable. All variables that are used in a program are declared using
variable declaration statement.
When a variable is declared, a certain number of bytes in the memory are
allocated to the variable name. The amount of the memory is allocated according to
the type of variable. The value of the variable is stored in this memory space. The
syntax to declare a variable in C is:

type list of variable


where
type specifies data type of variables. For example, int is used
to declare an integer type variable and float is used to
declare a float type variable.
List of Variables specifies a list of variables separated by commas. In a
single statement more than one variables, separated by
commas, of same data type can be declared.
Different statements are used to declare variables of different data types. The integer
type variables are declared using the int keyword. For example, to declare a variable
"abc" of integer type, the statement is written as:
int abc;
The short integer type variables are declared using the short int keyword or simple
short keyword. For example, to declare a variable "marks" of short integer type, the
statement is written as:
short int marks;
or short marks;

The long integer type variables are declared using the long int keyword. For example,
to declare a variable "length" of long integer type, the statement is written as:
long int length;
The float type variables are declared using the float keyword. For example, to declare
a variable "length" of float type, the statement is written as:
float height;
The double type variables are declared using the double keyword. For example, to
declare a variable "distance" of double type, the statement is written as:
double distance;
The long double type variables are declared using the long double keyword. For
example, to declare a variable "huge" of long double type, the statement is written as:
long double huge;
The character type variables are declared using the char keyword. For example, to
declare a variable "nm" of char type, the statement is written as:
Char nm;
More than one variable of the same data type can also be declared in one statement. In
this case, the names of the variables are written after the keyword separated by

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

commas. For example, to declare three variables "xyz", "d" and "s" of integer type,
the statement is written as:
Int xyz, d, s;
To declare different types of variables, different statements are used. For example, to
declare variables a and xy as int types, b as float type, nm as character type and sum
as double type, the statement are written as:
int a, xy;
float b;
char nm;
double sum;

Q11: What is meant by initialization of Variables?


Initialization of Variables:
When a variable is declared, a memory location is assigned to it. A value can
also be assigned to it at the time of its declaration. Assigning a known value to a
variable at the time of its declaration is called initializing of the variable. For example,
to declare variables a, b, and c of integer type and assigning values a = 110 and b =
60, the statements are written as:
int a=100, b=60, c;
float num=67.3f;
Q12: What are symbolic constants? How are they defined in a C
program?
A symbolic constant is an identifier that represents a constant value. For
example, the value of Л (pi) is frequently used in engineering calculations. It
represents a constant value and is approximately equal to 3.141593. Instead of typing
the numerical value each time it is to be used, this value is assigned to an identifier
and that identifier is written whenever the value of pi is to be used. This identifier
represents a constant value and is called the symbolic constant.
There are two way to define a symbolic constant. These are given below with
detail.
• Using the "define" Directive
• Using the "const" Directive

The "define" Directive


It is a preprocessor directive. It is used to assign a constant quantity to an
identifier. This directive can be used anywhere in the program. Its syntax is given
below.
#define identifier constant
where

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252
Chapter No 2: Variables & Constants

identifier specifies the identifier name to which the constant value


is to be assigned.
constant specifies the constant value that is to be assigned to the
identifier.
The #define statement does not end with a semicolon. The identifier specified in the
define directive is not a variable it does not have any data type. The preprocessor
simply replaces all occurrences of the identifier with its value in the statements that
follow the directive. For example, in the following statement, PI has been defined to
represent 3.141593f.
#define PI 3.141593f
It is a convention in C to write identifiers that appear in a #define statement in capital
letters. This is done to distinguish then from variables. A variable of the name of the
identifier cannot be declared in the program.

The "const" Qualifier


The "const" qualifier is used to define a variable with a value that is fixed. The
value is assigned to the variable at the time of its declaration. In the following
statement, PI has been declared as floating point variable and a fixed value 3.141593f
has been assigned to it.
const float Pi=3.141593f;

Malik Tayyab Email: tayyab8632@yahoo.com, malik8632@yahoo.com


Mob: 03445064252

Вам также может понравиться