This may seem simple to one's eye but, this question is itching me in many ways.
my question is about declaration and defenition on variables in c.
there are actually many explanation in internet regarding this one and there is not just one solution to this issue as many view points are placed in this issue. i want to know the clear existance of this issue.
int a;
just take this is this a declaration or definition?, this one when i use printf
, it has 0
as value and address as 2335860
. but if this declaration then how come memory is allocated for this.
int a;
int a;
when i do this it says previous declaration of 'a' was here and redeclaration of 'a' with no linkage.
some sources say redeclaration is permitted in c and some say dont what is the truth?
int a;
if written in global scope is a tentative definition. Which means if no other definitions are available in current compilation unit, treat this as definition or else this is a declaration.From 6.9.2 External object definitions in C11 specs:
So you are effectively doing multiple declarations but getting the address and value because of tentative definition rule.
Redeclaration is permitted in
C
. But redefinition is not.Related question: What is the difference between a definition and a declaration?
Prefer a good book instead of internet to get the hold of language. You can choose a good book from: The Definitive C Book Guide and List