[HN Gopher] So you think you know C?
       ___________________________________________________________________
        
       So you think you know C?
        
       Author : tosh
       Score  : 46 points
       Date   : 2024-01-20 21:06 UTC (1 hours ago)
        
 (HTM) web link (wordsandbuttons.online)
 (TXT) w3m dump (wordsandbuttons.online)
        
       | LatticeAnimal wrote:
       | If anyone is curious, on gcc 12.3.0 and clang 16.0.6 (x86_64),
       | the answers are what most people (who have written lots of C)
       | would expect:
       | 
       | 1) 8
       | 
       | 2) 0
       | 
       | 3) 160
       | 
       | 4) 1 (both clang and gcc output a warning)
       | 
       | 5) 2 (only clang outputs a warning)
       | 
       | While I like the idea of this quiz, I think it would be more
       | powerful if it provided examples of compilers / architectures
       | where these are not the correct answers. (I also think thorough
       | unit tests would catch most of these errors)
        
         | Farmadupe wrote:
         | The author is making a deliberate point about undefined
         | behaviour in the article. Hence them not not providing worked
         | examples.
         | 
         | In fact, by not doing so they are making a subtle implicit
         | statement that it is uninteresting to consider actually
         | attempting to execute these snippets.
        
       | o11c wrote:
       | A more useful "so you think you know C?" is this test:
       | 
       | Write a variadic macro `CLEANSE_MACRO_ARGS` that can be used
       | within one of C's unhygienic macros, to turn it into a hygienic
       | macro without reducing the prettiness of the macro body.
       | 
       | In standard C, this requires C23 and only works for macros that
       | are not used as expressions. Or you can use GNU extensions and
       | make it work for expressions and work even on old compilers.
        
       | ahoka wrote:
       | 5/5. I don't like C.
        
       | red75prime wrote:
       | If it were "Implementation defined" and "Undefined" or "I know
       | that I can't answer" instead of "I don't know", I'd have done
       | better. Choosing to answer that you don't know is hard,
       | especially if you know why you can't give definite answer.
        
       | carom wrote:
       | That felt like a waste of time for a "gotcha" post. The author
       | must be very smart(tm). C has undefined behavior, got it, I
       | probably have a pretty good intuition for how common compilers
       | implement it though.
        
       | kwertyoowiyop wrote:
       | Did the code at the nuclear power plant include any of these? :-O
        
       | ryao wrote:
       | The third one does not make sense to me. 13 is an integer by
       | default, so multiplying it by a char promotes that char to an
       | integer. There should be no integer overflow regardless of memory
       | model.
       | 
       | That said, these days, I assume either the LP64 or ILP32 memory
       | model is used unless told to assume otherwise. I also assume
       | struct are padded unless told otherwise, because that is how the
       | compilers I support do things. As a result, the only one I got
       | right was #5.
       | 
       | He probably could add a 6th question, which is what does this
       | return:
       | 
       | int main(void) { return -1 == (~1 + 1) }
       | 
       | I am fairly confident that the answer will be the same on every
       | system on which that is run, but technically, the C standard does
       | not guarantee that it is the same unless you use C23.
        
       ___________________________________________________________________
       (page generated 2024-01-20 23:00 UTC)