I can´t understand C variables.....

B

Bilbo

Hi, I am a long time matlab user, now I am learning C but can´t understand
the following:

1)If I define a variable, without giveing a value to it, the compiler
gives any value to it.
2) when I use rand(), the same number is given on each compilation.

What´s wrong here??

Thanks

Derek
 
J

Joona I Palaste

Bilbo said:
Hi, I am a long time matlab user, now I am learning C but can´t understand
the following:
1)If I define a variable, without giveing a value to it, the compiler
gives any value to it.
2) when I use rand(), the same number is given on each compilation.
What´s wrong here??

Look up the srand() function.
 
L

Leor Zolman

Hi, I am a long time matlab user, now I am learning C but can´t understand
the following:

1)If I define a variable, without giveing a value to it, the compiler
gives any value to it.

Looks like Joona missed that first one [it is not likely he does not know
the answer ;-) ]

There are different "storage classes" for variables in C, and the storage
class determines whether or not a variable is initialized with a default
value (if you don't use an initializer).

Variables with "static" storage, that is, having a fixed location in memory
(for example: file scope [ a.k.a. "global", or "external"] data, and data
declared "static" at block scope) get zero-initialized. "Zero" is
appropriate for the object's type (as opposed to necessarily meaning all
0-bits.)

Automatic storage (non-static data defined at block scope, otherwise known
as garden-variety local data) and some dynamic storage (such as that
obtained from malloc()) is not initialized; it contains garbage, or
whatever was there before. In the case of floating-point data and pointers,
you really want to make sure that the very first thing you do with such
uninitialized storage is assign a value to it. Doing so as a bona-fide
initialization at the point of the object's definition is best (for
pointers, it is generally considered safer to set them to NULL rather than
leaving them containing garbage, but this is a Religious Issue). A plain
assignment later on is also ok, so long as you don't forget to do it....
because reading an uninitialized floating point or pointer value invokes
undefined behavior.
-leor
 
L

Leor Zolman

Thanks, I apreciate your help!!

There's at least one rather special case regarding initialization of
automatics that I failed to mention: if you provide any initializers /at
all/ for an explicitly-sized automatic array, then all elements left
uninitialized will be zero-initialized (in the same sense as mentioned
above). Thus in the following scenario:
int a[5] = {0};
all five elements will be initialized to 0 upon entry to the enclosing
block. You can also say this:
int a[5] = {};
to produce the same result.
-leor
 
D

Dan Pop

In said:
There's at least one rather special case regarding initialization of
automatics that I failed to mention: if you provide any initializers /at
all/ for an explicitly-sized automatic array, then all elements left
uninitialized will be zero-initialized (in the same sense as mentioned
above). Thus in the following scenario:
int a[5] = {0};
all five elements will be initialized to 0 upon entry to the enclosing
block. You can also say this:
int a[5] = {}; ^^^^^^^^^^^^^^
to produce the same result.

Can you?

fangorn:~/tmp 189> gcc -ansi -pedantic test.c
test.c: In function `main':
test.c:3: warning: ISO C forbids empty initializer braces

Dan
 
L

Leor Zolman

In said:
There's at least one rather special case regarding initialization of
automatics that I failed to mention: if you provide any initializers /at
all/ for an explicitly-sized automatic array, then all elements left
uninitialized will be zero-initialized (in the same sense as mentioned
above). Thus in the following scenario:
int a[5] = {0};
all five elements will be initialized to 0 upon entry to the enclosing
block. You can also say this:
int a[5] = {}; ^^^^^^^^^^^^^^
to produce the same result.

Can you?

fangorn:~/tmp 189> gcc -ansi -pedantic test.c
test.c: In function `main':
test.c:3: warning: ISO C forbids empty initializer braces

Dan

Oh my! Sorry. To be honest, I saw someone use that form of array
initialization once, and I was aghast. I believe I even commented upon it
in a post. Then I tried it, and it worked (I know, I know, and I can't even
figure out now what platform I was testing on...). I looked it up in the
Standard then, and it seemed to be valid. I even looked it up again this
morning before posting the above...but I didn't test again, which was my
biggest mistake.

Now I'm seeing how I misunderstood the Standard. Section 6.7.8/19 says "all
subobjects that are not initialized explicitly shall be initialized
implicitly the same as objects that have static storage duration." I took
the "all" to mean "up to every single one of them".

But taking a closer look at paragraph 16, "Otherwise, the initializer for
an object that has aggregate or union type shall be a brace enclosed
list of initializers for the elements or named members.", it doesn't say
the list is optional.

Plus, of course, all the compilers I respect most reject the empty list
completely. Blush.

But I'm happy it is illegal ;-)
Thanks,
-leor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top