Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the C compiler do with different types of declarations?

Tags:

c

declaration

I understand this:

int i = 3;  // declaration with definition

It tells the compiler to:

  1. Reserve space in memory to hold integer value.
  2. Associate name with memory location.
  3. Store the value 3 at this location.

But what does this declaration tell the compiler:

int i;  // declaration
like image 881
user784276 Avatar asked Jun 04 '11 21:06

user784276


People also ask

How do C++ compilers work?

How compilers work. Compilers are utility programs that take your code and transform it into executable machine code files. When you run a compiler on your code, first, the preprocessor reads the source code (the C++ file you just wrote). The preprocessor searches for any preprocessor directives (lines of code starting with a #).

What are types and declarations in compiler design?

In this article, we will discuss Types and Declarations in compiler design. Typical basic types and declarations for a language contain boolean, char, integer, float, and void; here, void denotes "the absence of a value." A type name is a type expression.

What are the different types of compilers?

Ask New Question. The main types of computer compilers are single pass compilers, multi pass compilers, cross compilers and optimizing compilers. A compiler takes one computer language, called a source code, and converts it into the target language. It enables a computer to be able to read different source codes.

Which compiler should I use for my code?

2.If you are running your code in Linux system use gcc. The main types of computer compilers are single pass compilers, multi pass compilers, cross compilers and optimizing compilers. A compiler takes one computer language, called a source code, and converts it into the target language.


2 Answers

The declaration tells the compiler to reserve space for the variable i and associate the name i with that space (your points 1. and 2.).

If i is a global variable it is initialized to 0.

If it is local the value of i is undefined (probably garbage, ie. some random value) and you should assign to it before reading it.

like image 157
sverre Avatar answered Nov 15 '22 10:11

sverre


There are two cases: at file scope (i.e. for a global declaration), and in a function.

In a function, the declaration int i; does two things: it declares a variable called i whose type is int, and it reserves some storage in memory to put a value of type int. What it does not do is give the variable a value. The storage used by i will still contain whatever garbage was there before. You need to initialize the variable, i.e. assign a value to it, before you can read a value from it. Good compilers will warn you if you don't initialize the variable.

At file scope, int i also declares a variable called i. The rest depends on other things: this is known as a tentative definition. You can have multiple such declarations in your file. At most one of these is allowed to have an initializer, making it a full-fleged definition. If none of the declarations of i at file scope have an initializer, the declaration is also a definition, and there is an implicit initialization to 0. Thus:

int i;
/* ... more code ...*/
int i;

is valid, and i will be initialized to 0 (assuming these are the only declarations of i at file scope). Whereas:

int i;
int i = 3;

is also valid, and i will be initialized to 3 when the program starts.

In practice, at file scope, there's often a difference between leaving the initialization implicit and explicitly initializing to 0. Many compilers will store an explicit 0 in the binary, but let the operating system initialize implicit zeroes automatically when the program is loaded. Don't worry about this unless you have a large global array (which shouldn't happen often) or you work on tiny embedded systems.

like image 23
Gilles 'SO- stop being evil' Avatar answered Nov 15 '22 10:11

Gilles 'SO- stop being evil'