Blegh. Besides that unoriginal title, I am currently fiddling with C.
It yells at me a lot and needless to say, we are not on the best of terms yet.

So, I come before programming peoples for help. For example,

SUCCESS!

...Sorry. I type out my posts as I think and do stuff. So, yes. I just figured out how to solve my own problem while typing this out. But, I'm sure there's more problems to come.
That *always* happens to me, 98% times I ask for help I do it.

If I don't, I get stuck.

I guess writing about it helps me.
Okay.


Code:
main()
{
   printf("%d\n",max(12,42));
}
max(a,b) {if (a>b) return a; else return b;}


Why does this return an incompatible implicit declaration warning?


Code:
max.c: In function ‘main’:
max.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’
Prizm C doesn't have any such function as printf(); there's PrintXY() instead. Also, "max(a,b) {if (a>b) return a; else return b;}" is not valid; this is valid instead:


Code:
int max(int a, int b);
int main() {
...blah...
}

int max(int a, int b) {
    if (a>b) return a; else return b;
}
Function such as printf() are console functions. PRIZM C is more of a graphical C.
ScoutDavid wrote:
Function such as printf() are console functions. PRIZM C is more of a graphical C.
Yup, that's more or less accurate. Of course, it would be relatively trivial for someone to whip up a set of printf(), gets(), getc(), scanf() routines that could emulate a simple terminal. Which reminds me that I need to write my own custom text library.
I have also noticed that the program I posted above actually works.
It throws a warning but it actually works.
Raylin wrote:
I have also noticed that the program I posted above actually works.
It throws a warning but it actually works.
Works as in prints to the screen? Shock
Yeah. It's weird.
Raylin wrote:
Yeah. It's weird.
If you call printf multiple times, does it print and then start scrolling when it gets to the end? I'm wondering if perhaps one of us (Jonimus? Tari? Simon? z80man?) implemented a rudimentary printf() somewhere, or if it's an actual syscall.
I don't think he means on the Prizm, because there's nothing remotely like that in libfxcg.

It works because printf basically expects to have arguments thrown at it willy-nilly on the stack anyway, so even though the compiler warns you that it doesn't know what printf should look like (because you didn't prototype any functions).

A version of that which should be free of both errors and warnings:

Code:
#include <stdio.h>
int max(int a, int b);

int main()
{
   printf("%d\n",max(12,42));
}
int max(int a,int b)
{
    if (a>b)
        return a;
    else
        return b;
}

I'm amazed it compiled without the types on max, and the inclusion of stdio provides the prototype for printf.
That is why you should *always* compile C with -werror (treat warnings as errors).
Tari wrote:
I don't think he means on the Prizm, because there's nothing remotely like that in libfxcg.
That's what I thought too, but then I said this and he didn't disagree, but perhaps he just didn't read carefully:
KermMartian wrote:
Prizm C doesn't have any such function as printf(); there's PrintXY() instead. Also, "max(a,b) {if (a>b) return a; else return b;}" is not valid; this is valid instead [...]
Kllrnohj, or alternatively you could just not ignore warnings. Razz
KermMartian wrote:
Kllrnohj, or alternatively you could just not ignore warnings. Razz


Two problems:

1) That requires you to watch the build output - and if you are building more than a handful of files, staring at the build looking for warnings quickly becomes a waste of time.

2) Too easy to just ignore.
As long as you don't package with -Werror so that when it comes time for someone else to build it compiler version changes don't break your build.
It is valid to omit return type and argument types. Missing types are assumed to be "int". I'm not sure if it's valid C90/C99, but it is valid according to K&R C (which GCC allows). You can even omit types on global variable declarations. Smile

It's generally considered poor form to leave off types, of course.

EDIT:

Code:
foo;
main(argc)
{
        return 42;
}

Here's gcc's output (gcc -std=c90 -pedantic -Wall tmp/tmp.c):

Code:
tmp/tmp.c:1:1: warning: data definition has no type or storage class
tmp/tmp.c:1:1: warning: type defaults to `int' in declaration of `foo'
tmp/tmp.c:2:1: warning: return type defaults to `int'

So it looks to be valid according to c90 (and also c99), though it does throw warnings about the missing types.

Omitting types like this can be useful for really tight code, as found on ioccc.org Razz
I suppose if you're trying to write obfuscated code, but in normal circumstances, it's a travesty to omit so much stuff that way. Smile

Code:
#define GOOD 1.5
#define NA 1
#define BAD .5
// The unit types are:
// Infantry, Mech, Recon, Tank, Md Tank, Neo Tank, APC, Artillery, Rockets, Anti-Air, Missles.
// Their types are: Inftry, Veh, Copter, Ship, Sub, Plane
// Syntax: Type, Move, Vision, Fuel, Max Fuel, Consumption, Primary Weapon, Primary Rounds, Primary Max Rounds, Secondary Weapon, Secondary Rounds, Secondary Max Rounds.
// Weapons: None, M Gun, Bazooka, Cannon, Neo Cannon, Rockets, Vulcan, Missiles
int unitStats[11][12] = {{1,3,2,99,99,1,0,0,0,1,0,0},{1,2,2,70,70,1,2,3,3,1,0,0},{2,8,5,80,80,2,0,0,0,1,0,0},{2,6,3,70,70,1,3,9,9,1,0,0},{2,5,1,50,50,1,3,8,8,1,0,0},{2,6,1,99,99,1,4,9,9,1,0,0}, {2,6,1,70,70,1,0,0,0,0,0,0}, {2,5,1,50,50,1,3,9,9,0,0,0}, {2,5,1,50,50,2,5,6,6,0,0,0}, {2,6,2,60,60,1,6,9,9,0,0,0}, {2,4,5,50,50,2,7,6,6,0,0,0}};
int unitCosts[] = {1000,3000,4000,7000,16000,22000,5000,6000,15000,8000,12000};
char *unitType(int a);
main()
{
   printf("An %s unit costs %d G.\n",unitType(0),unitCosts[0]);
}
char *unitType(int a)
{
   static char *names[]={"Infantry","Mech","Recon","Tank","Md Tank","Neo Tank","APC","Artillery","Rockets","Anti-Air","Missles"};
   return names[a];
}


In that case, is this okay? Razz
You're still missing a type declaration on main. The normal, standard sauce is:


Code:
int main(int argc, char* argv[]) {


Don't worry about what argc (number of arguments) and argv (contents of arguments) are for until you start taking command-line arguments.
KermMartian wrote:
I suppose if you're trying to write obfuscated code, but in normal circumstances, it's a travesty to omit so much stuff that way. Smile

"Travesty" doesn't even begin to describe the code on ioccc.org. I would say it's a "horror" instead. Shock Razz
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
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 vote in polls in this forum

 

Advertisement