extern int VALUE vs int VALUE
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > extern int VALUE vs int VALUE




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    extern int VALUE vs int VALUE  
Chad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 12:50 PM

I thought the following two program snippets where doing the same
thing.  I think I'm wrong. On the second snippet, I get the following
error message: env.c:7: warning: unused variable `VALUE'.

#include <stdio.h>
#include <stdlib.h>

extern char **environ;
extern int VALUE;

int main(void) {
char *s = getenv("VALUE");

if(s == NULL)
exit(1);
else
printf("The value of a ho is: %s \n", s);
return 0;
}

$gcc -Wall env.c -o env
$export VALUE=88
$./env
The value of a ho is: 88
$

----------------------------------------------------------------------------
---
#include <stdio.h>
#include <stdlib.h>

extern char **environ;

int main(void) {
int VALUE = 0;
char *s = getenv("VALUE");

if(s == NULL)
exit(1);
else
printf("The value of a ho is: %s \n", s);
return 0;
}

$gcc -Wall env.c -o env
env.c: In function `main':
env.c:7: warning: unused variable `VALUE'
$export VALUE=88
$./env
The value of a ho is: 88
$

Can someone enlighten me as to what is going on?

Thanks in advance,
Chad






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 12:50 PM


Chad wrote:

You should probably direct your question to comp.lang.c/c++ as it's not
directly related to unixes.

> I thought the following two program snippets where doing the same
> thing.  I think I'm wrong. On the second snippet, I get the following
> error message: env.c:7: warning: unused variable `VALUE'.

The observable behaviour of the programs is indeed the same. The code,
however, is not.

[]

> Can someone enlighten me as to what is going on?

The compiler warns you that you have an unused variable. It might also
do that for the second snippet.






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Hubble


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 12:50 PM

>I thought the following two program snippets where doing the same
>thing.  I think I'm wrong. On the second snippet, I get the following
>error message: env.c:7: warning: unused variable `VALUE'.

>  extern int VALUE;
>  char * s=getenv("VALUE");

>vias

> int VALUE;
> char * s=getenv("VALUE");

Do not confuse environment variables with C program variables. In
either case, you do not use the variable VALUE at all. In the second,
the compiler can recognize this because VALUE is local to the text
file.

You have to do something like VALUE=atoi(s) to assign the desired value
from the environment getenv("VALUE");

Please do not write C-Variables in upper case. Upper case is reserved
for macros, so use "int value;" instead of "int VALUE;". Either one is
not connected to the environment.

In real code, be sure to check the return value of getenv is not NULL.

Hubble.






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
ilko.k.v@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 12:50 PM

One solution

#include<unistd.h>
#include<stdio.h>

int main()
{
unsigned  value;

sscanf(getenv("VALUE")? :"0","%d",&value);
printf("===> %d\n",value);

return 0;
}






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Chad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 12:50 PM


Hubble wrote: 
> 
> 
> 
>
> Do not confuse environment variables with C program variables. In
> either case, you do not use the variable VALUE at all. In the second,
> the compiler can recognize this because VALUE is local to the text
> file.
>
> You have to do something like VALUE=atoi(s) to assign the desired value
> from the environment getenv("VALUE");
>
> Please do not write C-Variables in upper case. Upper case is reserved
> for macros, so use "int value;" instead of "int VALUE;". Either one is
> not connected to the environment.
>
> In real code, be sure to check the return value of getenv is not NULL.
>
> Hubble.

I've also seen the used in other places than macros. Like the
following:
NAME
efence - Electric Fence Malloc Debugger

SYNOPSIS
#include <stdlib.h>

void * malloc (size_t size);

void free (void *ptr);

void * realloc (void *ptr, size_t size);

void * calloc (size_t nelem, size_t elsize);

void * memalign (size_t alignment, size_t size);

void * valloc (size_t size);

extern int EF_ALIGNMENT;

extern int EF_PROTECT_BELOW;

extern int EF_PROTECT_FREE;

extern int EF_ALLOW_MALLOC_0;

extern int EF_FILL;

I guess that is where I picked up the caps thingies.

Chad






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Hubble


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-05 10:57 PM

>I've also seen the used in other places than macros. Like the
>following:
>...
>#include <stdlib.h>
>...
>extern int EF_ALIGNMENT;
>...

Yes. These seem to have been macros once and are now replaced by
"extern int"s. They can be used like macros, and code formerly using
macros can safely use them instead, so a programmer won't notice the
difference.

However, to make your own code readable, you should follow the common
convenctions, and only deviate where appropriate. (Some of) These are
for the C language:
- macros usually in upper case
- variables in lower case
- procedure names lower case or mixed case

Sometimes, macros are replaced by variables and vice versa. A famous
example is the "errno" variable when using threads. It is then a macro
which dereferences the errno location for the current thread. Code
using "if (errno<0)" and such won't notice, but will continue to work
in a multithreaded program.

Hubble.






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Chad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-05 01:51 AM

So in other words, I could have something like this:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int VALUE = 0;

char *s = getenv("VALUE");

if(s == NULL)
exit(1);
else{
printf("The value of a ho is: %d \n", atoi(s));
}
return 0;
}

And for whatever reasons, I don't want the user to know about the
variable 'VALUE'. However, this value still can be modified. Because
when I go something like:

$export VALUE=90
$./env
The value of a ho is: 90

Is there anykind of safeguard against this kind of scenario?

Chad






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Erik Max Francis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-05 01:51 AM

Chad wrote:

> And for whatever reasons, I don't want the user to know about the
> variable 'VALUE'. However, this value still can be modified. Because
> when I go something like:
>
> $export VALUE=90
> $./env
> The value of a ho is: 90
>
> Is there anykind of safeguard against this kind of scenario?

The variable VALUE is not used anywhere.  You need to be clearer about
what it is you want to achieve, because so far what you're doing has no
effect on your program's behavior whatsoever (outside of harmless
compiler warnings).

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I am become death, the destroyer of worlds.
-- J. Robert Oppenheimer (quoting Hindu legend)





[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Chad


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-05 01:51 AM

Erik Max Francis wrote:
> Chad wrote:
> 
>
> The variable VALUE is not used anywhere.  You need to be clearer about
> what it is you want to achieve, because so far what you're doing has no
> effect on your program's behavior whatsoever (outside of harmless
> compiler warnings).
>
> --
> Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
> San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
>    I am become death, the destroyer of worlds.
>    -- J. Robert Oppenheimer (quoting Hindu legend)

Okay, maybe I'm missing this. If doing something like
$export VALUE=90
has no effects on the programs behavior, then how come the program I
ran will print the value of 'VALUE' set at the bash shell?

Chad






[ Post a follow-up to this message ]



    Re: extern int VALUE vs int VALUE  
Erik Max Francis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-05 07:50 AM

Chad wrote:

> Okay, maybe I'm missing this. If doing something like
> $export VALUE=90
> has no effects on the programs behavior, then how come the program I
> ran will print the value of 'VALUE' set at the bash shell?

Your declaration/definition of a variable called VALUE inside the
program is what is having no effect.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
But when I reached the finished line / Young black male
-- Ice Cube





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:38 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register