Live data from Hacker News

C Questions and Answers

kukuruku.co

91–100 of 135 posts

Re: C Questions and Answers

#91
post #37
post #28

Earlier quoted context omitted.

>Did he make sure the behavior he mentioned is uniform across all ISO standards? I don't know. >What is his source for coming up with a certain answer to a certain piece of code? His conclusions are consistent with my understanding of the C standard. His justifications for his conclusions refer to specific rules regarding program behaviour, which suggests he's using the standard(s). >First of all, this gives a strong…

> ... MAY assume ... and therefore his claim: > ... Turns out, bar() is invoked even when x is the null pointer ... is incorrect (bar may or may not be invoked) and misleading for C newbies, without any mention of the subtleties of standards and implementations. Hence my original comment.

You're misreading a descriptive statement as normative. It could have been clearer but it's not wrong.

Re: C Questions and Answers

#92

Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…

> I have trouble imagining how to make something like C any lower without becoming architecture-specific or cumbered with details that are more economically-suited for a compiler.

You mean Algol, Mesa, and few others from the same vintage or even older?

Or rather Macro Assemblers like MASM, TASM that provided higher level macros for structured programming?

Re: C Questions and Answers

#93
post #39

This reminds of me of the Quiz books that were popular years ago. They'd show some code that inadvertently tripped some obscure corner of the language. Rarely did the quizzes provide great insight. Rather, they confirmed the benefits of keeping your code idiomatic.

It's pretty much what pops into my head every time I see things like these. I got most of those correct, but my universal reaction was why the hell would I write something like that in the first place ?

I figure a lot of the time, you don't write it like that, but you get weird behavior in a much more complicated situation without obvious defects that eventually can be reduced to an example that would fit in with those quizzes.

Re: C Questions and Answers

#94
post #68

Regarding the first answer: What is called a "tentative definition" is of course a "declaration".

Isn't a declaration "extern int i;"? Without the "extern", it's a definition, there's a symbol in that translation unit, etc.

Re: C Questions and Answers

#95
post #67

Earlier quoted context omitted.

Meh, the problem is the preprocessor. A lot of really silly things are produced by macro expansion, so the "obviously silly" optimizations really do end up mattering.

That sounds interesting, but I'm suspicious. Do you have any examples of such macros and maybe some statistics or ideas how often they are actually seen in the wild?

There are also libraries such as libCello[0] and Viola[1] (mine) to really simplify C through macros. libCello is some really cool stuff.

[0] http://libcello.org/ [1] https://github.com/eatonphil/viola

Re: C Questions and Answers

#96
post #3
post #2

This reminds me of tests I took in earlier CS classes. Knowing those things are utterly useless in practice.

What is your main programming language; in which you do most of the work?

C, actually, at the moment. But I don't use C for work or web development.

Re: C Questions and Answers

#97

Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…

#1. Tentative definitions are historical baggage from Fortan Common blocks (https://blogs.oracle.com/ali/entry/what_are_tentative_symbol...), which may have helped adoption of C by Fortran users. Although it remains in the C language specification, this feature can be ignored.

#2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions such as asserts or crashes to guard against potentially dangerous side effects. C compiler assumes that the programmer is in control of his/her code. In this example, it can be assumed that a careful C programmer has already guaranteed that the pointer will not be null when it is dereferenced. This C feature is an optimization to avoid generating redundant or unnecessary asserts or handling code.

3. C allows the programmer to handing pointers, allowing for such low-level optimizations that may not be possible in higher-level languages. A careful C programmer may have taken steps outside the function to handle the situation where yp==zp, or may otherwise be unconcerned about a particular case, for performance reasons.

4. A correct implementation of IEEE 754.

5. Since C is designed to efficiently compile to any computer architecture, it needs to be aware of the distinction between the arithmetic width of an instruction set vs the width of addresses. Ints are optimized to default to the natural arithmetic width of an instruction set (so that compilation doesn't produce unnecessary packing/unpacking instructions whenever they are accessed) but is guaranteed to be atleast 16 bits wide. However, since data structure can be as large as addressable memory, it is necessary for size_t to be the width of addresses.

6. Allowing size_t to be unsigned allows all bits of a size_t variable to be utilized for expressing size.

7. Undefined behavior is, again, an optimization feature, allowing each compiler to implement as it sees fit.

8. Comma operator is useful when first operand has desirable side effects, such as compactly representing parallel assignment or side effects in for loops.

9. C allows unsigned integers to wrap around 0 and UINT_MAX. This feature can be utilized as an optimization, for example as a free (no additional instruction) deliberate modulus operation. This is usually how unsigned integers behave in assembly.

10 & 11 & 12. Some ISA's, like MIPS, treat overflow of signed numbers as an exception. Others simply treat the result as a valid two's-compliment value. Since C is machine independent, C's official specification for overlow of signed numbers must be compatible for all ISA's. Simply treating the result as undefined does the trick, and means the compiler doesn't have to make guarantees or version for each ISA.

Re: C Questions and Answers

#98
post #92

Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…

> I have trouble imagining how to make something like C any lower without becoming architecture-specific or cumbered with details that are more economically-suited for a compiler. You mean Algol, Mesa, and few others from the same vintage or even older? Or rather Macro Assemblers like MASM, TASM that provided higher level macros for structured programming?

what I mean in my paragraph is that C has all these (nasty) details that are necessary due to C's position as being almost assembly language (but not quite) AND being cross-platform. Macro assembles like MASM and TASM use ISA-specific assembly languages, so you can't write cross platform code. I suppose one could image a sortof cross-platform LLVM IR structured macro assembler might be an example of something lower than C that is still architecture-independent that you would then pass to an machine-specific optimizing compiler.

Algol is higher level than C.

Re: C Questions and Answers

#99

Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…

#1. Tentative definitions are historical baggage from Fortan Common blocks ( https://blogs.oracle.com/ali/entry/what_are_tentative_symbol... ), which may have helped adoption of C by Fortran users. Although it remains in the C language specification, this feature can be ignored. #2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions…

Good comment, but I'd like to add that int is not really the "natural arithmetic width of an instruction set" any more. We will never see 64-bit ints. The sizes of int and long seem to be "whatever works, and is compatible with what it used to be".

Re: C Questions and Answers

#100

Earlier quoted context omitted.

#1. Tentative definitions are historical baggage from Fortan Common blocks ( https://blogs.oracle.com/ali/entry/what_are_tentative_symbol... ), which may have helped adoption of C by Fortran users. Although it remains in the C language specification, this feature can be ignored. #2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions…

Good comment, but I'd like to add that int is not really the "natural arithmetic width of an instruction set" any more. We will never see 64-bit ints. The sizes of int and long seem to be "whatever works, and is compatible with what it used to be".

thanks for clarification!
Post reply on HN