6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Sep 21, 2024 11:36 pm

All times are UTC




Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Fri Apr 15, 2016 4:49 am 
Offline

Joined: Thu Mar 03, 2011 5:56 pm
Posts: 284
BigDumbDinosaur wrote:
rwiker wrote:
BigDumbDinosaur wrote:
pnoyes wrote:
I have been doing a lot of 6502 assembly programming. Last night I went back to doing some C programming and I needed to comment out some code. I commented it out, compiled, and to my surprise it was still running as before. I then spent a good 20 minutes trying to figure out why commented out code could still possibly run.

It turns out I used a semicolon in the C code for the comment instead of //. Prepending a line in C with a semicolon does not produce a compile error.

Anyone else mix this up?

Interesting that the compiler didn't issue a diagnostic with the semicolon at the start of the line. Which compiler is this?

Why should a C compiler complain about a semicolon at the start of a line? Keep in mind that the canonical string copy routine in C is something like
Code:
void strcpy(char *d, char *s) {
 while (*d++ = *s++)
    ;
}

The semicolon in the middle of the strcpy while loop works because it is on an otherwise blank line, which automatically makes it the terminating character.


Not quite... the semicolon is a statement terminator, and in this case terminates a null statement. Being able to make a null statement stand out in the source code is useful, and not something that you want the compiler to complain about.


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 15, 2016 6:10 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8390
Location: Midwestern USA
rwiker wrote:
Not quite... the semicolon is a statement terminator, and in this case terminates a null statement. Being able to make a null statement stand out in the source code is useful, and not something that you want the compiler to complain about.

You're splitting hairs.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 15, 2016 6:50 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1948
Location: Sacramento, CA, USA
I'm not a great C programmer, but FWIW I can vouch for rwiker's description of the semicolon issue. White space, including line breaks (or the lack thereof) carry far less importance than semicolons in C and C++ source, allowing some misleading or confusing constructions at first glance (or even second glance), because many of us as humans have a tendency to see them as being in the same "ball park" of significance. In my experience, most assemblers tend to share that view. Most C compilers certainly do not.

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 15, 2016 8:10 am 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
barrym95838 wrote:
I'm not a great C programmer, but FWIW I can vouch for rwiker's description of the semicolon issue. White space, including line breaks (or the lack thereof) carry far less importance than semicolons in C and C++ source, allowing some misleading or confusing constructions at first glance (or even second glance), because many of us as humans have a tendency to see them as being in the same "ball park" of significance. In my experience, most assemblers tend to share that view. Most C compilers certainly do not.

Mike B.

Whitespace is largely ignored in C. If you look at examples of obfuscated C programs they are often have either minimal whitespace and most of the code is on very long lines, or they use lots of redundant whitespace to format the code into an interesting shape.
http://www.ioccc.org/2015/burton/prog.c

You could rewrite the strcpy example as ...
Code:
void strcpy(char*d,char*s){while(*d++=*s++);}

... and it would still work. The only mandatory whitespace in this example is between the return type and the function name.

The C grammar defines an 'expression-statement' as and optional 'expression' followed by a semicolon. A 'while' statement is defined as 'while ( expression ) statement' where 'statement' expands into a choice of several types of statement including the 'expression-statement'.

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 15, 2016 9:18 am 
Offline

Joined: Sat Mar 27, 2010 7:50 pm
Posts: 149
Location: Chexbres, VD, Switzerland
Actually whitespace is compulsory only around keywords and variable names, when a special symbol such as '(' is not used. This is obvious, as ina=3; doesn't mean the same thing as int a=3;

It can also be compulsory in pre-processor directives.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: