Machine-translated by Google. Questions are authored in English.
Top

C Constants Questions and Answers

81.

Comment on the output of this C code?

#include <stdio.h>
    int const print()
    {
        printf("Sanfoundry.com");
        return 0;
    }
    void main()
    {
        print();
    }

 

Answer: B

Correct answer: Sanfoundry.com

The string is printed once and the program ends. Applying const to a function's return type qualifies the returned value; for a return type of int it has essentially no effect, since the value is a copy that the caller here discards.

It is certainly not an error - a return type may be qualified - and nothing in the code causes repetition, print() being called once from main. The trap is reading int const print() as something unusual; it is an ordinary function with a redundant qualifier.

Please log in or register to report this question.

82.

Constants in C can be divided into categories ?

Answer: C

Correct answer: 2

C constants are conventionally divided into two categories: primary and secondary.

Primary constants are the fundamental values - integer, real and character constants. Secondary constants are those built from them, covering arrays, pointers, structures, unions and enumerations. The division mirrors the distinction between primitive and derived data types, and it is a teaching taxonomy rather than a distinction the language standard itself draws.

Please log in or register to report this question.

Answer: D

Correct answer: Enum constant

The primary constants are integer, real and character constants - the three fundamental kinds of value written directly in source.

An enumeration constant is classed as secondary, since it is built on the integer type rather than being a distinct kind of literal in its own right. In the same classification, arrays, pointers, structures and unions are also secondary, being composed from the primary types.

Please log in or register to report this question.

84.

------not a secondry constant.

Answer: A

Correct answer: Character

A character constant is a primary constant, alongside integer and real constants - one of the three fundamental kinds of value written directly in source.

The secondary constants are those built from the primary ones: arrays, pointers, structures, unions and enumerations. The division parallels the distinction between primitive and derived types, so the odd one out here is the only item that is not composed of anything more basic.

Please log in or register to report this question.

85.

We cannot use the way to declare the constant.

Answer: C

Correct answer: int constant a=1

There is no keyword constant in C. The qualifier is const, so int constant a=1; would be read as declaring a variable named constant of type int, followed by a stray a=1, and would not compile.

The first two forms are both valid and equivalent: a type qualifier may appear on either side of the type specifier, so const int a = 1; and int const a = 1; declare exactly the same thing.

Please log in or register to report this question.

86.

The constants are also called as _____________

Answer: C

Correct answer: literals

Constants written directly in source are called literals - the value appears literally in the text rather than being referred to by a name.

The categories are integer literals, floating-point literals, character literals and string literals. const is a type qualifier applied to an object, which is a different idea: a const object has a name and an address, whereas a literal is a value in an expression. A variable is by definition the thing a constant is not.

Please log in or register to report this question.

Answer: D

Correct answer: all of the mentioned

All the kinds named are literal constants: integer numerals, floating-point numerals, strings, character constants and, in C99 and later through stdbool.h, boolean values.

Each has its own syntax and its own type. Integer literals may carry u and l suffixes and a radix prefix, floating-point literals default to double unless suffixed, character literals have type int in C, and string literals are arrays of char with an appended null.

Please log in or register to report this question.

Answer: C

Correct answer: both const keyword and #define preprocessor

Both mechanisms are used, and they work quite differently. #define is a preprocessor directive performing textual substitution before compilation: the name has no type, no scope and no address, and the compiler never sees it.

const qualifies an actual object, which has a type, obeys the ordinary scoping rules, occupies storage and is visible to the debugger. const is generally preferred for that reason, though #define is still needed where a value must be a constant expression usable in contexts such as an array bound in older C.

Please log in or register to report this question.

89.

What will be the output of the following C++ code?

#include <iostream>
    using namespace std;
    int main()
    {
        int  const  p = 5;
        cout << ++p;
        return 0;
    }

 

Answer: C

Correct answer: Error

p is declared const, so ++p attempts to modify a const-qualified object and the compiler rejects it.

The order of int and const is immaterial - int const p and const int p are the same declaration - so nothing is wrong with the declaration itself. It is the increment that fails, and it fails at compile time: const-ness is enforced entirely by the compiler and has no run-time representation.

Please log in or register to report this question.

Answer: D

Correct answer: They end with a semicolon

This is the false statement. A preprocessor directive is terminated by the end of the line, not by a semicolon - and adding one has consequences, since a semicolon written at the end of a #define becomes part of the replacement text and is substituted along with the value.

The other three are true: directives are read and processed by the preprocessor, they generate no code themselves but control what the compiler sees, and each must occupy its own line.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: C Constants Questions and Answers || C Constants MCQ Questions and Answers || C Constants GK Questions and Answers || C Constants GK MCQ Questions || C Constants Multiple Choice Questions and Answers || C Constants GK || GK on C Constants || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming