The C Declarations
1. What are the different data types?
Ans: C data type is classified as follows:
1. Basic data type: Basic data types are (a) integer (int), (b) character (char), (c) floating point
(float) and (d) double floating point (double).
2. Derived data type: Derived data types are pointers, functions and arrays.
3. User-defined type: User-defined data types are struct, union, enum and typedef.
4. The void data type: It is an empty data set. This data type can be followed from the chapter on
Functions.
2. What are the differences between signed and unsigned data types?
Ans: When a variable is declared as unsigned, the negative range of the data type is transferred
to positive, i.e., doubles the largest size of the possible value. This is due to declaring unsigned int;
the 16th bit is free and is not used to store the sign of the number.
Some differences between signed and unsigned data types are shown in the following table.
signed int unsigned int
Range: −32,768 to 32,767 Range: 0 to 65535
Format %d or %i prints signed integers Format %u prints unsigned integers
For long signed int, range is −2147483648 to 2147483647 For long unsigned int, range is 0 to 4294967295
Format %ld prints long signed integers Format %lu prints unsigned integers
3. What do we mean by a variable and a constant?
Ans: A ‘variable’ is a data name used for storing a data value. Its value may be changed during the
program execution. It has memory location and it can store a single value at a time. The ‘constants’ in C
are applicable to the values, which do not change during the execution of a program.
2
M02_ITL-ESL4791_02_SE_C02.indd 13 12/22/2012 4:59:41 PM
II-14 Programming Concepts
4. Explain different types of constants in C.
Ans: Constants in C are as follows:
1. Numerical Constants
(a) Integer constants: These are a sequence of numbers from 0 to 9 without decimal points or
fractional part or any other symbols. It requires minimum 2 bytes and maximum 4 bytes of
memory. Integer constant could be either positive or negative or may be zero.
Example: -10, 20, +30, -15.
Besides, representing the integers/real constants in decimal number system, they also can be
represented in octal or hexadecimal number system based on the requirement.
(b) Real Constants:Real constants are floating-point constants.
Example: 2.5, 5.521, 3.14.
2. Character Constant
(a) Single character constants:A character constant is a single character (length = 1). It can also
be represented with single digit or a single special symbol or white space enclosed within a
pair of single quote marks.
Example: 'a', '8', '-'.
(b) String constants:String constants are a sequence of characters enclosed within double quota-
tion marks.
Example: "Hello", "India", "444", "a".
5. How the octal and hexadecimal numbers represented in C?
Ans: Numbers or constants can be represented in octal or hexadecimal based on the requirement/
applications.
Octal has base 8 and hexadecimal has base 16. The octal numbers are 0, 1, 2, 3, 4, 5, 6, 7 and hexa-
decimal numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
The representation of octal numbers in C would be done with leading digit 0 and for hex representa-
tion with leading 0X.
Following are a few examples of octal and hexadecimal numbers.
027, 037, 072 – Octal numbers
0X9, 0Xab, 0X4 – Hexadecimal
6. What are the C keywords? Elaborate them.
Ans: The C keywords are reserved words by the compiler having fixed meaning. They cannot be
used as a variable name. The 32 C keywords provided in ANSI C are listed in the following table.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float unsigned short
continue for signed void
M02_ITL-ESL4791_02_SE_C02.indd 14 12/22/2012 4:59:41 PM
The C Declarations II-15
default goto sizeof volatile
do if static while
Additional keywords for Borland C:
asm cdecl far huge
interrupt near pascal
7. List the rules for declaring a variable.
Ans:
1. A variable must begin with a character without spaces but an underscore is permitted. The underscore
is treated as one type of character.
2. The length of the variable varies from compiler to compiler. Generally, most of the compilers support
eight characters. However, the ANSI standard recognizes the maximum length of a variable up to
31 characters.
For external names, the variable names should be of six characters and in single case.
3. The variable should not be a C keyword.
4. The variable names may be a combination of uppercase and lowercase characters. For example,
suM and sum are not the same. The traditional practice is to use lowercase letterers for variable
names and upper case letters for symbolic constants.
5. The variable name should not start with a digit.
6. Blanks and commas are not permitted within a variable name.
8. What are the identifiers?
Ans: Identifier is a symbolic name used to refer to a variable, constant, function, structure, union etc.
In other words, identifiers are the names of variables, functions, arrays etc.
Identifiers refer to variety of entities such as structures, unions, enumeration, constants, typedef
names, functions and objects.
Identifiers are user-defined names. Identifiers are generally defined in lower case letters. However,
the upper case letters are also permitted. The (_)underscore symbol can be used as an identifier. In
general, underscore is used to link between two words for the long identifiers.
9. Explain the methods for initialization of variables.
Ans: Variables declared can be assigned or initialized using assignment operator ‘=’. The declaration
and initialization can also be done in the same line.
Syntax:
variable_name = constant;
or
data_type varaible_name= constant;
Example: x = 5; where x is an integer variable.
Example: int y = 4;
M02_ITL-ESL4791_02_SE_C02.indd 15 12/22/2012 4:59:41 PM
II-16 Programming Concepts
10. Explain constants and volatile variables.
Ans:
1. Constant variable
If the value of a certain variable to remain the same or unchanged during the program execution,
it can be done by declaring the variable as a constant. The keyword const is then prefixed before
the declaration. It tells the compiler that the variable is a constant. Thus, constant declared variables
are protected from modification.
Example: const int m = 10;
2. Volatile variable
The volatile variables are those variables that can be changed at any time by other external
program or the same program. The syntax is as follows:
volatile int d;
11. Write a program on constant and volatile variables.
Ans:
void main()
{
const int a=11;
volatile num=12;
clrscr();
num=num-1;
printf("%d %d",num,a);
getche();
}
OUTPUT:
11 11
Explanation: Volatile variable can be changed, but constants cannot be changed. Hence, the results
shown are based on the theory.
12. Write about space requirement for variables of different data types.
Ans: Following chart gives the space requirement for different data types.
Data Type Space Requirement (Bytes)
char
1
unsigned char
1
short or int 2
unsigned int
2
long
4
unsigned long
4
float
4
double
8
long double
10
M02_ITL-ESL4791_02_SE_C02.indd 16 12/22/2012 4:59:42 PM
The C Declarations II-17
13. What are the delimiters? Explain their uses.
Ans: The language pattern of C uses special kind of symbols, which are called delimiters. They are:
Delimiters Symbols Use
Colon
:
Useful for label
Semicolon
;
Terminates statements
Parentheses
()
Used in expression and function
Square brackets
[]
Used for array declaration
Curly braces
{}
Scope of statement
Hash
#
Preprocessor directive
Comma
,
Variable separator
14. Is ‘main’ a keyword? Explain.
Ans: ‘main’ is not a keyword. The void main() function is a user-defined function. We can use
the word main as an identifier to create variables.
15. List any three keywords with their use.
Ans:
1. break:
It can be used to forcefully break an executing loop.
For example,
int i;
for(i=0;i<5;i++)
{
if(i==3) break;
}
2. goto:
It can be used to transfer the control to a specified location in the program using ‘label’.
For example,
int i;
scanf("%d",&i);
if(i==0)
goto ONE;
else goto TWO;
ONE: printf("Demo");
TWO: exit(0);
/*SOME PROGRAM CODE*/
3. return:
It can be used to return a definite value as per the return type of a function.
For example, ‘main’ returns an integer
M02_ITL-ESL4791_02_SE_C02.indd 17 12/22/2012 4:59:42 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset