Small C issue.. "expected expression before.."
Note: The main questions has been solved already but there's good info for novices down there. If you have some spare time, it won't hurt to read further :)
Hello, I'm currently studying C with "C Programming Language Second Ed." and I just can't figure out what's wrong with my code. I'd appreciate a small hint here. The error: Quote:
And the code: Code:
#include <stdio.h> |
Code:
#define IN 1; // defines if it's in a word Code:
#define IN 1 /* defines if it's in a word */ |
'else' can only come after a matching 'if', ie
Code:
if (x == 'x' ) Code:
if( x == 'x') Personally I ALWAYS use parentheses to avoid probs like that, also if you go to add a debug line to the (implied) if block, it'll fail... Its also unusual to have #define other than at the top of the prog, just after the #includes. Its just a directive to the pre-processor to do a simple text substitution everywhere it matches, as per Guttorm's comment. |
Edit: It's interesting because on the book it's written without curly braces and it's supposed to work but it actually doesn't.
Thanks a lot guys... Skuzye |
Quote:
|
ops.. :P
Quote:
|
Guttorm and swodniw's answers were correct (at least the main part) and maybe enough. But I think Tinkster might still be missing the key point:
Code:
if( x == 'x') Code:
if( x == 'x') Code:
if( x == 'x') If the ; is included when you define OUT and included again when you use OUT then the compiler must parse two ; in a row. Quote:
|
Quote:
|
Just one more question.
I was examining again this code and I noted that I stored an integer number in a char type variable and I haven't noted any issues with that. Is it ok? I mean, I can see it's possible but is it a good practice?
I just can't see why to use an integer(4bytes) or even a short integer(2 bytes) if I just need to store 1 or 0 and a char type is 1 byte. Skuzye |
Quote:
Code:
state = OUT; Quote:
Quote:
Depending on what else you do with the variable "state", this is likely to be one of the less common cases (for x86 or x86-64) where a char results in faster code than an int. I personally would tend to use a char for that variable, but I consider that choice pretty arbitrary. Neither char nor int would be considered more or less "good practice" in this case. Best practice in C++ for that variable would be either: Rename it so that it is meaningful with values true and false, and then use a bool. or Define IN and OUT within an enum (much better than #define anyway) and then make "state" that enum type. (but I forget how much of the above is reasonable in C rather than C++). Quote:
|
johnsfine has pointed out the why's and why not of using a char/int; yet as you are using C and there are only two possible values have you considered using a bool. bool was introduced to the language with C99 and requires the header stdbool.h
You could change the code to something like Code:
bool in = false; |
Swodniw, I think you have that test backwards and I think
Code:
in = !in; Skuzye, I didn't notice before that you failed to initialize "state". That is another bug in your code. Using bool (assuming that C++ or C99 construct is OK to use) I suggest: Code:
bool in_word = false; |
Quote:
Quote:
|
Quote:
Quote:
Quote:
Quote:
Quote:
Skuzye |
Quote:
The compiler probably would use registers in a way such that there isn't actually any difference in data size. If there were a difference in data size, any difference in code size would likely be bigger than the difference in data size. Unless you are very experienced in both asm and C, you can't predict the difference in code size that results from using char vs. int for a function local variable. More often than not the code for char will be larger (and slightly slower) than the code for int. But it can go either way. Quote:
Quote:
|
Lightening fast answer :P
Quote:
Quote:
Let me know if I understood something: 1. Shrinking code would matter because smaller code is stored on faster memories. (link) Quote:
Is this right? Quote:
Skuzye |
Quote:
I didn't mean to imply you should routinely take that into account when coding. You shouldn't. But you seemed to want to know what would be better when you think about the possibility that lots of tiny differences might add up to a significant difference. Then the code size for using char local variables vs. int local variables might add up to something that matters. The data size difference won't. Quote:
On the stack, the data storage size difference between a char and an int usually makes less difference than when stored elsewhere in memory. More often than not, it makes no difference. When stored in a register it is near certain that the data storage size makes no difference (the rest of that register won't be used for anythig else anyway). So even when you care about tiny differences (such as the possible code size difference) which you usually shouldn't care about, you still shouldn't care about the strage space fscalar local variables. Quote:
|
Thanks a lot!
Quote:
Well I guess this is enough for this thread if you don't mind. I can't see how to go further, although if you have any indications of websites or book I'd appreciate. Skuzye |
A couple of suggestions, sorry I didn't post them earlier but they didn't came to mind then:
First, avoid using #define when dealing with constants. Use const typename for that. const gives your compiler a chance to check for type compatibility and it is also useful to avoid errors like the one you've experienced. #define is useful when you want to write portable code, for example: Code:
#ifdef PLATFORM == WINDOWS Instead, think of what will happen in case you use EVERY bit in your array (and not just the first from every element). Thus, the 0-th bit of x[0] will store weather 0 is located in the list or not; the 1-st bit of x[0] will store info. about 1 ... the 7-th bit of x[0] will store info. about 7 Information about number 59412 will be stored on the fourth bit of x[7426]. Using this technique the total size drops 8 times, or sizeof(char), to a respectable value of 512MB of RAM. In case you were not familiar with this method, I suggest looking up 'bitsets' or 'bit arrays' (C++ has it's own bitset class, but I would stay away from that until I've understood them well). In case you already knew about bitsets... well sorry to take you time :P Maybe somebody else will benefit from it. Cheers! |
@agemo
Really interesting. I laughed when you said it would take 4GB of RAM, I can't even imagine such a result in a computer with less RAM than it (which isn't so rare, huh..hehe) I didn't know about it thank you, you didn't take my time :P |
Quote:
Quote:
Certainly in C++ there are such flaws. I always use enum when the constant I'm defining logically ought to be a const int (I've done that for so long, I've forgotten some of the flaws in const that made me start doing it) . C++ seems to have better support for enum and an enum can do all the things that a const int could have done. IIRC, const double is even more flawed in C++ language rules than const int and there is no similar work around. I still try hard to avoid needing to use #define, which is even more flawed. A scalar constant should be something you can define in a header file with the scoping benefits of whatever scope (typically a class definition) you want to give it. C++ doesn't work that way, which is a flaw in the language specification. #define ignores ordinary scoping rules and its scope is normally the rest of the compilation unit in which it was seen. That makes it even worse than the flawed rules for const typename. |
Quote:
Quote:
Quote:
Quote:
|
Quote:
C++ would be a better language (would let us write more effective and more maintainable programs) if constant class members could be defined within the class definition. |
Quote:
|
All times are GMT -5. The time now is 03:37 PM. |