Help: initialization (Newbie Question) 
Author Message
 Help: initialization (Newbie Question)

# Please help me with the following stupid question:

That's not a stupid question. Stupid questions are those in the FAQ.

# Consider a code:

# #define <stdio.h>

# int main(){
#   int a=33;
#   int b=a;
#   printf("%d\n",b);
# }

# With the compiler I am using, the code will print "33". However,  I am interested to know if that is true for all ansi compilers, i.e., does the ansi standard requires
# that b has to be 33 in this case?

Yes. ANSI, or ISO these days, allows you to initialize automatic
variables using expressions. However, static variables and those
with file scope (outside any function definition) can only
be initialized with constants. So

int rnd = rand ();         /* wrong, file scope */

void foo (void)
{
  int *p = malloc (42);    /* okay */
  static int *q = p;       /* wrong, static */

Quote:
}

Regards,

        Jens
--
Jens Schweikhardt   http://www.*-*-*.com/
SIGSIG -- signature too long (core dumped)



Fri, 27 Apr 2001 03:00:00 GMT  
 Help: initialization (Newbie Question)


Quote:
>Please help me with the following stupid question:

>Consider a code:

>#define <stdio.h>

>int main(){
>  int a=33;
>  int b=a;
>  printf("%d\n",b);
>}

>With the compiler I am using, the code will print "33". However,  I am
> interested to know if that is true for all ansi compilers, i.e., does the ansi
> standard requires
>that b has to be 33 in this case?

a is initialised to 33 and b is initialised to the value of a which is 33.
What other possibility are you thinking might occur? The only issue is whether
this code is vaslid or not. It is, you can initialse an automatic variable
such as b with a general expression (as long as b is a scalar type, so not
a structure, union or array).

--
-----------------------------------------


-----------------------------------------



Fri, 27 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. newbie's base struct member initialization question

2. NEWBIE: Initialization of member variables

3. Newbie: static const member initialization

4. Newbie question: Need help please!

5. getchar() : newbie question, pls HELP

6. Newbie Question, please help with searching

7. Newbie questions - Help!

8. Need help in C- newbie question

9. Please Help! --newbie question, very simplistic stuff--

10. Newbie question: Need help please!

11. Random Variable help! (newbie question)

12. Newbie question: Need help finding a particular C keyword

 

 
Powered by phpBB® Forum Software