We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, when trying to compile an small C program with uVision3 v3.53 I'm coming across some unexpected - at least to me - compilation errors, following is the complete program:
#include <reg515c.h> #define H 1 /* H abk. von HIGH */ #define L 0 /* L abk. von LOW */ sbit P4_0 = P4^0; main() { P4 = 0xFF; /* Port 4 als Eingang eingerichtet */ /*Why on earth these lines cannot be compiled?*/ unsigned char zustand = H; unsigned int i; while(1) { for(i = 0; i <= 8; i++) { while(P4_0 == H) ; if(P4_0 == L && zustand == H) { zustand = L; P1 = i; } while(P4_0 == L) ; if(P4_0 == H && zustand == L) { zustand = H; } } } }
And the error the compiler reports are: Build target 'mM-515' assembling STARTUP.A51... compiling PinNummern.c... PINNUMMERN.C(11): error C141: syntax error near 'unsigned' PINNUMMERN.C(11): error C202: 'zustand': undefined identifier PINNUMMERN.C(12): error C141: syntax error near 'unsigned' PINNUMMERN.C(12): error C202: 'i': undefined identifier PINNUMMERN.C(13): error C202: 'zustand': undefined identifier PINNUMMERN.C(18): error C202: 'i': undefined identifier PINNUMMERN.C(22): error C202: 'zustand': undefined identifier PINNUMMERN.C(24): error C202: 'zustand': undefined identifier PINNUMMERN.C(25): error C202: 'i': undefined identifier PINNUMMERN.C(29): error C202: 'zustand': undefined identifier PINNUMMERN.C(31): error C202: 'zustand': undefined identifier Target not created
The offending lines are the ones in red, neither the variable zustand nor i have sfr or sbit type so it should be allowed to declare them inside a function. When taking those variables out of main's body this way:
#include <reg515c.h> #define H 1 /* H abk. von HIGH */ #define L 0 /* L abk. von LOW */ sbit P4_0 = P4^0; unsigned char zustand = H; unsigned int i; main() { P4 = 0xFF; /* Port 4 als Eingang eingerichtet */ /* etc */
The program compiles without errors.
Any help would be appreciated. Thanks.
you need to place the lines in question before this statement (so, at the beginning of main):
P4 = 0xFF; /* Port 4 als Eingang eingerichtet */
this is a simple C issue - you are not allowed to define variables after a statement.
#define H 1 /* H abk. von HIGH */ #define L 0 /* L abk. von LOW */
Are you really, really sure that you want to have #defines for single letters? That's a recipe for headaches.
main()
Functions in C should have return values.
/*Why on earth these lines cannot be compiled?*/
In C, variable definitions must come before the first statement in a function. Hence, you'll need to define those variables before you assign a value to P4. (Things are different in C++, but C51 is not a C++ compiler).
"main() Functions in C should have return values."
I think you mean: Functions in C should have return types. void is a perfectly good type for functions that do not have a return value.
In this case, the declaration "main() { ... }" is old-style C for a function with int return type. This form should not be used. And sicne the startup script will not expect any return value, void is the only return type that makes sense for a C51 project.
"That's a recipe for headaches."
Agreed!
But, even with better names, is there really any point in defining "HIGH" as 1 ?
Isn't that (nearly) as bad as:
#define ZERO 0 #define ONE 1
A more useful definition would be to give names that actually describe what the High and Low mean; eg,
#define LED_OFF 1 /* Writing a 1 (high) turns the LED off */ #define LED_ON 0 /* Writing a 0 (low) turns the LED on */
Then, if you change the "sense" of the drive, you only need to change the definitions - the usage of LED_ON and LED_OFF remain constant!
:-)
Well, it certainly is better than
#define Z 0 /* Abbreviation of ZERO */ #define O 1 /* Abbreviation of ONE */
... which would score major bonus points in the "unmaintainable code" category due to the similarity of 0 and O in some fonts.
Let's try to improve on the scores:
#define l 0 /* lit */ #define O 1 /* off */
The above looks more like a 2x2 identity matrix.
Zero isn't the only digit looking very similar to a letter :)
Thank you, I haven't realized it, but after your explanation I've looked section 1.2 of K&R's book over, and I've seen it. Doing:
main() { unsigned char zustand = H; unsigned int i; P4 = 0xFF; /* Port 4 als Eingang eingerichtet */ /* etc */
and compiling produces no errors.
And things are different in C than in Java, I think I've got accostumed to that second language pretty much, in C there's less freedom.
Thanks.
Thank you.
in C there's less freedom.<p>
C gives the programmer basically _every_ freedom short of messing with the CPU registers directly, including the freedom to shoot himself in the foot in a multitude of ways.
and C51 gives you added extensions whereby you can actually mess with the CPU registers directly - thus giving you further freedom to blow yourself up completely!