473,508 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding using env variable

Hi

I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.

Thanks

Mohan
Nov 14 '05 #1
6 2113
In article <d1**********@ns2.fe.internet.bosch.com>,
invincible <mo**********@in.bosch.com> wrote:
I have program void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
} This one I can achieve using -dLinux while compiling it on linux But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.


No. The C standard does not require the consultation of any environment
variables at compile time.

That having been said: if you tell your compiler to show all the phases
as it compiles, you will often find that each different environment
automatically -D's different preprocessor tokens. For example, on
the machine I am on now, in the default compile mode I used,

-Dmips -DMIPSEB -D_MIPSEB -D_PIC -D__DSO__ -D__EXTENSIONS__ \
-D__INLINE_INTRINSICS -D__MATH_HAS_NO_SIDE_EFFECTS -DLANGUAGE_C -Dunix \
-Dsgi -Dhost_mips -D_SGI_SOURCE -D_LONGLONG -D_SVR4_SOURCE \
-D_LANGUAGE_C -D_MODERN_C -D__sgi -D__host_mips -D_SYSTYPE_SVR4 \
-D__unix -D_COMPILER_VERSION=730 -D__mips=3 -D_MIPS_ISA=3 -D_ABI64=3 \
-D_MIPS_SIM=_ABI64 -D_MIPS_FPSET=32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 \
-D_MIPS_SZPTR=64 -D_SIZE_INT=32 -D_SIZE_LONG=64 -D_SIZE_PTR=64

The exact set of tokens will be dependant on the system you are on,
on the compiler you are using, on the compiler version, and upon
compilation flags.

Note, by the way, that a preprocessor token definition is NOT an
environment variable.
--
Feep if you love VT-52's.
Nov 14 '05 #2

invincible wrote:
Hi

I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var which i can use for my required task.

Thanks

Mohan


I am not sure about a flag available to the compiler, but it can be
done during runtime.

#include <string.h>
#include <cstdlib.h.
char *ostype;
ostype = getenv("OSTYPE");
if (strcmp(ostype,"linux")==0) printf("I am in Linux");


Cheers,
Bill C.

Nov 14 '05 #3
invincible <mo**********@in.bosch.com> spoke thus:
void main()


http://www.eskimo.com/~scs/C-faq/q11.12.html
http://www.eskimo.com/~scs/C-faq/q11.14.html
http://www.eskimo.com/~scs/C-faq/q11.15.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
invincible wrote:

I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.


Who knows? This is not a C question. Ask in groups covering your
particular systems.

At any rate, the above program has un (or implementation) defined
behavior. void main is only allowed to adherents of BullSchildt
books.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5
"Bill C" <wc********@aol.com> wrote:
invincible wrote:
I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.
I am not sure about a flag available to the compiler, but it can be
done during runtime.

#include <string.h>
#include <cstdlib.h.
char *ostype;
ostype = getenv("OSTYPE");
if (strcmp(ostype,"linux")==0) printf("I am in Linux");


That may work for Linux, but it's hardly a general solution. In fact, it
causes undefined behaviour on my machine.
The usual solution is to do what invincible says he doesn't want to do:
have the makefile (which will probably need to be somewhat different for
most targets anyway) define a constant.

Richard
Nov 14 '05 #6

Richard Bos wrote:
That may work for Linux, but it's hardly a general solution. In fact, it causes undefined behaviour on my machine.
The usual solution is to do what invincible says he doesn't want to do: have the makefile (which will probably need to be somewhat different for most targets anyway) define a constant.


You are correct, my solution isn't general. In fact, I wasn't trying
to be general. The OP asked for a solution on 2 specific platforms. I
showed him one way to confirm that he is running on one of those 2. I
probably should have checked it out on Solaris to make sure it wouldn't
crash, but I figured he could do that much himself.

Your point is well taken, and I should have noted that this is a
potential, specific, solution to his specific problem.

Cheers,
Bill C.

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2443
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
2
1614
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
4
1894
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
9
1119
by: sam_cit | last post by:
Hi everyone, I have the following piece of code, #include <stdio.h> #include "sample1.h" int x = 10; namespace first
10
1878
by: sam_cit | last post by:
Hi Everyone, I had a doubt regarding extern decleration, i tried this is one source file, extern int sample; extern int sample; int main() {
4
2765
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these...
4
2291
by: Benny Van | last post by:
Hi all! I have a question regarding a windows operating system function: I was asked to write a small program for a homework to display the user name and computer name and the system time out to a...
1
1163
by: Froefel | last post by:
Hi group, I've been developing a number of classes, many of which have a similar structure, as follows: namespace { public class classname { Private Fields
2
1799
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
5
2220
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.