General > General Technical Chat
How many people code in C these days, and if so, why?
nctnico:
--- Quote from: SiliconWizard on May 10, 2020, 02:18:27 pm ---
--- Quote from: Nominal Animal on May 10, 2020, 12:47:50 pm ---typedef enum { true, false, FILE_NOT_FOUND } boolean;
:phew:
--- End quote ---
Uh huh. As was discussed in other threads, C enums are just loose collections of integer constants, and the language doesn't have any provision for type-checking enums (beyond the values being integers).
Just the way it is. Yes it sucks a little.
And yes C99 added a true boolean type (_Bool, or bool if you include stdbool.h) which is guaranteed to hold only 1 or 0 (unless probably you mess with it using pointers and casts).
I've never been really bothered with older C versions not having a true boolean type. All you had to know was that 0 was considered false as a condition, and anything else, true. That worked well enough. There was no point directly testing a "boolean" (I'd say rather a "condition") against a value anyway (something I have already seen from some developers - ugly!) This would make no sense. One way it could make sense would be if you used a bool value
--- End quote ---
I strongly disagree on this. IMHO a comparison should always show what is being compared; it will make the code unambiguous to someone not well versed in a language. This is especially true for C because only 0 is false and anything else is true. For that reason my API functions always return -1 (failure) or 1 (success).
engrguy42:
More importantly, even though C# and .NET are an f***'d up disaster, I somehow managed to add CSV file logging to my data acquisition app. Less than an hour. Got lucky I guess. :-+
madires:
--- Quote from: coppice on May 10, 2020, 11:45:10 am ---
--- Quote from: nctnico on May 10, 2020, 10:45:57 am ---It also seems the storage size of bool varies between compilers.
--- End quote ---
I've always found that weird. The generated code rarely seems to improve through the use of a bool where sizeof(bool) is bigger than 1.
--- End quote ---
The developer of the compiler has chosen whatever the CPU processes with the least amount of clock cycles. If the CPU needs the same number of clock cycles for an 8 bit, 16 bit, 32 bit or 64 bit value then roll a dice or think about memory usage and management.
Siwastaja:
IMHO,
if(dog_exists) // makes sense to a normal human being
if(dog_exists == true) // sounds like a tax bureacraut wrote this
if(dog_does_not_exist == false) // argh!
If you find adding excess "== true" or "== false" tests for bools makes the code more readable, that's a sign that your variable naming sucks or the variable logic is upside-down.
Or, that you are just stuck in this practice often seen by novices. Even experienced people have their blind spots.
nctnico:
--- Quote from: Siwastaja on May 10, 2020, 02:49:03 pm ---IMHO,
bool dog_exists;
if(dog_exists) // makes sense to a normal human being
if(dog_exists == true) // sounds like a tax bureacraut wrote this
--- End quote ---
The problem is that in C 'if' can be used with any type. So in order to fully understand what 'if(dog_exists)' does exactly you need to look up what type dog_exists is first. If you write 'if(dog_exists == true)' then it is clear that dog_exists is a boolean immediately. That makes reading & modifying the code a whole lot easier. The easier code is to read & understand the easier it is to hand over to someone else (including a novice). I'm not a fan of elitist code styles which basically say 'screw you novice, spend another year in kindergarten'. Then you are wasting people's salary because they are not as productive as they can be even though they may not grasp every tiny detail of the C syntax.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version