C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 8)
8.
Is the following statement a declaration or definition?
extern int i;
Declaration
Definition
Function
Error
Answer: Option
Explanation:

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".

Discussion:
38 comments Page 3 of 4.

Princydhas said:   1 decade ago
int i=1;

This statement is definition or declaration ?

Priya said:   1 decade ago
What is the difference between declaration and definition?

Janani said:   1 decade ago
main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}
Output is error .please explain

Pri said:   1 decade ago
It means some rules and regulations.

Devendra said:   1 decade ago
What is meaning of prototype?

Venky said:   1 decade ago
Let's consider file name is a1.c :

#include<stdio.h>
{
int i=10;
}


Then let us consider another file a2.c :

#include"a1.c"
#include<stdio.h>
main()
{
extern int i;
printf("%d\n",i);
}


Now this will print output is 10.

This is the right way how to use extern keyword.

Naveen Dahiya said:   1 decade ago
I am not agree with Wikiok.

We also can not declare a user defined data type like any structure or union because the memory is also allocated to these (structures or unions) in the same way as it is assigned to any int or float variable.

Wikiok said:   1 decade ago
Declaration: no memory is used. You can not declare a non user defined data type: "int a" is a definition. But you can declare any user defined data type: "struct mystruct{int a; };" It hasn't use any memory. In this view function prototypes are declarations too. "void myfunction(int a);"

Definition: Memory has been used in code or data segment. "int a;" "struct mystruct myvariable;" void myfunction(int a) {printf("%d",a); }

Nancy said:   1 decade ago
int a;
its a definition not declaration.
actually a variable is said to be defined if memory is allocated to that variable.
and when we write, int a;
a is assigned 2 bytes of memory and may vary according to compliler.
int a; / its a definition of variable
a=10;
if we carefully analyse second statement, value is assigned to a variable,its called initialization of a variable
a=10; / initialization of variable

Prathyusha said:   1 decade ago
HELLO Sagarborse,
u asked wat is declaration and definition in the context of a variale.
here i am with a program demonstrating this,
#include<stdio.h>
void main()
{
int a; // variable "a" declaration
a=10; // variable "a" definition
printf("value of a is %d",a);
}

output:value of a is 10


correct me if i am wrong plz!....


Post your comments here:

Your comments will be displayed after verification.