473,698 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C predefined macros and portability


friends,
Please go through the following code which I have downloaded from Bob
Stout (Snippets):

#include <stdio.h>

void main(void) /* Actually, void main() is non-ANSI/ISO */

{

int version;

#if defined(__ZTC__ )

#ifdef __SC__

printf("Symante c C++ ver. %x.%x\n", __SC__ >> 8, __SC__ & 0xff);

#else

printf("Zortech C++ ver. %x.%xr%x\n",

__ZTC__ >> 8, (__ZTC__ >> 4) & 0xf, __ZTC__ & 0xf);

#endif

#elif defined(__WATCO MC__)

printf("Watcom C/C++ ver. %d.%d\n",

__WATCOMC__ / 100, __WATCOMC__ % 100);

#elif defined(__TURBO C__)

version = __TURBOC__;

if (0x295 > version)

{

printf("Borland Turbo C ver. %x.%02x\n",

version >> 8, version & 0xff);

}

else if (0x400 <= version)

{

printf("Borland C++ ver. %x.%x\n",

(version >> 8) - 1, (version & 0xff) >> 4);

}

else if (0x297 > version)

printf("Borland Turbo C++ ver. 1.%02x\n", version - 0x295);

else printf("Borland C++ ver. 2.%02x\n", version - 0x297);

#elif defined(_QC)

printf("Microso ft Quick C ver. %d.%d\n", _QC / 100, _QC % 100);

#elif defined(_MSC_VE R)

printf("Microso ft C(/C++) ver. %d.%d\n",

_MSC_VER / 100, _MSC_VER % 100);

#elif defined(__POWER C)

printf ("MIX Power C ver. %d\n", __POWERC);

#else

puts("Unknown compiler!");

#endif

}
I have downloaded the above program which prints the specified compiler
by which the code is been compiled. I have tested with MSVC and TC++, GCC.
The out-put was exactly same as excepted. It throws "Unknown compiler!"
for GCC. Even though the following code did use some of the non- ANSI
standard
specific can anybody explain the following:
1)__ZTC__ , __SC__ etc. are predefined macros (correct me if I am
wrong).
Can any body explain how this work during the compile / run time?
(I am
not sure weather this evaluated during the compile or run time)

2) Why there are some of the operators are used in the printf. Can
any body
explain why?

3) Is this the way the C code becomes portable. If yes then is it
possible
to make a C code portable with different OS, processors.

4)I want to know more about the above. Any link on the web

If the above is discussed in FAQ or it is a OT please guide me to the
correct
NG.

Thanks

--
"Combinatio n is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)

Nov 14 '05 #1
5 4148
sathya_me <sa*******@noma il.com> wrote:
Please go through the following code which I have downloaded from Bob
Stout (Snippets): #include <stdio.h> void main(void) /* Actually, void main() is non-ANSI/ISO */
{
int version;
#if defined(__ZTC__ )
#ifdef __SC__
printf("Symante c C++ ver. %x.%x\n", __SC__ >> 8, __SC__ & 0xff);
#else
printf("Zortech C++ ver. %x.%xr%x\n",
__ZTC__ >> 8, (__ZTC__ >> 4) & 0xf, __ZTC__ & 0xf);
#endif
#elif defined(__WATCO MC__)
printf("Watcom C/C++ ver. %d.%d\n",
__WATCOMC__ / 100, __WATCOMC__ % 100);
#elif defined(__TURBO C__)
version = __TURBOC__;
if (0x295 > version)
{
printf("Borland Turbo C ver. %x.%02x\n",
version >> 8, version & 0xff);
}
else if (0x400 <= version)
{
printf("Borland C++ ver. %x.%x\n",
(version >> 8) - 1, (version & 0xff) >> 4);
}
else if (0x297 > version)
printf("Borland Turbo C++ ver. 1.%02x\n", version - 0x295);
else printf("Borland C++ ver. 2.%02x\n", version - 0x297);
#elif defined(_QC)
printf("Microso ft Quick C ver. %d.%d\n", _QC / 100, _QC % 100);
#elif defined(_MSC_VE R)
printf("Microso ft C(/C++) ver. %d.%d\n",
_MSC_VER / 100, _MSC_VER % 100);
#elif defined(__POWER C)
printf ("MIX Power C ver. %d\n", __POWERC);
#else
puts("Unknown compiler!");
#endif
} I have downloaded the above program which prints the specified compiler
by which the code is been compiled. I have tested with MSVC and TC++, GCC.
The out-put was exactly same as excepted. It throws "Unknown compiler!"
for GCC. Even though the following code did use some of the non- ANSI
standard specific can anybody explain the following:
1)__ZTC__ , __SC__ etc. are predefined macros (correct me if I am
wrong).
Can any body explain how this work during the compile / run time?
(I am not sure weather this evaluated during the compile or run time)
All the '#ifdef', '#elif', '#else' and '#endif' stuff is dealt with
by the preprocessor. Obviously for example the Watcom compiler defines
a macro named '__WATCOMC__' (and the whole thing only works if no
other of the compilers defines it!). In that case the lines between
the '#elif defined(__WATCO MC__)' and the '#elif defined(__TURBO C__)'
line are the ones that get included into the source code the compiler
is going to see, and if all the other macros are undefined that are
the only lines that will make it into the code.
2) Why there are some of the operators are used in the printf. Can
any body explain why?
For example '__WATCOMC__' seems to be defined as a number that stands
for the compiler version where e.g. 821 would stand for main version
8, subversion 21. All what happens in the second and third argument
to printf() is picking that number apart to get the 8 and the 21. The
same seems to happen (with variations) for the other compilers.
3) Is this the way the C code becomes portable. If yes then is it
possible to make a C code portable with different OS, processors.


