According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
Why then do most of my compilers barf on this?
#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
They barf on the "#if sizeof ..." line.
They explicitly say that I can't use sizeof in a #if
So which bit of the standard did I miss?
BFN. Paul. 11 4099
#include <stdio.h>
>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
[code snnipet...]
So which bit of the standard did I miss?
Well I think that #if can be used only with the Macros(who gets
replaced with contant expressions at compile time) since sizeof gets
resolved at run time I dont think it's good idea to use sizeof with
#if!!
Regards
Vikas Gupta
kerravon wrote:
According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
Why then do most of my compilers barf on this?
#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
They barf on the "#if sizeof ..." line.
They explicitly say that I can't use sizeof in a #if
So which bit of the standard did I miss?
BFN. Paul.
I don't have a copy of C90; but I suspect that this is not an area where
the differences between C90 and C99 matter. Section 6.10.1p4 of
n1256.pdf says
"After all replacements due to macro expansion and the *defined* unary
operator have been performed, all remaining identifiers (including those
lexically identical to keywords) are replaced with the pp-number 0, "
I believe that this applies to both 'sizeof' and 'int' in your example.
As a result, it becomes:
#if 0(0) >= 4
which explains why it fails.
vicks <vi*******@gmai l.comwrote in news:d485a4a6-dd4a-492e-a00b- 9b**********@r1 g2000hsg.google groups.com:
Well I think that #if can be used only with the Macros(who gets
replaced with contant expressions at compile time) since sizeof gets
resolved at run time I dont think it's good idea to use sizeof with
#if!!
sizeof yields a compile-time constant, and is very much _not_ evaluated at
runtime.
--
Tomás Ó hÉilidhe
kerravon wrote:
>
According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
The preprocessor can't read keywords.
ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;83
83 Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
— there simply are no keywords, enumeration constants, etc.
--
pete
On Dec 10, 8:11 am, pete <pfil...@mindsp ring.comwrote:
kerravon wrote:
According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
The preprocessor can't read keywords.
ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;83
83 Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
-- there simply are no keywords, enumeration constants, etc.
--
pete
#if is for preprocessor, sizeof is a compile time operator but not
handled at preprocessing stage. If you have gcc put sizeof in your
code and run gcc -E <soure filenameit should give you the
preprocessor output and sizeof would remain as it is.
In article
<81************ *************** *******@d21g200 0prf.googlegrou ps.com>,
kerravon <ke******@w3.to wrote:
Why then do most of my compilers barf on this?
#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
One way to see this is that sizeof is not usable in the proprocessor, which has no
knowledge of types. There is a solution, though
#include <stdio.h>
#define XXX (sizeof(int) >= 4 ? "big" : "small")
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
If you want to check at compile time that int is big:
/* cause a compile-time error if x is 0 */
#define CompileAssert(x ) do{typedef struct{char f[(x)?1:-1];}assertion_fai lure;}while(0) /* compile time assertion failure */
#include <stdio.h>
int main(void)
{
CompileAssert(s izeof(int) >= 4);
puts("hello big");
return (0);
}
Francois Grieu
Keith Thompson wrote:
>
pete <pf*****@mindsp ring.comwrites:
ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;83
83 Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
-- there simply are no keywords, enumeration constants, etc.
That quotation is from the C99 standard, not the C90 standard.
That quotation is from the C90 standard.
I think you must be using a public draft instead.
This quotation is from the C99 standard:
ISO/IEC 9899:1999(E)
6.10.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;140)
140) Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
— there simply are no keywords, enumeration constants, etc.
--
pete
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>pete <pf*****@mindsp ring.comwrites:
ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;83
83 Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
-- there simply are no keywords, enumeration constants, etc.
That quotation is from the C99 standard, not the C90 standard.
That quotation is from the C90 standard.
I think you must be using a public draft instead.
No, I'm using a copy of the actual C90 standard, but you're right, the
quotation is from the C90 standard. I don't know how I missed that.
Apologies.
[...]
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>> pete <pf*****@mindsp ring.comwrites:
ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints
identifiers (including those lexically identical to keywords)
are interpreted as described below;83
83 Because the controlling constant expression
is evaluated during translation phase 4,
all identifiers either are or are not macro names
-- there simply are no keywords, enumeration constants, etc.
That quotation is from the C99 standard, not the C90 standard.
That quotation is from the C90 standard.
I think you must be using a public draft instead.
No, I'm using a copy of the actual C90 standard, but you're right, the
quotation is from the C90 standard. I don't know how I missed that.
Apologies.
[...]
--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Saunders |
last post by:
I have made a declaration like this:
private const Complex I = new Complex(0.0, 1.0);
When I try to build this I get the error:
The expression being assigned to 'ComplexNumberLib.ComplexMath.I' must be
constant.
I do not understand why this constructor is not considered to be constant
|
by: devdatta_clc |
last post by:
Hi C experts
I've a bunch of questions.
Consider this simplified piece of code.
const int a = 10;
int main () {
static int b = a;
|
by: Andrzej 'Foxy' D. |
last post by:
Hi!
I cannot figure out, why is the following code incorrect:
const int array = { 1 };
const int var = 1;
template<int I>
struct Blah {};
|
by: lovecreatesbeauty |
last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the
"hello, world" thing using the name "string-constant". But `ISO/IEC
9899:TC2' does not include this kind of thing in section `A.1.5
Constants'.
|
by: Dan Smithers |
last post by:
What constitutes a constant-expression? I know that it is something that
can be determined at compile time.
I am trying to use template code and keep getting compiler errors
"error: cannot appear in a constant-expression"
template <int s>
class CFoo
{
private:
| |
by: John Koleszar |
last post by:
Hi all,
I'm porting some code that provides compile-time assertions from one
compiler to another and ran across what I believe to be compliant code
that won't compile using the new compiler. Not naming names here to
remove bias - I'm trying to tell if I'm relying on implementation defined
behavior or if this is a bug in the new compiler.
Consider this stripped down example:
|
by: Hendrik Schober |
last post by:
Hi,
this
#include <string>
class test {
typedef std::string::size_type size_type;
static const size_type x = std::string::npos;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
|
by: Adem |
last post by:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) :
case constant-expression :
I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
|
by: Stefano Sabatini |
last post by:
Hi all, I'm encountering this while trying to implement a factory
singleton method to generate objects.
The singleton has a static map which binds a static creation function
defined in each class to the type of the object to be created.
Here it is the code, which is a modification of the wikipedia C++
factory example code:
----------------------------------8<--------------------------------
|
by: PJ6 |
last post by:
Const factor As Double = Math.Sqrt(3) / 6
Error 1 Constant expression is required.
This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the same
input, and those that can change. All (?) of the functions in Math are of
the former type and should be allowed to evaluate to a constant expression,
just the same as the results of expressions with +, -, /, etc.
|
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...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |