C Language FAQ

Interview Questions on C Tokens
 

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.
Example Program:
int main ()
{
    int x, y, total;
    x = 10, y = 20;
    total = x + y;
    Printf (“Total = %d \n”, total);
}
Where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, total – identifier
main, {, }, (, ), int, x, y, total – tokens

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.

0 comments:

Post a Comment