Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/19/09 17:32
Read: times


 
#171769 - Both are ok.
Responding to: ???'s previous message
Both expressions are correct - either using the digit zero, or a backslash + zero inside single quotes.

The backslash + zero represents the ASCII NUL character with ordinal value zero.

But in C, a character constant and an integer are basically the same thing, so it is just as ok to directly write the integer 0 instead of doing it as an explicit character constant. This is similar to using the numeric values 10 and 13 when handling line feed (backslash + n) or carriage return (backslash + r).

A quick note about C. It is a bit special in that an assignment forms a subexpression with the value of the assign. This allows code like:
a = b = c = d = 0;

The bad thing, is that it allows a developer to accidentally write an assign when intending a comparison:
if (a = b) { ... }
instead of:
if (a == b) { ... }


Because it is so common to make this mistake, most compilers will detect this and issue an error. The way to prove to the compiler that the assign was intended and that no warning is needed is then to perform an explicit comparison on the result of the assign, as in:
if ((a = b) != 0) { ... }

The resulting code generation for the above is identical as the following, but the above way of assigning make sure that there will not be any warning.
if (a = b) { ... }


List of 14 messages in thread
TopicAuthorDate
Some help with copying strings in C (again)            01/01/70 00:00      
   Blast.            01/01/70 00:00      
   ?            01/01/70 00:00      
      Crunched function            01/01/70 00:00      
         maybe            01/01/70 00:00      
            = vs ==            01/01/70 00:00      
               ok            01/01/70 00:00      
                  '\0'            01/01/70 00:00      
                     just for record            01/01/70 00:00      
                        Both are ok.            01/01/70 00:00      
                           A warning - not an error            01/01/70 00:00      
                              Yes. Should have said "warning"            01/01/70 00:00      
                           but            01/01/70 00:00      
                              Assign always spreads to the left, but mind the data type            01/01/70 00:00      

Back to Subject List