Exercise: Declarations And Initializations

Questions for: Declarations And Initializations

When we mention the prototype of a function?
A:
Defining
B:
Declaring
C:
Prototyping
D:
Calling
Answer: B

A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, argument types and return type.

While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.

In the following program where is the variable a getting defined and where it is getting declared?
#include<stdio.h>
int main()
{
    extern int a;
    printf("%d\n", a);
    return 0;
}
int a=20;
A:
extern int a is declaration, int a = 20 is the definition
B:
int a = 20 is declaration, extern int a is the definition
C:
int a = 20 is definition, a is not defined
D:
a is declared, a is not defined
Answer: A

- During declaration we tell the datatype of the Variable.

- During definition the value is initialized.

Identify which of the following are declarations
1 : extern int x;
2 : float square ( float x ) { ... }
3 : double pow(double, double);
A:
1
B:
2
C:
1 and 3
D:
3
Answer: C
extern int x; - is an external variable declaration.

double pow(double, double); - is a function prototype declaration.

Therefore, 1 and 3 are declarations. 2 is definition.
Is the following statement a declaration or definition?
extern int i;
A:
Declaration
B:
Definition
C:
Function
D:
Error
Answer: A

Declaring is the way a programmer tells the compiler to expect a particular type, be it a variable, class/struct/union type, a function type (prototype) or a particular object instance. (ie. extern int i)

Declaration never reserves any space for the variable or instance in the program's memory; it simply a "hint" to the compiler that a use of the variable or instance is expected in the program. This hinting is technically called "forward reference".

Which of the following is not user defined data type?
1 :
struct book
{
    char name[10];
    float price;
    int pages;
};
2 :
long int l = 2.35;
3 :
enum day {Sun, Mon, Tue, Wed};
A:
1
B:
2
C:
3
D:
Both 1 and 2
Answer: B

C data types classification are

  1. Primary data types
    1. int
    2. char
    3. float
    4. double
    5. void
  2. Secondary data types (or) User-defined data type
    1. Array
    2. Pointer
    3. Structure
    4. Union
    5. Enum

So, clearly long int l = 2.35; is not User-defined data type.
(i.e.long int l = 2.35; is the answer.)

Ad Slot (Above Pagination)
Quiz