C Language Interview Questions Part 4

Interview Questions on Tokens and Constants in C Language

1. What is a C Token?
Tokens are the basic building blocks of C programming. In a C source program, the basic element recognized by the compiler is the "token."

The compiler first groups the characters into tokens. A token is source-program text that the compiler does not break down into component elements.

Example: if, for, while, sum, add, +, -, /, ++, +=, = etc.

2. List the different types of C Tokens?

C has six types of Tokens.

Keyword: for example int, char, while, else, class.
Identifier: for example main, amt, area, radius etc.
Constant: for example 121, 500, -150.
String: for example “PRIT”, “Hello” etc.
Operators: for example + - * /
Special symbols: for example { }, @, [ ] etc.

3. What is an identifier?

Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions and labels.

An identifier can be composed only of uppercase, lowercase letters, underscore and digits, but should start only with an alphabet or an underscore.

If the identifier is not used in an external link process, then it is called as internal. Example: Local variable. If the identifier is used in an external link process, then it is called as external. Example: Global variable.

An identifier is a string of alphanumeric characters that begins with an alphabetic character or an underscore character that are used to represent various programming elements such as variables, functions, arrays, structures, unions and so on.
Actually, an identifier is a user-defined word. There are 53 characters, to represent identifiers. They are 52 alphabetic characters (i.e., both uppercase and lowercase alphabets) and the underscore character. The underscore character is considered as a letter in identifiers. The underscore character is usually used in the middle of an identifier. There are 63 alphanumeric characters, i.e., 53 alphabetic characters and 10 digits (i.e., 0-9).

4. What are the rules to be followed for Identifiers?

There are certain rules that should be followed while naming c identifiers:

•    They must begin with a letter or underscore (_).
•    They must consist of only letters, digits, or underscore. No other special character is allowed.
•    It should not be a keyword.
•    It must not contain white space.
•    It should be up to 31 characters long as only first 31 characters are significant.

Some examples of c identifiers:
_A9: Valid identifier.
Temp.var: Invalid identifier as it contains special character other than the underscore.
void: Invalid as it is a keyword.

5. What is Internal Identifier?

If the identifier is not used in an external link process, then it is called as internal. These identifiers are also known as internal names; includes the names of local variables. It has at least 31 significant characters.

6. What is External Identifier?

If the identifier is used in an external link process, then it is called as external. These identifiers are also known as external names; include function names and global variable names that are shared between source files. It has at least 63 significant characters.

7. Define Keyword?

C programs are constructed from a set of reserved words which provide control and from libraries which perform special functions.

The basic instructions are built up using a reserved set of words such as main, for, if, while, default, double, extern, for, int etc.

C demands that they are used only for giving commands or making statements. You cannot use default, for example, as the name of a variable. An attempt to do so will result in a compilation error. The keywords are also called ‘Reserved words’.

Keywords have standard, predefined meanings in C. These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers. 

Keywords are an essential part of a language definition. They implement specific features of the language. Every C word is classified as either a keyword or an identifier.

A keyword is a sequence of characters that the C compiler readily accepts and recognizes while being used in a program.
Note that the keywords are all lowercase. Since uppercase and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier.

8. List different types of keywords?

C language supports 32 keywords which are given below.
auto, double, int, struct, break, else, long, switch, case, enum,typedef, char, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile do if  static while, register.

9. What are the restrictions applied for keywords?

•    Keywords are the words whose meaning has already been explained to the C compiler and their meanings cannot be changed.
•    Keywords can be used only for their intended purpose.
•    Keywords cannot be used as user-defined variables.
•    All keywords must be written in lowercase.

10. State the meaning of the following keywords in C: auto, double, int, long?

auto: auto is used to define a variable of storage class automatic.
For example: auto int var1;

This statement suggests that var1 is a variable of storage class auto and data type int. Variables declared within function bodies are automatic by default. They are recreated each time a function is executed. Since, automatic variables are local to a function; automatic variables are also called local variables.

double and float: double and float are used for indicating floating type variables. Keywords float and double represent single precision and double precision floating point data respectively.
For example: float variable1;
                     double variable2;
Here, variable1 is single precision floating type variable whereas, variable2 is a double precision floating type variable.
int: int is used for indicating the variable is of type integer.
For example: int var0;
Here, var0 is a variable of type integer.

long: Specifies a variable can hold fairly large integer type of data.
Syntax: long int var1;

11. What are Qualifiers?

Qualifiers or modifiers are identifiers that may precede the scalar data types (except float) to specify the number of bits used for
representing the respective type of data in memory. The qualifiers in C are short, long, signed, and unsigned.

12. What is a string?

A string is a sequence of characters ending with NULL. It can be treated as a one–dimensional array of characters terminated by a NULL character.

13. What is a constant?

A constant is a value that does not change during the program execution. A constant used in C does not occupy memory.

14. What is an Operator?

An operator is a symbol or a special character that tells the computer to perform certain mathematical or logical manipulations which is applied to operands to give a result. It can operate on integer and real numbers.

Operators are used in programs to manipulate data and variables. They usually form a part of mathematical or logical expressions.

15. What are Special symbols?

The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.
For Example: [] () {},:: * … = #

Braces {}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement.

Parentheses (): These special symbols are used to indicate function calls and function parameters.

Brackets []: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.

16. What is a constant?

Constant in C language is that item whose value remains same during the execution of the program from the beginning till the end of a program.

Constant may be a numeric constant or an unknown numeric constant. Although a constant's value is specified only once, a constant may be referenced many times in a program.

Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).

17. How to declare and use constants? :

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.
The general syntax of constant declaration is:
Syntax: const data-type name = value;
Here const is known as a keyword that must be used to declare a constant.  Name is the name of the constant, data-type is the type of the data which we are declaring and value is the constant value of the data-type.
For example: const int manager_id=001;
Here we declared a constant named as manager_id of type integer which is assigned a value 001.

There are two simple ways in C to define constants:

•    Using #define preprocessor.
•    Using const keyword.

The #define Preprocessor: Following is the form to use #defines preprocessor to define a constant:
#define identifier value
For example:
•    #define FIRST_NUMBER 1

The const Keyword: You can use const prefix to declare constants with a specific type as follows:
const type variable = value;
For example:
•    const float pie =3.147;
•    const int radius =4;
•    const char c = 'A';

18. What is the benefit of using #define to declare a constant?

Using the #define method of declaring a constant enables you to declare a constant in one place and use it throughout your program. This helps make your programs more maintainable, because you need to maintain only the #define statement and not several instances of individual constants throughout your program.

For instance, if your program used the value of pi (approximately 3.14159) several times, you might want to declare a constant for pi as follows #define PI 3.14159.

Using the #define method of declaring a constant is probably the most familiar way of declaring constants to traditional C Programmers. Besides being the most common method of declaring constants, it also takes up the least memory.

Constants defined in this manner are simply placed directly into your source code, with no variable space allocated in memory. Unfortunately, this is one reason why most debuggers cannot inspect constants created using the #define method.

19. What is the difference between Constants and Variables?

A constant, Variable or a keyword can be formed from any combination of Alphabets, Digits and Special Symbols. A constant is an entity whose value does not change throughout the program execution.

A variable is an entity whose value keeps on changing throughout the program execution. However, it’s not a rule that the value of the variable will change.

A variable value might remain same throughout the program execution. However the main difference between variable and constant is that we can’t change the value of constant in between the program, but we can change the value of the variable during program execution.
Variable's syntax is: data_type variable_name;
Constant's syntax is: const data type constant_name=value;

Variables can be initialized after its declaration where as constants must be initialized at the time of their declaration otherwise they will take a garbage value.

Let us take an example:
We define a variable named x in a program. Let us say x=3.After sometime during execution we change the value of x to 6. Now, x=6.Here value of x is getting changed. But the value of 3 and 6 will never change. Hence, x is called a variable and 3 is called a constant.

20. What are the different types of Constants in C language?

There are four basic types of constants in C. They are:

•    Integer constants
•    Floating-point constants
•    Character constants
•    String constants

Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants.

21. What are the rules to be applied to all numeric-type constants?

The following rule applies to all numeric type constants:

•    Comma and blank spaces cannot be included within the constants.
•    Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be               positive.
•    The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from           one C compiler to another.

22. What are Integer constants in C language?

Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits.
An "integer constant" is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value.
Use integer constants to represent integer values that cannot be changed.

23. What are the rules for constructing Integer constants?

•    An integer constant must have at least one digit.
•    It must not have a decimal point.
•    It can either be positive or negative.
•    No commas or blanks are allowed within an integer constant.
•    If no sign precedes an integer constant, it is assumed to be positive.
•    The allowable range for integer constants is -32768 to 32767.

24. What are decimal integers?

A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants.


25. What are octal integer constants?

An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants.


26. What are hexadecimal integer constants?

A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lower-case). The following are valid hexadecimal integer constants.


27. What are unsigned and long integer constants?

Unsigned integer constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.

Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.
For example,
 50000U decimal unsigned.
 1234567889L decimal long.
 0123456L octal long.
 0777777U octal unsigned.

28. What are real or Floating-point constants in C language?

A real or floating-point constant is a base-10 number that contains either a decimal point or an exponent or both.

A floating-point constant can be written in two forms: Factorial form or Exponential form.

A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used.

29. What are the Rules for constructing Real constants (Fractional Form)?

Rules for constructing Real constants (Fractional Form) are,

•    A real constant must have at least one digit
•    It must have a decimal point
•    It could be either positive or negative
•    If no sign precedes an integer constant, it is assumed to be positive.
•    No commas or blanks are allowed within a real constant.

E.g.: +867.9, -26.9876, and 654.0

30. What are the Rules for constructing Real constants (Exponential Form)?

Rules for constructing Real constants (Exponential Form) are,

•    The mantissa part and the exponential part should be separated by the letter ‘e’
•    The mantissa may have a positive or negative sign(default sign is positive)
•    The exponent must have at least one digit
•    The exponent must be a positive or negative integer (default sign is positive)
•    The range of real constants in exponential form is -3.4e38 and +3.4e38

E.g.: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4


Post a Comment