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 19:23
Read: times


 
#171777 - Assign always spreads to the left, but mind the data type
Responding to: ???'s previous message
1) Careful there, since this forum doesn't allow the correct expression to be displayed.
It is ok with backslash + zero inside single quotes. Just a zero inside single quotes will match the character zero instead of the character NUL. The character zero has the ASCII number 48.

2) Yes, every C compiler must let the result of an assign spread to the left. But you will have to think about your data types in case you mix different types. If you have the value 1000, the value will not fit in a 8-bit char, so you can't do:
uint32_t my32;
uint16_t my16;
uint8_t my8;
my32 = my16 = my8 = 1000;

The right-most assign with truncate to 8 bits, resulting in the value 232 (1000 = 0x03e8, 232 = 0x00e8) being assigned to the larger variables further to the left. The compiler may warn that 1000 is truncated and made unsigned.

Instead writing as below, will make sure that the value fits until it reaches the final 8-bit variable.
my8 = my16 = my32 = 1000;


Using the result of the assign expression is very useful, but you must remember that the data type of the result may not be the same as the data type of the original value that you tried to assign.

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