"Everything should be made as simple as possible, but not simpler."
- Albert Einstein
8.
Is the following statement a declaration or definition? extern int i;
[A].
Declaration
[B].
Definition
[C].
Function
[D].
Error
Answer: Option C
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".
extern here refers to type of storage class in c . If you have 2 files say A and B and you want to access the global variable declared in A to be appearing in B then u culd do so by adding extern keyword . Refer to google if not satisfied .
Rahul said:
(Sat, Aug 7, 2010 03:25:08 AM)
Why we use extern keyword?
Sagarborse said:
(Tue, Aug 31, 2010 08:29:39 AM)
When we are declaring any function after main() function then we declare it before its use which is declaration
and what that method is going to invoke that code is its definition
But what is declaration and definition in context of Variable?
Atul said:
(Fri, Sep 24, 2010 04:02:16 PM)
Sagarborse tells us about prototype not for extern.
Extern is use to get access power to use the data member which is store in other file.
Sureshkumar said:
(Mon, Jan 3, 2011 12:22:48 AM)
Where should I use extern? can you tell little deeper.
Sandeep said:
(Tue, Jan 25, 2011 10:14:17 AM)
When you want all your program variables and functions to use a particular defined value then that could be declared as EXTERN.
More clearly...When many functions or variables need to access a particular variable whose value should be the same for all those which refer it (even if the defined value is changed. All those should access the same new value) then "extern" is used.
Appu said:
(Thu, Feb 3, 2011 07:39:59 PM)
int i=1;
This statement is definition or declaration ?
Anybody help me?
Prathyusha said:
(Sat, Mar 5, 2011 03:01:07 AM)
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!....
Nancy said:
(Sat, Mar 5, 2011 09:22:45 AM)
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
Wikiok said:
(Thu, Mar 10, 2011 02:53:31 AM)
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); }
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.
Venky said:
(Fri, Jul 29, 2011 11:54:17 PM)
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.
Devendra said:
(Sat, Aug 20, 2011 03:34:56 PM)
What is meaning of prototype?
Pri said:
(Tue, Nov 22, 2011 08:00:57 PM)
It means some rules and regulations.
Janani said:
(Sun, Dec 25, 2011 09:51:30 PM)
main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}
Output is error .please explain