I am getting:
Warning 4 comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
my code that produce this is:
if (714 < V < 723) //21.50-21.75
pwm_duty(1550);
What your code is doing is
if ((714 < V) < 723)
What you want is
if ((V > 714) && (V < 723))
I agree, I've fallen into the same trap many times while learning my way around C,
Oh shit, I now have 40 statements to correct
Just for ease of visual review I usually write such things as
((LowVal < x) && (x < HiVal))
Oh shit, I now have 40 statements to correct
You have 40 'if' statements (like that) in your code? Ouch. Aside from the syntax issue, is there a better way to accomplish what you're trying to do? E.g., a table of hi/lo values?
I know it's probably not elegant or fast but I need to change a PWM based on supply voltage as well as an input selection so the code first checks which input is selected and then starts looking at the supply voltage in order to set the correct pwm
Just for ease of visual reviewI usually write such things as
((LowVal < x) && (x < HiVal))
+1. I avoid relaying on operator precedence.
Another option, since the OP has 40 similar comparisons:
inline boolean isInRange(int v, int min, int max) {
return (v > min) && v (v > max);
}
and then
if (isInRange(v, 714, 723)) {
}
This also semantically cleaner because v will be evaluated exactly once (e.g. in case it's a function call).
Just for ease of visual reviewI usually write such things as
((LowVal < x) && (x < HiVal))
yes!
pet peeve of mine: people do not use enough parens in if-statements. I love to see explicit grouping. rely on precedence? no way, I don't always remember what comes before what, but with parens, its clear as day.
I encourage c programmers to use more parens like this. its always better design to be very explicit, even if just to help readers understand what you wrote and meant.
@linux-works: +1 and a hug
Oh shit, I now have 40 statements to correct
vim and regexps are your best friends to do that
Luckily, there are languages where a<b<c means ((a<b) and (b<c)). Python is one of them. I'd have speculated that MATLAB would be equally smart (considering that MATLAB is for non-programmers) , but unfortunately, they're as dumb as C. MATLAB continues to disappoint me all the the time. Don't even begin with scoping...
they're as dumb as C.
C is a low-level language... it's mean't to be a language for writing operating systems and not math formulas
have you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason)
while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language. actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages
C is a low-level language... it's mean't to be a language for writing operating systems and not math formulas
have you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason)
while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language. actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages
C is a high-level language. As good as any other. What you mean is a domain-specific language.
About that particular expression not understood by C. Every language has "formalities" you need to know. You can now argue that one formality is essential, or you can just learn what the language you use has, move on and spend your time solving the problem you really want to solve.
Since Python was mentioned. Many people argue Python's "formalities", like the indentation style, are superior to other languages. While one formality (e.g. brackets) was just replaced by another (indentation). That is breaking continuity just for being different, not for adding anything of value. Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.
Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.
The two expressions have different semantic. In (a < b < c) range checking each of a, b, c, is evaluated once while in ((a < b) and (b < c)) b is evaluated twice. Examples:
(a < ++b < c) vs ((a < ++b) and (++b < c))
(a < readVoltage() < b) vs (a < readVoltage()) and (readVoltage() < c)
with C you can directly access memory - it's not high level for me.
But you cannot directly query the status flags generated by the ALU or manipulate I/O ports so it is high-level for me (I say this as an implementor of Prolog compilers and runtimes so I know what high-level is, and I'm currently debugging microcode for a CISC CPU so I know what low-level is). It's all relative and it's all meaningless. The real question is if it meets the spec. or not. Language wars are for the small minded.
C is a high-level language. As good as any other. What you mean is a domain-specific language.
with C you can directly access memory - it's not high level for me.
link to wikipedia:
http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning
no wonder for modern C# or NET hash hash programmers to say such thing. low level language is machine code, and then there is assembly code which is one to one translation of the machine code, hence still a low level language. and then there are more human readable languages, it starts from language such as C/C++. if its a low level language, you wont have the luxury of doing nonsense things like "if (a < b < c) bracket" since its a nonsense in machine code domain or semantically ambiguous there is. and then there are more modern languages type to cope with OOP (creation) hassles?, they are managed languages, perharps thats what you are using right now. and no, you cant access memory directly in C (or any other type of languages, even assembly) in protected mode OS.
and no, you cant access memory directly in C (or any other type of languages, even assembly) in protected mode OS.
and what is a pointer then ? if not a pointer to a location in your address space ? no one says you can access the physical memory (that's hidden from you by the MM) - but you can freely poke around in the address space belonging to the process (virtual address space) with pointers in C.
with high level languages (python, perl, ruby... whatever) you simply can't do that.
with high level languages (python, perl, ruby... whatever) you simply can't do that.
ctypes?? Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.
with high level languages (python, perl, ruby... whatever) you simply can't do that.
ctypes?? Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.
-- quote from ctypes web
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
-- end quote
does it sound to you like python itself can access the memory ? not to me
and what is a pointer then?
pointer is a "feature" provided by the language where it has no merit to decide if a laguange is low or high imho. from one perspective, "low or high" level was meant to how you interact programmatically with the native machine code. and from one perpective, pointers or arrays or even variables are all the same, they are addressing memory location. when you are assigning a = 2 you are actually manipulating memory space
granted with variables or arrays you cant control of where in the memory location you want to manipulate, do you think you can do that with pointers in protected mode? such as *ptr = whereeverilike ? you may try, there is reason once upon a time the blue screen was called "famous". exception to some hacks maybe, thats i call the deviation from an "ideal" or "theoritical" or "intended" implementation of the system. being able to hack from C doesnt necessarily means its low enough level. (2cnts from a person who lived the life)
and with pointers in C, you dont take it for granted, you need to call/pass system/OS's memory management or API to get the address. remember "free", "malloc", "new"? what do you think are they?
with high level languages (python, perl, ruby... whatever) you simply can't do that.
ctypes?? Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.
-- quote from ctypes web
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
-- end quote
does it sound to you like python itself can access the memory ? not to me
As it can apparently pass memory into C functions that take a pointer and access same on the way out there is something going on there, not as simple as p = 0x1234; *p = 0x5678 I admit.
and what is a pointer then?
pointer is a "feature" provided by the language where it has no merit to decide if a laguange is low or high imho. from one perspective, "low or high" level was meant to how you interact programmatically with the native machine code. and from one perpective, pointers or arrays or even variables are all the same, they are addressing memory location. when you are assigning a = 2 you are actually manipulating memory space granted with variables or arrays you cant control of where in the memory location you want to manipulate, do you think you can do that with pointers in protected mode? such as *ptr = whereeverilike ? you may try, there is reason once upon a time the blue screen was called "famous". exception to some hacks maybe, thats i call the deviation from an "ideal" or "theoritical" or "intended" implementation of the system. being able to hack from C doesnt necessarily means its low enough level. (2cnts from a person who lived the life)
and with pointers in C, you dont take it for granted, you need to call/pass system/OS's memory management or API to get the address. remember "free", "malloc", "new"? what do you think are they?
blue screens, segfaults and kernel panics are exactly the consequence of possibility poking around in the address space belonging to the process... i'm not saying you can do whatever you want with pointers (actually you can , but with appropriate consequences) - i'm saying it's possible with pointers in C and therefore C is low-level.
i used to write linux kernel modules (device drivers - for non linux guys) for custom hardware.... you don't have to explain me anything
from my point of view (and it's not only me, there is lot of us over the world;) ) C is considered low level because you can directly manipulate the memory within the address space belonging to the process.
back in times when C was born it was considered high-level , but not now.... if C is high level, then what would you call the real high level languages ? super-high-level ?