Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is Initialising int's on OS X different to Unix??

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi, I'm a newbie C programmer, Iv been doing some small exercises using the gcc compiler on OS X, and am a-bit confused.

In C I was told when you initialise an int, but do not assign it a value
(int i;) it would assume an arbitary value.
However on my Mac int i; will assign i the value 0. More like Java.

Could anyone clarify this for me? Are int's supposed to assume an arbitrary value?

Thankyou
Oxy

we are all of us living in the gutter.
But some of us are looking at the stars.
 
> Are int's supposed to assume an arbitrary value?
Zero is just as good an arbitrary value as any other. It may be quite likely that it will be zero the first time you call a function in a really simple program, but generally speaking in a large program, most uninitialised variables will contain something pretty random.

The value of i will likely depend on whether you call foo() first or second.
Code:
#include <stdio.h>

void foo ( ) {
    char msg[] = &quot;hello world\n&quot;;
    printf(&quot;%s&quot;,msg);
}
void bar ( ) {
    int i;
    printf(&quot;%d\n&quot;, i );
}
int main ( int argc, char *argv[] ) {
    if ( argc > 1 ) {
        foo();
        bar();
    } else {
        bar();
        foo();
    }
    return 0;
}

--
 
hi Oxymoron,

in C language, there are more than one storage class of vars
(for storage class is not intended char,intfloat,ecc)

the most important are

global, static, automatic

(only static verb you meet in program, the other are implicit)

exists other, as register or other but they depends from Machine type and OS.


int total; // global one

void print()
{
static int ntime_called ; // static
ntime_called++ ;
printf( &quot;%d %d\n&quot;, total, ntime_called ) ;
}

void load()
{
total = 10 ;
}

main()
{
int i ; // automatic
load();
for( i=0 ; i<3 ; i++ )
print();
}

A global is a var declared outside a routine: these vars
are loaded with 0 before program runs.

Automatic are declared inside ruotine, and they have life
from the begin of routine utils its end: after they come forgotten (the stotage area is the stack and it is lost when
the routine exist). The begin value is unpredictable, and it
has to be initialized

Static are a mix of them: we need them inside a routine and their scope does not outbound the routine, but, they are not stack-stored and their initial value is 0. Furthemore when you go back in a routine already called, the value of a static var is maintained, and it may be useful for counter or other. (another typical use is when you perform a return of a pointer)

char* String()
{
static char buffer[10];
...
return buffer;
}

The use of static verb outside rotine is allowed, but not useful for vars. Instead may be very important before a rotine name:

static void Total()
{
}

This tells to compiler and linker that the scope of rtn does not outbound module (you can have more than 1 Total bodies of routine in different files with different body in each module ( a module typically is a file)
The Total() rtn that stays in file p1.c is different from that is in p2.c, but they can have the same name, is at a name you give a meaning.
You can call them p1_Total() and p2_Total(), without declare them static and dont receive a linker message:
Total() has already a body, but is easier call them with the same name (if it has a sense) and prefix them with static verb.

ciao,
Vittorio
 
Victorv,
static vars have file (module) scope, it's very useful to keep a module (not only function) state in static vars outside function bodies (e.g. a module is a family of related static functions with some global functions as module interface). It's some C surrogate of C++ class - very useful, safety and effective approach...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top