Enforcing ANSI C89 with clang

I'm trying to make the C compiler clang go into ANSI C89 mode but without success. Here is an example session: $ cat t.c #include <stdio.h> int main(void) { puts(__FUNCTION__); return 0; } $ gcc -pedantic -std=c89 -Wall t.c t.c: In f...
more »

2017-11-29 19:11 (1) Answers

Is it safe to cast size_t to unsigned long int?

I need a portable way to print the value of a variable n of type size_t. Since I use ANSI C89 I cannot use the z length modifier. My current approach is to cast the value to long unsigned int: printf("%lu\n", (long unsigned int) n); Provided that ...
more »

2017-11-29 16:11 (2) Answers

Printing size_t using printf in GNU systems

While I was reading this article, I came across this paragraph: Similarly, don’t make any effort to cater to the possibility that long will be smaller than predefined types like size_t. For example, the following code is ok: printf ("size = %...
more »

2017-09-06 22:09 (1) Answers

Shared memory in C and missing MAP_ANONYMOUS?

I am writing a C program where I am dealing out cards to n players, represented by n forked processes. I wish for them to all share the same deck of cards, so I am attempting to use mmap() to keep track of the deck size, however the machine I have to...
more »

2017-07-15 00:07 (1) Answers

How to sum large numbers?

I am trying to calculate 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + ... + 1 * 2 * ... * n where n is the user input. It works for values of n up to 12. I want to calculate the sum for n = 13, n = 14 and n = 15. How do I do that in C89? As I know, I can ...
more »

2017-05-30 12:05 (4) Answers

the size of dynamically allocated array in C

I know that this question has been asked before, but my question is more specific, here is the code: #include <stdio.h> #include <time.h> /* must be included for the time function */ main() { time_t t = time(NULL); srand((uns...
more »

2017-05-11 14:05 (4) Answers

Adding or assigning an integer literal to a size_t

In C I see a lot of code that adds or assigns an integer literal to a size_t variable. size_t foo = 1; foo += 1; What conversion takes place here, and can it ever happen that a size_t is "upgraded" to an int and then converted back to a size_t? Wo...
more »

2016-10-22 04:10 (3) Answers