816 views
1 1 vote
Whenever we declare a variable then compiler will not look for other details such as definition of the variable. why is this statement true ?

2 Answers

Best answer
2 2 votes

Explanation :  The compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker (you might have heard these terms as Linker and Loader) can find the definition from another file. If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.

• selected by
0 0 votes
A variable in simple terms is a storage place which has some memory allocated to it
when we declare a variable memory is not allocated for that that's why we can declare variable more than one time but it should be defined only once. At the time of definition, memory is allocated for it


// This is only declaration. y is not allocated memory by this statement 
  extern int y; 

  // This is both declaration and definition, memory to x is allocated by this statement.
  int x;

we can do both declare and define at same time

we need seperate declaration and definition in case of extern and functions.

u can refer: http://quiz.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-1/

Position:
Show:

Related questions

0 0 votes
1 1 answer
6.8k
6.8k views
cse23 asked Jun 16, 2016
6,777 views
Of the following expressions I A II A [3] III B IV B [3] which will not give compile-time errors if used as left hand sides of assignment statements in a C program? I, ...
9 9 votes
2 2 answers
355
355 views
GO Classes asked Jun 27
355 views
Which of the following correctly describes the declarations?int *a[10]; int (*p)[5];$\texttt{a}$ is a pointer to an array of $10$ integers, and $\texttt{p}$ is an array o...
7 7 votes
2 2 answers
240
240 views
GO Classes asked Jun 10
240 views
What is the output of the following code?#include <stdio.h int main() { extern int i; printf("%d", i); return 0; } int i = 100;Garbage value $\texttt{0}$ $\texttt{100}$ C...
7 7 votes
1 1 answer
227
227 views
GO Classes asked Jun 10
227 views
Is there any difference between the following declarations?extern int fun(); int fun();Both are identical No difference, except $\texttt{extern int fun();}$ is probably i...