3 registered members (AndrewAMD, mciwjd545, 1 invisible),
778
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Casting literals causes trouble
#416916
02/05/13 21:33
02/05/13 21:33
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
This one really knocked my socks off.
#include <acknex.h>
int x = (int)1;
void main()
{
printf("%d",x);
}
I still have a feeling I overlooked something...
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Casting literals causes trouble
[Re: jcl]
#416952
02/06/13 13:35
02/06/13 13:35
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Fixing the code isn't a problem as the cast is pointless and simply can be omitted. It also works when defining x as a local variable. But that does not help me get my socks back on... ![tongue tongue](/ubb7/images/graemlins/default_dark/tongue1.gif) I'm curious where the syntax error hides...
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Casting literals causes trouble
[Re: Uhrwerk]
#416958
02/06/13 14:08
02/06/13 14:08
|
Joined: Mar 2011
Posts: 3,150 Budapest
sivan
Expert
|
Expert
Joined: Mar 2011
Posts: 3,150
Budapest
|
-
Last edited by sivan; 02/06/13 14:08.
|
|
|
Re: Casting literals causes trouble
[Re: sivan]
#417007
02/07/13 09:31
02/07/13 09:31
|
Joined: Jul 2000
Posts: 27,987 Frankfurt
jcl
![](/ubb7/images/groups/adm.gif)
Chief Engineer
|
![](/ubb7/images/groups/adm.gif)
Chief Engineer
Joined: Jul 2000
Posts: 27,987
Frankfurt
|
Ok, here's the solution: In a programming language, all expressions must be inside functions. Outside a function you can initialize a variable with a constant, but not with an expression. The reason is that the result of an expression is normally not known at compile time, but only at runtime. In lite-C an expression is any value that is not a constant. int x = 1; // ok int x = y; // not ok int x = sqrt(1); // not ok int x = (int)1; // not ok I hope you see now why your code can not work. Here's the fixed version:
#include <acknex.h>
int x;
void main()
{
x = (int)1;
printf("%d",x);
}
|
|
|
Re: Casting literals causes trouble
[Re: jcl]
#417014
02/07/13 10:33
02/07/13 10:33
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
@JCL: Constant expressions can be evaluated in programminglanguages ![tongue tongue](/ubb7/images/graemlins/default_dark/tongue1.gif) (and thats what (int)1 is) And maybe its possible to add an error message if a dynamic expression is used for initialization? It does not work, so the compiler should abort(or am i wrong?)
Last edited by Rackscha; 02/07/13 10:33.
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: Casting literals causes trouble
[Re: jcl]
#417048
02/07/13 18:33
02/07/13 18:33
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
OP
Expert
|
OP
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
In a programming language, all expressions must be inside functions. That's not true. Neither for programming languages in general, nor for Lite-C. The c standard includes constant expressions for very good reason. And you obviously followed the standard at very least partially:
#include <acknex.h>
int my_flag = PASSABLE;
void main()
{
printf("%d",my_flag);
}
You can also replace "PASSABLE" by "(1 << 9)", hence the expression evaluation cannot be done by the precompiler. It already has to be implemented in the compiler. You can even do more complex expressions, like "2 + 2 * 2 % 2". I did some quick tests and the results suggest that you already implemented correct operator precedence for static expressions. I'd love to see this functionality expanded to fully meet the c standard. @Rakscha This is also a bug, as the compiler should give a syntax error here.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|