I see some strange behaviour of the ICC AVR compiler. In the following code:
char i,r;
for (i=0;i 0) { do something.... } }
I expect the for loop to break when r is negative. However, it does not and the loop is executed 5 times, no matter the returned result of get_data_field().
Is the compiler wrong or my understanding of the effects of the break-statement.
I've always mistrusted the 'break' function---seems too much like BASIC to me! i would code the function more like:
#define MAXFIELD 5 for (i=0;i 0) { do something.... } }
About the only only place I use 'break' is after the case elments in a switch statement.
It would be interesting to see what assembly language is being generated by your compiler for this code. Perhaps there is an obvious error in the generated object code.
No. Plain char, signed char, and unsigned char are three distinct types. It is implementation-defined as to whether plain char is signed or not. Most 8-bit compilers I'm familiar with make it unsigned by default.
If you want a small int, use signed char. Typedef it if the name bugs you. I use S8 or SI8
Never use plain char for anything. It's useless, even dangerous. Particularly unsuitable for character data.
Yes, sometimes. But I found the culprit (with apologies to Vladimir): a char type in unsigned in ICC AVR. So I have to explicitly declare 'signed char get_data_field()' So now it works OK.
It's time for some old-fashioned debugging. You are assuming that char is signed, which may be true for your compiler, but needn't be for C in general (it is good practice to type the variable signed char, which will force it to be signed on any conforming compiler). What is the return type for get_data_field()? If it is int and the return value is outside the range of r, you could get a false indication.
I suggest looking at the generated code. You may be able to have the compiler generate assembly output or you can look at disassembled code. In either case, verify the code for the first test on r.
It may be that the value returned in r isn't what you think it is. You can use a simulator, ICE, or other means to inspect the returned data. For debugging realtime applications on the fly I have inserted code to generate pulse patterns on a spare pin at certain critical points in the code.
When I write in C builder, char is always signed. In my older versions of '51 compiler, it was signed. Even in my Z80 compiler. Sigh..... Thanks for the hint though.
"Meindert Sprang" wrote in news: snipped-for-privacy@news.nb.nu:
Although the folks in comp.lang.c don't approve of this, I find it has saved me many a time in my embedded work with C compilers. Once I include this file I never use C's char, int, short, etc., only these base types.
**** Programmer is responsible for determining if compiler
** and architecture support these types and bit widths.
**** U = Unsigned integer type
** S = Signed integer type
** F = Floating point type
**** 8,16,32,64 indicates the bit width of the type
*/ typedef unsigned char U8; typedef signed char S8; typedef unsigned short U16; typedef signed short S16; typedef unsigned long U32; typedef signed long S32; typedef float F32; typedef double F64;
Yes. But it was an assumption based on experience. All compilers I have previously used treated char as signed unless an option was set. But you learn something everyday :-)
From a practical standpoint, I would suggest using unsigned char for "actual" characters. In all cases, the char value of a member of the execution character set cast to unsigned char will have the same representation (and vice-versa), so there's no real downside, even for standard library functions expecting char or char*. And it prevents errors like the following:
A number of years ago, I had a system with a custom terminal for the user interface. The terminal had a set of function keys, and would return 0xF1 for F1, 0xF2 for F2, etc. I learned the hard way that the following is an infinite loop if char is signed (functions simplified for the sake of the example):
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.