8 ms·
So you think you know C? (2016)
- deleted 3y ago[deleted]
- LatticeAnimal 3y agoIf 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 3y agoThe author is making a deliberate point about undefined behaviour in the article. Hence them not executing 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. The third paragraph of the "P.S" of the article (you have to press submit to see it) is the one that really gives the game away.
- ryao 3y agoMost of these things are implementation defined rather than undefined. Only the 5th is undefined.
- 1000100_1000101 3y agoMore than implementation defined, for some you need context that simply isn't given. On the ones with mixed-type structs, even if you know what system it's compiled for you don't know if someone has used pragma pack 1 to byte pack the data instead of standard packing. Just seeing the struct, you still don't know.
- LatticeAnimal 3y agoI agree that in theory it would be cool to have C code that uses only defined behavior and works on all platforms for all eternity. However, I think most programs have a fairly clear understanding of what platforms (OS+arch) they are targeting and what compilers they are using to target those platforms. If the compiler has defined behavior (and you have unit tests for that behavior) on all of these platforms, I don't think it is a huge deal. (Ideally you wouldn't... but sometimes its an accident or unavoidable) As an example, while struct padding (problem 1) might not technically be in the spec, it is a cornerstone of FFI and every new compiler (that supports C FFI) has a way to compile structs with the same padding. To my original point, if the article had instead given examples of compilers + architectures that produced different answers, I might feel differently. However, just saying mentioning that these weird edge cases are undefined (in the spec) doesn't mean much to me.
- ryao 3y agoMy answers for 2, 3 and 5 were different: 2) I thought the type would be promoted to short. It turns out that the result of the arithmetic operation is promoted to int. 3) The signness of char is platform dependent. It is signed on x86 and amd64, but unsigned everywhere else. After seeing my mistake, I would expect this to cause the answer to be -96 on amd64 from sign extension when it converted into an integer, yet it is 160, which is what I would have expected from a platform where char is signed. If anyone knows why it is 160 here, please let me know. 5) This is a classic. I know to answer I do not know because despite having an Operator Precedence, C famously says that this is undefined. I have no idea why the standard does this when there is a clearly right answer. Java for example makes this have only 1 right answer.
- erhaetherth 3y agoCan you explain 3? A space is 20 IIRC, 2013 is 260. An unsigned char tops out at 255 but I guess this one is* signed so... that's 127. And then I have no idea what happens, some kind of overflow, but I don't know the wrapping rules.
- jbeninger 3y agoI made the same mistake having worked with URL encoding for so long. " " is 20...in hex.
- defrost 3y ago" " is a C string constant ... so a space encoding followed by a NUL encoding.
- ryao 3y ago' ' is 0x20 or 32.
- defrost 3y agoor 0x40 .. or something else. https://en.wikipedia.org/wiki/EBCDIC https://en.wikipedia.org/wiki/EBCDIC
- deleted 3y ago[deleted]
- not2b 3y agoThe case with multiple increments in an expression might produce different results depending on optimization level, perhaps not in this case but in other cases. That is because the compiler is allowed to use any order, so the order it picks might depend on what is in the registers.
- erhaetherth 3y ago5) How does 2 make sense? Shouldn't be 0 + 1? Or does the pre-increment take precedence over the addition, thus the left i is 1 but not because of the post-increment?
- deleted 3y ago[deleted]
- P-Nuts 3y agoTo get 2, there are (at least) a couple of ways it can happen, we can do i=0,i++ and get LHS=0, now i=1,++i and get RHS=2. Or we can do i=0,++i and get RHS=1, then i=1,i++ and get LHS=1. However we’re also allowed to do something like this: i=0, a=i, b=i, b=b+1, RHS=b (RHS=1), LHS=a (LHS=0), a=a+1, i=a, i=b. Probably quite a lot of other things are allowed to happen. Usual disclaimer that a standard-compliant compiler is allowed to vaporise your cat etc as part of UB. The thing to Google is “sequence points”.
- o11c 3y agoA 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 3y ago[flagged]
- red75prime 3y agoIf 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.
- ummonk 3y agoYeah if the first one weren't so obviously implementation defined, I'd have second-guessed myself on a lot of the others.
- carom 3y agoThat felt like a waste of time for a "gotcha" post. The author must be very smart™. C has undefined behavior, got it, I probably have a pretty good intuition for how common compilers implement it though.
- pardoned_turkey 3y agoIt's even worse: for example, the first one isn't undefined, it's merely unspecified - i.e., "depends on the system in a well-known and predictable way," not "you're doing something very wrong and the result is chaos."
- aw1621107 3y ago> it's merely unspecified - i.e., "depends on the system in a well-known and predictable way," I think that should be implementation-defined behavior, not unspecified behavior? IIRC unspecified behavior in C is not required to be known or consistent.
- plorkyeran 3y agoMy bigger problem with the first one is that the explanation is incorrect and it isn't actually about structure padding. The explanation is making the incorrect assumption that `sizeof(int)` is always 4, but it isn't. `sizeof(*(&s))` can be as small as 2.
- advisedwang 3y agoThat's kind of his point though - don't rely on your intuition for how most common compilers work! Learn enough to write correct code!
- kwertyoowiyop 3y ago[flagged]
- okanat 3y agoHopefully not. However those stuff historically have been written in Ada or a very restricted set of C.
- ryao 3y agoPresumably, someone writing code for a nuclear power plant would be using astree and get warnings whenever they do anything wrong: https://www.absint.com/astree/index.htm https://www.absint.com/astree/index.htm It is a shame that it is not accessible to the rest of us. :/
- 1over137 3y agoNot accessible? You mean very expensive?
- ryao 3y agoHe 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.
- pockybum522 3y agoProgrammers annoyed with the 'quiz' might really enjoy this book on that site, which is an excellent read. https://wordsandbuttons.online/SYTYKC.pdf https://wordsandbuttons.online/SYTYKC.pdf
- MaxMoney 3y ago[dead]
- sys_64738 3y agoI can proudly say I got zero and I pretend to be a C programmer!
- wrs 3y agoI learned C on a VAX, then used it on 68K, ARM6, very briefly Cray (that's a weird one), x86 in segmented mode, x86 in 32-bit flat mode, and now 64-bit in various flavors. As soon as I saw this code was using "short" and "int" I immediately knew all bets were off! The comments remind me of the VAX programmers in 1990 who wrote code assuming pointers and ints were the same size, and if yours weren't, they told you to get a "real computer".
- Const-me 3y agoMy today’s WTF on the topic. The following C++ returns incorrect value when the argument is 8 or greater: inline uint64_t makeRemainderMask( ptrdiff_t missingLanes ) { // This is not a branch, compiles to conditional move missingLanes = std::max( missingLanes, (ptrdiff_t)0 ); // Make a mask of 8 bytes // No need to clip for missingLanes <= 8 because the shift is already good, results in zero uint64_t mask = ~(uint64_t)0; mask >>= missingLanes * 8; return mask; } TIL the language standard defines right shift operator for unsigned types in a weird way, making so for uint64_t argument, a >> b is equal to a >> ( b % 64 ). I expected zero on the output for b >= 64, not that.
- dzaima 3y agoC++ doesn't even define it as that; it's actually just undefined behavior to shift by any amount greater than or equal to the width!
- gosub100 3y agoThats not on the topic though. Do you think because two languages share a common letter in their name that they are equivocal?
- trealira 3y agoThey said C++, but aside from the line containing "std::max," they wrote C code, and what they said applies to C as well.
- mogoh 3y ago[flagged]
- tombert 3y agoDespite it being the first language I ever learned, I don't touch C a lot anymore since I've removed myself almost entirely from the systems space, and undefined behavior isn't even the main reason. Primarily, I have determined that I really don't like dealing with manual memory management. For all the tasks I'm interested in, C's performance gains over a GC'd language are marginal at best. Number crunching? Julia is competitive with C. Web servers? The JVM will handle it just fine. Microcontrollers? For what I do Lua+NodeMCU or MicroPython does everything I need it to. Reasonably fast command line application? Go's got you covered. When I do use C in 2024, I pretty much always cheat and use the Boehm GC, which is fast enough for whatever I need it for. I'm not smart enough to know if I handled pointers correctly, and I don't know that I want to spend the time to get smart enough. Obviously systems C and manual memory management has a place in the driver and kernel world, and if you're genuinely good with it then my hat goes off to you, but I don't feel like it buys me enough today to use it much.
- genman 3y ago[flagged]
- ahoka 3y agoJust look at the comments here.
- genman 3y agoI know for what it is used fo and who are using it. That was not the question. There is a lot of undefined platform specific behaviour in C and yet it is mainly promoted for its cross platform property. It makes me a little uneasy if I happen to think about it too much.
- tialaramex 3y agoIf you didn't like these because they're "trick" questions you likely also would not enjoy CppQuiz (https://cppquiz.org/ https://cppquiz.org/) However you might well enjoy https://dtolnay.github.io/rust-quiz/ https://dtolnay.github.io/rust-quiz/ Like the C++ quiz, "Undefined Behaviour" is a valid answer, however, the quiz questions are about safe Rust, so that answer is always wrong. I still get more than half of them wrong unless given far too long to think about it.
- dwattttt 3y ago> Rust Quiz > > What is the output of this Rust program? > > > // JavaScript is required (sorry) Tears is the output of that Rust program. EDIT: I will never understand HN comment newlines.
- notfed 3y agoAlso: https://neal.fun/password-game/ https://neal.fun/password-game/ (Point is, some "quizzes" are made cynically to demonstrate a flaw in some design. Don't get your ego all bruised up because you got some questions "wrong" on the internet.)
- tialaramex 3y agoWhile that's true, neither the Rust nor C++ Quiz are for this purpose. They're intended as an opportunity to learn, what's actually going on and how does that differ from your intuition. The password game is a game first and a commentary on how stupid "password rules" are second. The game makes sense in our cultural context (where stupid password rules are a thing) but it would be fun (but less popular) anyway.
- CodeNest 3y ago[flagged]
- codeflo 3y agoSince the questions specified neither a specific compiler/platform nor included Undefined or Unspecified Behavior as an option, I assumed this is an informal quiz and the author just tested the programs on their specific compiler. Turns out the author does mean according to the standard, but thinks that “I don’t know” is a synonym for both undefined and unspecified behavior. It seems weird to use imprecise terminology in a post that’s all about lecturing about standards compliance.
- TillE 3y agoI'm really surprised at how many people are annoyed by this quiz. It doesn't specify a compiler/platform, therefore all the definite answers are clearly wrong.
- codeflo 3y agoI think most C programmers know that the size of ints is implementation defined, and I've programmed on platforms where it was 2 bytes instead of 4. But when someone promises a brain teaser, and then asks an unclear question, you read that and go "What the author actually means can't be that dumb, can it?" It's annoying when a charitable reading of an unclear statement leads to an aggressive "gotcha".
- croes 3y ago>I think most C programmers know that the size of ints is implementation defined, and I've programmed on platforms where it was 2 bytes instead of 4. So it's easy to say: I don't know.
- codeflo 3y agoBut I do know. Though not specified by the standard, ints are 4 bytes on any standard platform anyone has used in this millennium.
- WalterBright 3y ago
- utopcell 3y agoMy favorite UD in C is this: int x, y; x ^= y ^= x ^= y; I was using this for years until I realized that it is UD.
- ryao 3y agoTwo assignments to the same variable in the same statement are undefined for the same reason i++ + ++i is undefined. That said, even if it were not, you did not define x and y. Reading them is also undefined behavior, which is what I initially though you meant until I read what followed them.
- robertgraham 3y agoThe first two answers are 8 and 0. They are technically `undefined` according to the C standard, but are the behavior of every mainstream compiler. So much of the world's open-source code depends upon these that it's unlikely to change. Using clang version 15.0, the first 2 produce no warning messages, even with -Wall -Wextra -pedantic. Conversely, the last 3 produce warning messages without any extra compiler flags. The behavior of the first two examples are practically defined even if undefined according to the standard. Now, when programming for embedded environments, like for 8-bit microcontrollers, all bets are off. But then you are using a quirky environment-specific compiler that needs a lot more hand-holding than just this. It's not going to compiler open-source libraries anyway. I do know C. I knowingly write my code knowing that even though some things are technically undefined in the standard, that they are practically defined (and overwhelmingly so) for the platforms I target.
- ryao 3y agoThe first 4 are implementation defined rather than undefined. That said, warnings do not necessarily mean that the code is invoking undefined behavior. For example, with if (a = b) GCC will generate a warning, unless you do if ((a = b)). The reason for the warning is that often people mean to do equality and instead write assignment by mistake, so the compilers warn unless a second set of braces is used to signal that you really meant to do that.
- o11c 3y agoIn the cases involving overflow, it's implementation-defined whether there's undefined behavior.
- turminal 3y ago> The first 4 are implementation defined rather than undefined. Third and fourth are only defined in some implementations.
- ryao 3y agoThat is fair for 4, although would explain why it is the case for 3?
- vbezhenar 3y agoMy recent WTF with C and C++ was that I found out that C++ compiler can just throw away infinite loop (and clang does that), while C compiler must not throw it away and must compile it as expected. It's all according to the standard. For example it's a typical construct for embedded software. That solidified my opinion that C and C++ are very different languages despite naive view being that C is just a subset of C++. They're fundamentally different.
- ryao 3y agoHere is another. In C, union types support type punning. In C++, they broke that guarantee, so if you compile C code that uses a union type as C++ code, it could be broken. It would be nice to know why they broke this. My guess is that it violated the strong type philosophy of C++ and thus had to be broken on ideological grounds. C++ also broke implicit void pointer conversions, but at least that one had the reason that it was incompatible with function overloading. Not that anyone involved with C++ would tell you this. Instead, they provide bad C code that breaks the strict aliasing rule and claim that breaking implicit void pointer conversions somehow follows from it when in reality that is a non-sequitur: https://www.stroustrup.com/bs_faq2.html#void-ptr https://www.stroustrup.com/bs_faq2.html#void-ptr
- charleslmunger 3y agoWhen would you want a loop that does not terminate and does not perform I/O, read/write a volatile, synchronize with another thread, or perform an atomic operation?
- kerkeslager 3y agoI know C well enough to know that writing C to avoid every edge case left undefined by the standard is a fool's errand. There are a bunch of problems with this approach: a. Your target ecosystem(s) probably follows a few norms in cases not defined by the standard. If it works on the target ecosystem(s), nobody cares if it's non-standard. Testing on the target ecosystem, preferably automated testing, is the standard that matters. b. Some of these norms are extraordinarily powerful. For example, check out NaN boxing[1]. Standard? Hell naw. Useful? So useful that it's practically mandatory for dynamic PL interpreters to compete on performance. c. So let's say you're targeting an ecosystem that does some atypical stuff, i.e. doesn't follow the norms mentioned. Well, joke's on you, off-the-beaten-path ecosystems typically also have bleeding-edge tooling that doesn't implement the standards. So again, testing on the target ecosystem, preferably automated testing, is the standard that matters. d. So let's say you're targeting a lot of ecosystems, so many ecosystems that you can't reasonably test on all of them, like if you're writing NetBSD or the JVM. Okay, well, the edge cases of the C standard still don't matter because you just shouldn't be using any of the off-the-beaten-path parts of the C standard anyway, since those parts are the parts which aren't implemented in lots of obscure architectures. e. And sure, if you really just enjoy being annoyingly pendantic you can go and learn the whole C standard and go write your own blog test with integrated quiz and then condescendingly tell people they don't know C, with the implication that you do know C. But the reality is that knowing those weird edge cases of C isn't useful because if you actually use those edge cases of C then nobody else on your team can read your code. f. And lastly, there's plenty of defined behavior which you shouldn't use, too. Just because you know what it does, doesn't mean the next person reading your code knows. My answers to the questions in the test: 1. 8 on most ecosystems. If it matters, test it, but if it matters you probably need to use one of the non-standard extensions mentioned to do anything about it, so the standard didn't matter so much did it? 2. 0 on most ecosystems. But frankly, don't use `short int`, ever. Use int with the limits defined in `limits.h` if you just want the most efficient integer width, and if you need a specific integer width use the the types in `stdint.h`. Yes, I'm aware that `int` isn't defined as the most efficient in the standard, but `int` is one of the first things people implement in a C compiler so the likelihood of coming across an int that isn't the fastest integer size is absurdly small. 3. Play stupid games win stupid prizes. Who cares if this is defined? Even if it were defined it would be bad code. Don't multiply chars, FFS. 4. Play stupid games win stupid prizes. Obviously bit twiddling when you don't know teh structure of the bits is bad. The fact that Endianness is relevant in bit shifts and yet is not brought up by the author does in fact show the dangers of smugly pretending you know C better than other people. 5. Play stupid games win stupid prizes. Who cares if this is defined? Even if it were defined it would be bad code because depending on the pre/postfixedness of ++ is hard for most people to reason about. [1] https://craftinginterpreters.com/optimization.html#nan-boxing https://craftinginterpreters.com/optimization.html#nan-boxin...
- MaulingMonkey 3y agoScrewed up on the 4th question by mistakenly assuming the minimum standard-allowed magnitude of INT_{MIN,MAX} implied int was at least 32 bits. While that's true for LONG_{MIN,MAX} and long, int can be a mere 16 bits. Good to have had that mistaken bit of errata corrected.
- mark-r 3y agoA very long time ago, the Microsoft C/C++ compiler used 16 bit ints. I had a boss that insisted we use long instead of int because he had been burned by this. Hadn't been a problem for at least 20 years, but that didn't matter to him.
- dookahku 3y agoi'm an old C dog and i loved this post.
- dzaima 3y agoHere's a mini-quiz: on a regular gcc/clang for x86/x86-64/arm32/arm64, which of the following functions can ever produce undefined behavior? Answers can be obtained by compiling with "-O3 -fsanitize=undefined" and seeing which functions include __ubsan_handle*. int16_t a( int16_t a, int16_t b) { return a*b; } uint16_t b(uint16_t a, uint16_t b) { return a*b; } int32_t c( int32_t a, int32_t b) { return a*b; } uint32_t d(uint32_t a, uint32_t b) { return a*b; }
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- ryao 3y agoFor 32-bit types, the undefined behavior is where I expected it to be, but it is the opposite for the 16-bit types. Why are the 16-bit types different? To make things weirder, I tried 8-bit and 64-bit versions. The 64-bit version behaved like the 32-bit versions, but the 8-bit versions had no undefined behavior checks. Why is 8-bit so different?
- quasarj 3y agoWell, my first thought was of course, you can't know the answer to the first question without a lot more information.... I guess it's good that was the point.
- pipeline_peak 3y agoI never worked so hard to get a 0%
- Decabytes 3y agoIt really is amazing that over 50 years later, we are only finally getting languages that are working on replacing C. Most went in the direction of being Big Idea languages that tried to supplant domains like C++ and Java. The language that is probably the closest is Zig, but I hear Odin is pretty good too (the website specifically mentions it as a C alternative) The things I like about C aren't really about the language itself. There just aren't many programming languages that do these things well 1. Cross platform across Linux, MacOS (Intel and Arm), Windows (Without the use of MinGW And Cygwin), *BSD 2. Ability to create an executable 3. Small programs that start quick 4. Simple syntax that does not obfuscate when allocations are occurring For GCed languages GO is probably the one that best fits this criteria for most people. Sometimes I wish someone just took C and improved it. Instead of looking at C++ and thinking "what features should C have that C++ has" It should look back at C and go "What features does C not have, that it should"? Some that come to mind are 1. A better syntax for creating pointers and dereferencing them 2. Fixed const so that it works with array initialization 3. Modules instead of headers 4. Cross platform string types that encoded length 5. Hash tables 6. Remove preprocessor macros and replace common use cases with things built into the language 7. Build system that didn't rely on Make, Cmake, Ninja, Autotools, configure etc 8. Easy interop with C Maybe this is a lot to ask, but most languages I see are solving waay more problems then this. 2,4,5, can be done without leaving C, which is why most people homebrew their own solutions. I think Pascal actually comes close, but it's held back by the fact that the editor support is pretty poor. Pacal-Mode in Emacs is not good, and Lazarus with its multiple floating windows is infuriating to have to alt+tab multiple times to switch to and from it. Plus I could never actually get it to build my programs properly without throwing some dwarf errors. Recently I've been playing around with Gambit-C and it seems to suit most of my needs. It compiles to C, and then the C code is compiled with Clang, MSVC, or GCC depending on what your platform is. I get the benefit of a language that has more features than C with it being R7RS (but still being small because it's Scheme) and implementing the most common SRFIs (including hashes), but with all the portability of C. It also has dead simple interop with C using the C-Lambda and C-Define special forms (you can write C directly inside the Gambit code)^1 which means you can leverage C code with hardly any effort. There are tradeoffs with this approach, but IMO almost all of them are ecosystem related which can be fixed, as opposed to language related, which might be impossible to fix without breaking compatibility with existing code 1. https://www.deusinmachina.net/p/gambit-c-scheme-and-c-a-match-made https://www.deusinmachina.net/p/gambit-c-scheme-and-c-a-matc...
- not2b 3y agoI knew all these. I came from a time when portable code had to work on an incredibly diverse range of platforms with many different compilers, some proprietary, and all of these corresponded to real issues, not language lawyer nitpicking.
- tester756 3y ago>But the reasonable doesn’t mean right for C thats both: funny and sad
- deleted 3y ago[deleted]
- eqvinox 3y agoMissing "(2016)". (one of) previous discussions: https://news.ycombinator.com/item?id=20366940 https://news.ycombinator.com/item?id=20366940 I really wish we could let this die. All it shows is that if you provide insufficient context, you get insufficient answers. There are better ways to point out C's shortcomings.
- ttyt 3y agoC is really the only language. Every other feature in every other language can be implemented in C, but no one wants it bloated with high level features that can just be compiled as a library. We can know what is happening in memory in a human readable way. And that is the beauty of it. If you can’t tell what’s happening in memory to some degree, then it’s not that you don’t know C, it’s that you don’t comprehend computer architecture.
- notfed 3y agoSeems like some people are getting their ego hurt because they don't like being told they got the wrong answer. Very human. This quiz isn't an IQ test, people, and the questions are intentionally trick questions. It's essentially a form of cynicism intended to demonstrate major design oversights of the C programming language. So calm down and have a laugh. Also, "I don't know" is the right answer. If you can't accept that, you may need to meditate more.
- 010101010101 3y agoI don’t see anyone walking away feeling ego bruised from this, I mostly see people annoyed that the _more_ correct option of “I can’t know because you didn’t provide enough information” isn’t represented and the author is attempting a “clever” gotcha moment instead.
- WalterBright 3y agoHaving implemented two C compilers, knowing every last detail is sometimes a bit annoying, because one thinks "why the heck is that there!" For example, the syntax for a cast is indistinguishable from that for a function call for many cases. The only way to parse it correctly is to keep a symbol table of the typedefs. Believe me I tried, and finally threw in the towel and did a special symbol table just for typedefs. This is just pointless complexity. It's why D's cast syntax looks like this: cast(T)x; i.e. cast is a keyword.
- throwawaymaths 3y agoSeems pointlessly complex too. Why didn't you just make it cast(T, x)?
- WalterBright 3y agoNeither is more complex. I just like the former better.
- throwawaymaths 3y agoWell, certainly my eyes need to add an extra parsing case so maybe don't assume it's equally complex. I don't think I'm wrong on that count.
- absdiff 3y agoPython and Haxe have a function-like cast like that, but the argument order differs between the languages. I prefer D's approach because it doesn't have this opportunity for confusion. The prefix operator only has one place for the type to go. https://docs.python.org/3/library/typing.html#typing.cast https://docs.python.org/3/library/typing.html#typing.cast typing.cast(typ, val) https://haxe.org/manual/expression-cast.html https://haxe.org/manual/expression-cast.html cast expr; // unsafe cast cast (expr, Type); // safe cast
- throwawaymaths 3y ago
- deleted 3y ago[deleted]
- dang 3y agoRelated: So You Think You Know C? (2020) [pdf] - https://news.ycombinator.com/item?id=37541685 https://news.ycombinator.com/item?id=37541685 - Sept 2023 (86 comments) Free e-book (~2 MB): So You Think You Know C? [pdf] - https://news.ycombinator.com/item?id=22958870 https://news.ycombinator.com/item?id=22958870 - April 2020 (8 comments) So you think you know C? (2016) - https://news.ycombinator.com/item?id=20366940 https://news.ycombinator.com/item?id=20366940 - July 2019 (322 comments) So you think you know C? - https://news.ycombinator.com/item?id=12902304 https://news.ycombinator.com/item?id=12902304 - Nov 2016 (198 comments) So you think you know C? - https://news.ycombinator.com/item?id=12900980 https://news.ycombinator.com/item?id=12900980 - Nov 2016 (1 comment) So you think you know C? - https://news.ycombinator.com/item?id=12900279 https://news.ycombinator.com/item?id=12900279 - Nov 2016 (9 comments) Same title different articles: So you think you know C? - https://news.ycombinator.com/item?id=4657317 https://news.ycombinator.com/item?id=4657317 - Oct 2012 (13 comments) So you think you know C: the Ksplice Pointer Challenge - https://news.ycombinator.com/item?id=3125891 https://news.ycombinator.com/item?id=3125891 - Oct 2011 (98 comments)
- greatgib 3y ago> But even more, the size of the char type itself is not specified in bits either. I still hope that you will have look at what are the standard type sizes before trying to develop or compile something for a weird platform or compiler...
- yongjik 3y agoKnowledge is knowing that sizeof(int) is implementation-defined. Wisdom is understanding how likely your code is to ever run on any platform where sizeof(int) is not 4, and if the answer is "not really," then stop worrying about it. To succeed as a C programmer, you need both knowledge and wisdom.
- acuozzo 3y agoAnd expertise is baking that assumption into the code explicitly with a static_assert.
- tpoacher 3y agowell, just from reading the title, i assumed it would be trying to make a point, so I took the test without even reading the questions and got 5/5. PS: 'sizeof with parentheses' looks so weird to me.
- mark-r 3y agoSizeof without parentheses looks weird to me. Sometimes you need them, sometimes you don't, so I just have a habit of using them every time. Redundant parentheses don't hurt anything.
- cppc 3y agoLet's C