Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (degenerate_762, AndrewAMD), 877 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Casting literals causes trouble #416916
02/05/13 21:33
02/05/13 21:33
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline OP
Expert
Uhrwerk  Offline OP
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
This one really knocked my socks off. shocked
Code:
#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: Uhrwerk] #416928
02/05/13 23:16
02/05/13 23:16
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
well, that's indeed strange


Visit my site: www.masterq32.de
Re: Casting literals causes trouble [Re: MasterQ32] #416946
02/06/13 13:00
02/06/13 13:00
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
It admittedly looks unsuspicious, but a look in the manual reveals that it is in fact wrong syntax, at least in lite-C. This is also a bug, as the compiler should give a syntax error here.

Opportunity for a user quiz: Who can put Uhrwerk back in his socks, show where his code won't work, and fix it with a very small modification? Solution tomorrow.


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 Offline OP
Expert
Uhrwerk  Offline 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

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] #416954
02/06/13 13:40
02/06/13 13:40
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
The solution is not omitting the cast. Typecasting a literal is an allowed operation in C.

Re: Casting literals causes trouble [Re: jcl] #416956
02/06/13 13:47
02/06/13 13:47
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline OP
Expert
Uhrwerk  Offline OP
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
I'm glad the language supports this exotic feature! grin


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 Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
-

Last edited by sivan; 02/06/13 14:08.

Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Casting literals causes trouble [Re: sivan] #417007
02/07/13 09:31
02/07/13 09:31
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
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:

Code:
#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 Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
@JCL: Constant expressions can be evaluated in programminglanguages tongue (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 Offline OP
Expert
Uhrwerk  Offline OP
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: jcl
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:
Code:
#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
Originally Posted By: jcl
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...

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1