Interview Questions on C language Constants
Interview Questions on C language Constants
1. What is a constant?
A C constant is usually just the written version of a number. The constants can never change their value. The constants value is locked for the duration of the program. Constants also provide a strong hint to the compiler for optimization. Since the compiler knows the value can’t change, it doesn’t need to load the value from memory and can optimize the code to work for only the exact value of the constant. Constants are also inherently static – you can declare the constant and its value in a header file and not have to worry about defining it exactly one place.
2. What are different types of constants?
C supports different types of constants. They are
1. Integer Constants.
2. Real Constants.
3. Single Character Constants.
4. String Constants.
3. What is an integer constant?
An integer constant is 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. Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator.
4. How many types of integers are there in C?
There are 3 types of integers. They are decimal integer, octal integers and hexadecimal integer.
5. What is a decimal integer?
Decimal Integer consists of a set of digits 0 to 9 preceded by an optional + or – sign. Spaces, commas and non digit characters are not permitted between digits.
Examples for valid decimal integer constants are as follows:
6. What is an octal integer constant?
Octal Integer constant consists of any combination of digits from 0 through 7 with a O at the beginning.
Some examples of octal integers are as follows:
7. What is hexadecimal integer constant?
Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refer to 10 to 15 in decimal digits.
Examples of valid hexadecimal integers are as follows:
8. What are the rules to declare integer constants?
The integer constant must have at least one digit. It should not contain a decimal place. It can be positive or negative. Use of blank space and comma is not allowed between real constants.
Examples: 1880, 184, -349
9. What is a real constant in C?
Real Constant consists of a fractional part in its representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082.
Examples of real constants are as follows:
10. How real numbers are represented in C?
Real Numbers can be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign.
11. What are the Rules for constructing Real Constants in Exponential Form?
Rules for Constructing Real Constants in Exponential Form are:
1. The mantissa part and the exponential part should be separated by letter in exponential form.
2. The mantissa part may have a positive or negative sign.
3. Default sign of mantissa part is positive.
4. The exponent part must have at least one digit, which must be a positive or negative integer. Default sign is positive.
5. Range of real constants expressed in exponential for is -3.4e38 to 3.4e38.
12. What is the use of real or floating-point constants?
Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices and so on. These quantities are represented by numbers containing fractional part. Such numbers are called real or floating point constants.
13. What are the different forms of Real constants in C?
The Real or Floating-point constants can be written in two forms:
1. Fractional or Normal form.
2. Exponential or Scientific form.
14. How do express a Real constant in fractional form?
Expressing a Real constant in fractional form:
A real constant consists for a series of digits representing the whole part of the number, followed by a decimal point, followed by a series of representing the fractional part. The whole part or the fractional part can be omitted, but both cannot be omitted. The decimal cannot be omitted. That is, it is possible that the number may not have digits before the decimal point or after the decimal point.
Valid Real constants (Fractional): 0.0 -0.1 +123.456 .2 2.
15. What are the rules for Constructing Real Constants in Fractional Form?
Rules for Constructing Real Constants in Fractional Form are as follows:
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. Commas or blanks are not allowed within a real constant.
16. State the rules for constructing real constants in Exponential form?
Expressing a real constant in Exponential form as follows:
A real constant is combination of a whole number followed by a decimal point and the fractional part. If the value of a constant is either too small or too large, exponential form of representation of real constants is usually used.
In exponential form, the real constant is represented in two parts.
Mantissa – The part appearing before e, the mantissa is either a real number expressed in decimal notation or an integer.
Exponent – The part following e, the exponent is an integer with an optional plus or minus sign followed by a series of digits. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase.
17. What is character constant in C?
A character constant is formed by enclosing a single character from the representable character set within single quotation marks (‘ ‘). Character constants are used to represent characters in the execution character set.
18. What is a single character constant in C?
A Single Character constant represent a single character which is enclosed in a pair of quotation symbols.
Example for character constants are
All character constants have an equivalent integer values which are called ASCII Values.
19. What is a Character Constant in C?
A character is a single letter from an alphabet. C defines two basic alphabets; one in which the source code is written and the one which is used when the program is run. They are usually the same, but they do not have to be. If they are different, it is the compiler’s job to translate character constants from the source code alphabet to the runtime alphabet.
20. How many types of Character constants are there in C?
There are two types of character constants
2. Escape sequence.
21. What is direct character constant?
A single character is enclosed within single quotation marks.
22. What is Escape sequence character constant?
More than one character begins with single quotation mark. The first character must be (/).
23. What is string constant in C?
A sequence of characters enclosed within double quotes. It can be digits, alphabets, special characters.
Example of string constants are:
24. What is floating point constant in C?
A floating point constant is a sequence of digit with decimal point means real number. It is prefixed with plus or minus sign.
25. What are backslash character constants [escape sequences] in C?
Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character
26. What is the difference between #define and constant in C?
A #define is used as immediate constant or as a macro where as the constant is a variable whose value cannot change. Pointer can be declared to a constant, but not for #define. The #define cannot define externally, and there is no way to use it for availability to the linker where as the constant can be global.
27. What is an Enumeration Constant?
Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable.