Blog

Tag Archives: conversions-and-promotions

ptrdiff_t links for the week-end
Pascal Cuoq on 2 March 2012

Here are two links related to ptrdiff_t: Ptrdiff_t is evil [removed dead link] a blog post for if you are not tired of conversion and promotion issues yet and an interesting answer on StackOverflow. I was doubtful about the second one (I had no difficulties to accept the first one...

Read More

Checking for overflows, revisited once
Pascal Cuoq on 12 February 2012

I do not have any solution I am 100% happy with to the overflow dilemma in the previous post. Here is one of the solutions that does not make me 100% happy. The first (partial) solution is: program so that overflows correspond exactly to unwanted circumstances (and then it becomes...

Read More

Checking for overflows operation by operation
Pascal Cuoq on 20 January 2012

My colleague Bernard Botella pointed out an interesting example in an offline discussion following the last quiz. The setup Consider the snippet: int s; unsigned u1 u2; ... s = u1 - u2; The programmer's intention with the assignment is to compute in variable s of type int the mathematical...

Read More

A bit of explanation regarding the quiz in the last post
Pascal Cuoq on 20 January 2012

There are only positive constants in C, as per section 6.4.4 in the C99 standard: integer-constant: decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt decimal-constant: nonzero-digit decimal-constant digit octal-constant: 0 octal-constant octal-digit hexadecimal-constant: hexadecimal-prefix hexadecimal-digit hexadecimal-constant hexadecimal-digit ... The minus sign is not part of the constant according to the grammar. The...

Read More

Constants quiz
Pascal Cuoq on 20 January 2012

What does the following program print? long long l; #include <stdio.h> int main() { l = -0x80000000; printf(\%lld" l); } $ gcc t.c && ./a.out And this one? (beware trick question) long long l; #include <stdio.h> int main() { l = -2147483648; printf("%lld" l); } $ gcc t.c && ./a.out...

Read More