static storage class and variable initialization

A

arnuld

Here is the program and i have some observations about the use of Static
storage class specifier. Please tell me whether I am right or wrong:


#include <stdio.h>


#define DBLVAL 1.001

int main(void)
{
int i = 0;
char c = 'c';

static char ch;
static int x = i;
static double d = DBLVAL;

ch = c;

printf("static int x = %d\n", x);
printf("static char ch = %c\n", ch);
printf("static doubler d = %f\n", d);
return 0;
}

==================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra static.c
static.c: In function ‘main’:
static.c:14: error: initializer element is not constant
[arnuld@dune programs]$




OBSERVATION 1: If you going to initialize a variable declared with static
specifier then you can only pass compile-time constant values, else you
can't compile the program.

OBSERVATION 2: If you want to pass a variable (which is not a compile-
time constant) to a variable declared/defined with static specifier then
you can do that only in assignment.
 
B

Barry Schwarz

Here is the program and i have some observations about the use of Static
storage class specifier. Please tell me whether I am right or wrong:


#include <stdio.h>


#define DBLVAL 1.001

int main(void)
{
int i = 0;
char c = 'c';

static char ch;
static int x = i;
static double d = DBLVAL;

ch = c;

printf("static int x = %d\n", x);
printf("static char ch = %c\n", ch);
printf("static doubler d = %f\n", d);
return 0;
}

==================== OUTPUT =========================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra static.c
static.c: In function ‘main’:
static.c:14: error: initializer element is not constant
[arnuld@dune programs]$

If you are going to include these types of messages, add something to
tell us where line 14 is. Unless the blank line before the #include
is part of your source, line 14 is the blank line after the definition
of d.
 
P

Phil Carmody

Barry Schwarz said:

Well, i isn't constant.
If you are going to include these types of messages, add something to
tell us where line 14 is. Unless the blank line before the #include
is part of your source, line 14 is the blank line after the definition
of d.

Guesswork's part of the fun!

Phil
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top