473,228 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,228 software developers and data experts.

Syntax error?

Looking over some code I came across a line like this

if isalnum((unsigned char)c) {

which was accepted by the compiler without complaint. Should the
compiler have issued a diagnostic in this case? (I think it's not
required to, but I'd like confirmation).

Jeremy.
Nov 13 '05 #1
4 3760
In article <bn************@ID-114079.news.uni-berlin.de>,
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote:
Looking over some code I came across a line like this

if isalnum((unsigned char)c) {

which was accepted by the compiler without complaint. Should the
compiler have issued a diagnostic in this case? (I think it's not
required to, but I'd like confirmation).


isalnum probably expands to something that's wrapped with parens.
F'rexample, on a random system readily accessible to me:
--------
dave@hct-cvs:~/clc (0) $ cat jy-for-syntax-expand-isalnum.c
#include <ctype.h>
#include <stdio.h>

#define STR(x) STR_(x)
#define STR_(x) #x

int main(void)
{
printf("isalnum(x) expands to %s\n",STR(isalnum(x)));

return 0;
}
dave@hct-cvs:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O -c jy-for-syntax-expand-isalnum.c
dave@hct-cvs:~/clc (0) $ ./a.out
isalnum(x) expands to (__ctype_b[(int) ((x))] & (unsigned short int) _ISalnum)
dave@hct-cvs:~/clc (0) $
--------
so "if isalnum(foo)" would be expanded to
"if (expansion_of_isalnum_with_parens)", which is valid.

If this weren't the case it would be, if I'm not mistaken, a syntax
error that requires a diagnostic. GCC appears to agree with me:
--------
dave@hct-cvs:~/clc (0) $ cat jy-for-syntax-broken-if.c
int bogus_function(int);

#include <stdio.h>

int main(void)
{
if bogus_function(42)
{
puts("bogus_function() returned nonzero");
}

return 0;
}
dave@hct-cvs:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O -c jy-for-syntax-broken-if.c
jy-for-syntax-broken-if.c: In function `main':
jy-for-syntax-broken-if.c:7: parse error before `bogus_function'
jy-for-syntax-broken-if.c:10: warning: control reaches end of non-void function
dave@hct-cvs:~/clc (1) $
--------
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Have some pride, you're a physics major! You know deep in your heart
you're better than the rest of the slobs there, you should be able to
knock this course off easily. --Brian B. Rodenborn in comp.lang.c
Nov 13 '05 #2
Jeremy Yallop wrote:
Looking over some code I came across a line like this

if isalnum((unsigned char)c) {

which was accepted by the compiler without complaint. Should the
compiler have issued a diagnostic in this case? (I think it's not
required to, but I'd like confirmation).


Presumably this is accepted because 'isalnum' is a macro that happens to
have a form that makes the syntax valid. You probably knew that already,
but some people reading might not.

I don't know for sure, but I don't think the standard mandates anything
particular about the form of the 'is*' and 'to*' macros (if they even
exist), other than the requirement that they evaluate their argument
exactly once.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #3
Jeremy Yallop wrote:

Looking over some code I came across a line like this

if isalnum((unsigned char)c) {

which was accepted by the compiler without complaint. Should the
compiler have issued a diagnostic in this case? (I think it's not
required to, but I'd like confirmation).


It's bad code, but not necessarily incorrect code.
For example, here's one of the several definitions of
isalnum() on the system I happen to be using at the
moment (exactly which definition you get depends on a
lot of environmental things):

#define isalnum(c) (__ctype_mask[c] & _ISALNUM)

Note the parentheses surrounding the replacement. Once
the macro is expanded in the context you've shown, you'll
have

if (__ctype_mask((unsigned char)c) & _ISALNUM) {

.... which is syntactically correct (as far as it goes).

Of course, relying on this would be a mistake.

--
Er*********@sun.com
Nov 13 '05 #4
In <bn************@ID-114079.news.uni-berlin.de> Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:
Looking over some code I came across a line like this

if isalnum((unsigned char)c) {

which was accepted by the compiler without complaint. Should the
compiler have issued a diagnostic in this case? (I think it's not
required to, but I'd like confirmation).


It entirely depends on what the compiler is seeing in translation phase 7,
when the syntactical analysis of this statement takes place. If there is
an "#undef isalnum" in effect anywhere between the inclusion of <ctype.h>
and this statement, the diagnostic is required, of course.

Your example is no different from

if FOO {

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5

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

Similar topics

1
by: Steve | last post by:
I just spent waaaaaaaaaaaayy too much time trying to track down an error that was incorrectly reported just now, and I would like to see if someone can explain to me why it was reported that way. ...
1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
2
by: david | last post by:
Anyone could give me a hand about this syntax error? Thank you. David Source Code: Dim conn As New SqlConnection(strConn) Dim daAngio As New SqlDataAdapter(strSelectStatement, conn) 'Create a...
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
1
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something ...
7
by: Josh | last post by:
I have a lot of except Exception, e statements in my code, which poses some problems. One of the biggest is whenever I refactor even the triviallest thing in my code. I would like python to...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
6
by: muby | last post by:
Hi everybody :) I'm modifying a C++ code in VC++ 2005 my code snippet void BandwidthAllocationScheduler::insert( Message* msg, BOOL* QueueIsFull,
5
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.