All this doesn't got much to do with portability. It just is an
attempt to figure out which compiler is used to compile the
program. For portable C code you _don't_ need that information
at all - if you need to know that you're typically already way
down the slippery road to non-portability.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2


Je***********@p hysik.fu-berlin.de wrote:
sathya_me <sa*******@noma il.com> wrote:

Please go through the following code which I have downloaded from Bob
Stout (Snippets):

#include <stdio.h>

void main(void) /* Actually, void main() is non-ANSI/ISO */
{
int version;
#if defined(__ZTC__ )
#ifdef __SC__
printf("Symante c C++ ver. %x.%x\n", __SC__ >> 8, __SC__ & 0xff);
#else
printf("Zortech C++ ver. %x.%xr%x\n",
__ZTC__ >> 8, (__ZTC__ >> 4) & 0xf, __ZTC__ & 0xf);
#endif
#elif defined(__WATCO MC__)
printf("Watcom C/C++ ver. %d.%d\n",
__WATCOMC__ / 100, __WATCOMC__ % 100);
#elif defined(__TURBO C__)
version = __TURBOC__;
if (0x295 > version)
{
printf("Borland Turbo C ver. %x.%02x\n",
version >> 8, version & 0xff);
}
else if (0x400 <= version)
{
printf("Borland C++ ver. %x.%x\n",
(version >> 8) - 1, (version & 0xff) >> 4);
}
else if (0x297 > version)
printf("Borland Turbo C++ ver. 1.%02x\n", version - 0x295);
else printf("Borland C++ ver. 2.%02x\n", version - 0x297);
#elif defined(_QC)
printf("Microso ft Quick C ver. %d.%d\n", _QC / 100, _QC % 100);
#elif defined(_MSC_VE R)
printf("Microso ft C(/C++) ver. %d.%d\n",
_MSC_VER / 100, _MSC_VER % 100);
#elif defined(__POWER C)
printf ("MIX Power C ver. %d\n", __POWERC);
#else
puts("Unknown compiler!");
#endif
}

I have downloaded the above program which prints the specified compiler
by which the code is been compiled. I have tested with MSVC and TC++, GCC.
The out-put was exactly same as excepted. It throws "Unknown compiler!"
for GCC. Even though the following code did use some of the non- ANSI
standard specific can anybody explain the following:
1)__ZTC__ , __SC__ etc. are predefined macros (correct me if I am
wrong).
Can any body explain how this work during the compile / run time?
(I am not sure weather this evaluated during the compile or run time)


All the '#ifdef', '#elif', '#else' and '#endif' stuff is dealt with
by the preprocessor. Obviously for example the Watcom compiler defines
a macro named '__WATCOMC__' (and the whole thing only works if no
other of the compilers defines it!). In that case the lines between
the '#elif defined(__WATCO MC__)' and the '#elif defined(__TURBO C__)'
line are the ones that get included into the source code the compiler
is going to see, and if all the other macros are undefined that are
the only lines that will make it into the code.


The preprocessors check the *conditions* inside the top most #if and
#endif block.
If the preprocessor finds the specific condition it prints the message
and exits.
If not then continues. So it happens not during the run time but during
preprocessor time.
(Basic preprocessor evaluation, which I have over looked, I must be more
careful)


2) Why there are some of the operators are used in the printf. Can
any body explain why?
For example '__WATCOMC__' seems to be defined as a number that stands
for the compiler version where e.g. 821 would stand for main version
8, subversion 21. All what happens in the second and third argument
to printf() is picking that number apart to get the 8 and the 21. The
same seems to happen (with variations) for the other compilers.


Clear and understood.

3) Is this the way the C code becomes portable. If yes then is it
possible to make a C code portable with different OS, processors.


All this doesn't got much to do with portability. It just is an
attempt to figure out which compiler is used to compile the
program. For portable C code you _don't_ need that information
at all - if you need to know that you're typically already way
down the slippery road to non-portability.

Asking more of this will make the thread an OT. So concluding.

Thanks
N.Sathyashrayan

--
"Combinatio n is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)

Nov 14 '05 #3
sathya_me <sa*******@noma il.com> wrote:

Je***********@p hysik.fu-berlin.de wrote:
All the '#ifdef', '#elif', '#else' and '#endif' stuff is dealt with
by the preprocessor. Obviously for example the Watcom compiler defines
a macro named '__WATCOMC__' (and the whole thing only works if no
other of the compilers defines it!). In that case the lines between
the '#elif defined(__WATCO MC__)' and the '#elif defined(__TURBO C__)'
line are the ones that get included into the source code the compiler
is going to see, and if all the other macros are undefined that are
the only lines that will make it into the code.
The preprocessors check the *conditions* inside the top most #if and
#endif block.
If the preprocessor finds the specific condition it prints the message
and exits.
If not then continues. So it happens not during the run time but during
preprocessor time.
(Basic preprocessor evaluation, which I have over looked, I must be more
careful)


No, the preprocessor doesn't print anything, that only happens when
you run the final program. It just removes the parts of the program
for which the macros aren't defined and in what remains it expands
the macros.
2) Why there are some of the operators are used in the printf. Can
any body explain why?


For example '__WATCOMC__' seems to be defined as a number that stands
for the compiler version where e.g. 821 would stand for main version
8, subversion 21. All what happens in the second and third argument
to printf() is picking that number apart to get the 8 and the 21. The
same seems to happen (with variations) for the other compilers.


In the case of only '__WATCOMC__' being defined and it's being 821
you end up with a program that will basically look similar to

void main(void)
{
printf("Watcom C/C++ ver. %d.%d\n",
821 / 100, 821 % 100);
}

This is what then gets compiled and will produce the output
"Watcom C/C++ ver. 8.21" when run.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
# 1)__ZTC__ , __SC__ etc. are predefined macros (correct me if I am
# wrong).
# Can any body explain how this work during the compile / run time?
# (I am
# not sure weather this evaluated during the compile or run time)

Individual compilers often make nonstandard #defines to identify the
compiler to the compiled program. The programs can let the preprocessor
select different versions of the source code that exploit compiler
specific extensions to C, or in some cases a version that patches around
a compiler bug.

# 2) Why there are some of the operators are used in the printf. Can
# any body
# explain why?

Compiler specific. Those compilers probably set their special #define
to encoded version number; the arithmetic is then to extract that
version number.

# 3) Is this the way the C code becomes portable. If yes then is it
# possible
# to make a C code portable with different OS, processors.

All standard conforming compilers should compile the same standard
conforming program. These are to identify nonstandard extensions
and compiler bugs.

# 4)I want to know more about the above. Any link on the web

I doubt there is any central directory. You will probably have to
look up the documentation on each compiler separately. Then again,
if you are going to use these, it should be to exploit the compiler
extensions, so you'll have to read the documentation anyway.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no respect for people with no shopping agenda.
Nov 14 '05 #5
In article <2o************ @uni-berlin.de>,
sathya_me <sa*******@noma il.com> wrote:
If the preprocessor finds the specific condition it prints the message
and exits.


There is nothing in the given source code that makes the "preprocess or"
print anything or makes the "preprocess or" exit.

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #6

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

Similar topics

25
9123
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
6
7179
by: cody | last post by:
where can i find a list of all predefined preprocessor macros in c#? i searched in the msdn but i can't find it. i now there is at lest one: DEBUG. but what else? -- cody www.deutronium.de.vu || www.deutronium.tk
6
2713
by: J.G.Harston | last post by:
I've been trying to track down explicit information about this for some time. I know that the ANSI C standard specifies the __* and _* macro namespace as reserved for the compiler implementation, and playing with a handful of compilers I can find what those specific compilers predefine, but is there any information out there on how that reserved namespace should be used? If I am writing some portable code what predefined macros should...
3
1791
by: sathyashrayan | last post by:
Friends, Can any body suggest any link to a tutorial, which can cover various concepts of using macro in a program? I have seen macros used to over come the endiness problems, portability issues, generality etc. But I don't understand how it is been done. Thanks
6
19863
by: Tom Torfs | last post by:
Hello All, I've been missing the lack of support for binary numeric literals in C. To get around it I wrote the following handy macros, which allows you to simply write something like: whatever = B8(10101010); and will translate as:
35
2527
by: Durgesh Sharma | last post by:
I want to use strrchar(source_string,last_char ) function from string.h header file,to find out the last occurrence of the NON SPACE Alphanumeric Character. Then i will put a NULL CHAR after incrementing that position,received by strrchar() function by 1. This is my idea of Trimming a string from right . According to the definition of the strrchar function,i am supposed to
5
12837
by: ampeloso | last post by:
Hello, I would like to allocte memory, but I want it to start at a predefined address. I have a program that writes data to ROM in an embedded device and I would like to state where it goes. Can this be done and how? Thanks Mike
33
8885
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
3
2634
by: travish | last post by:
Is the stringize operator for macros commonly/correctly implemented on compilers advertising ANSI C compatibility? Example: #define STRINGIZE(ARG) ARG = #ARG void main() { char *test;
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.