Question about variables

what is the difference between the two declarations?

int ledState=0;

and

int ledState;

do they do the same thing?

easterly81: what is the difference between the two declarations?

int ledState=0;

and

int ledState;

do they do the same thing?

The first declares a variable and initializes its value to 0.

The second declares a variable. Its value will be whatever was last in that memory location. It might be 0, it might not be.

The second declares a variable. Its value will be whatever was last in that memory location

, unless it has global scope or is qualified "static", in which case, the compiler will thoughtfully set it to zero for you.

AWOL:

The second declares a variable. Its value will be whatever was last in that memory location

, unless it has global scope or is qualified "static", in which case, the compiler will thoughtfully set it to zero for you.

Actually, that's crt.o that does that, not the compiler :P

Actually, the two statements define the variable named ledState. In the first case, the rvalue of ledState is zero. In the second definition its rvalue is whatever random bit pattern happens to exist at the lvalue (i.e., memory address) given to ledState. Programmers seem to assume that declare and define mean the same thing. They don't. As pointed out by K&R:

It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (its type, size, etc.); a definition also causes storage to be allocated.

A data declaration forms an attribute list for the data item, which is often used for type checking. Function prototypes and externs are good examples of declarations. A data definition, on the other hand, also forms an attribute list, but also allocates memory (its lvalue) of where the value for that data item (its rvalue) get stored. Understanding the difference can make it much easier to understand many other programming topics.

econjack: Actually, the two statements define the variable named ledState. In the first case, the rvalue of ledState is zero. In the second definition its rvalue is whatever random bit pattern happens to exist at the lvalue (i.e., memory address) given to ledState. Programmers seem to assume that declare and define mean the same thing. They don't. As pointed out by K&R:

It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (its type, size, etc.); a definition also causes storage to be allocated.

A data declaration forms an attribute list for the data item, which is often used for type checking. Function prototypes and externs are good examples of declarations. A data definition, on the other hand, also forms an attribute list, but also allocates memory (its lvalue) of where the value for that data item (its rvalue) get stored. Understanding the difference can make it much easier to understand many other programming topics.

I think you'll find that relates to "external" variables - the difference between:

extern int value;

and

int value;

All variable definitions of the second type allocate storage space. The only ones that don't are extern (where you reference a variable in another file), and pointers.

majenko: I think you'll find that relates to "external" variables - the difference between:

I think you'll find that econjack's explanation was spot on, and you're just pointing out examples of declarations versus definitions.

PeterH:

majenko: I think you'll find that relates to "external" variables - the difference between:

I think you'll find that econjack's explanation was spot on, and you're just pointing out examples of declarations versus definitions.

So why does it specifically mention "the declaration of an external variable and its definition" then?

majenko: So why does it specifically mention "the declaration of an external variable and its definition" then?

This is the declaration of an external variable:

extern int value;

This is its definition:

int value;

The former declares some useful properties about 'value' - its an int. The latter also declares the same properties but crucially it allocates memory for it :)

You have to have a definition of a variable if you have declared an 'extern'. But you don't necessarily need a declaration if you have defined a variable, as defining it also declares it but at the same time also allocated memory.

Isn't it just wonderful how threads on this site have a tendancy to wander so far off topic?

The question was answered in the very first response. Since then we have wandered off into discussing the difference between externs / declarations and definitions.

Everything after the first reply is just arguing semantics, and as this is a multinational forum with pretty much every language on the globe represented, the subtle nuances of the difference between "definition" and "declaration" will be completely lost on a huge number of people here. As such you're just all serving to confuse the poor OP by going on about things that really don't concern the original question.

So, to easterly81: Read the first response, then completely ignore the rest of the thread as it doesn't have any bearing on the actual question. It's just the pedants getting their arses out again - something you'll see a lot of on this board.

...you're just all serving to confuse the poor OP by going on about things that really don't concern the original question.

While I stand guilty as charged, that was not my intent. Having taught university-level programming courses for over 30 years, I know how confused students get on some topics because they never truly understood the difference between define and declare. Concepts in other languages where you find the "new" keyword, or "by ref" or "by val" all are much more easily understood by someone who appreciates the difference between define and declare. The only reason for the word extern in C is so the definition of a data item can be made in one file but its attribute list can be declared and used in other, allowing the linker to do its job.

Personally, I've learned a lot by reading posts to many of the forums here, well past the first post that answered the question. In this particular case, I don't feel my define vs declare rant is that much of a disservice. To those for whom I wasted their time, I am sorry.

The question was answered in the very first response.

sp. "The question was partially answered in the very first response" , hence reply #2

AWOL:

The question was answered in the very first response.

sp. "The question was partially answered in the very first response" , hence reply #2

I reiterate my comment about the pedants :P

majenko: As such you're just all serving to confuse the poor OP by going on about things that really don't concern the original question.

majenko: I reiterate my comment about the pedants :P

And this from the person who was disputing whether global data initialisation is done by the compiler or the runtime environment ... :P

PeterH:

majenko: As such you're just all serving to confuse the poor OP by going on about things that really don't concern the original question.

majenko: I reiterate my comment about the pedants :P

And this from the person who was disputing whether global data initialisation is done by the compiler or the runtime environment ... :P

You see - I speak from a position of knowledge.

And anyway, that's not global data initialisation - that's BSS initialisation, because the value is 0 :P

In computer programming, the name .bss or bss is used by many compilers and linkers for a part of the data segment containing statically-allocated variables represented solely by zero-valued bits initially (i.e., when execution begins). It is often referred to as the "bss section" or "bss segment".

Some Olympic-standard willy-waving going on here