c89

Was `long` guaranteed to be as wide as `size_t`

青春壹個敷衍的年華 提交于 2020-03-01 05:15:08
问题 When looking for evidence of unsigned long being enough to hold size_t for the purpose of being argument to printf I ran into two fact(oid)s. First there's this answer stating that long is indeed not guaranteed to be large enough for size_t . On the other hand I saw this answer suggesting to use printf("%lu", (unsigned long)x) in pre C99, x being of size_t . So the question is could you assume or were you guaranteed that long were enough to hold size_t in pre C99 . The other question is

Matrices as function parameters in C89

二次信任 提交于 2020-02-02 02:21:38
问题 For most of my undergrad C programming course, we studied C99 and our lecturer never bothered to teach us the main differences between C99 and previous versions. We have recently been informed that there's a possibility we'll be asked to implement solutions using C89 during our next exam, instead. My question regards the use of variable-length multidimensional arrays with regards to declaration and usage inside a function. In C99, I can have a function like this: void func(int cols, int mat[]

Which type should I use for a pointer ? ptrdiff_t or void*?

喜夏-厌秋 提交于 2020-01-25 06:18:31
问题 Which line is the correct (best) way for defining a pointer? typedef ptrdiff_t pointer; // pointers are ptrdiff_t. -- or -- typedef void* pointer; // pointers are void*. pointer ptr = malloc(1024); 回答1: Pointers in C are of type T* where T is the type pointed to; void* is the generic pointer type. Usually, you let C implicitly convert void* to something useful, e.g. char *buffer = malloc(1024); ptrdiff_t is the type returned by the subtraction of two pointers, e.g. ptrdiff_t d = write_ptr -

Which type should I use for a pointer ? ptrdiff_t or void*?

核能气质少年 提交于 2020-01-25 06:18:12
问题 Which line is the correct (best) way for defining a pointer? typedef ptrdiff_t pointer; // pointers are ptrdiff_t. -- or -- typedef void* pointer; // pointers are void*. pointer ptr = malloc(1024); 回答1: Pointers in C are of type T* where T is the type pointed to; void* is the generic pointer type. Usually, you let C implicitly convert void* to something useful, e.g. char *buffer = malloc(1024); ptrdiff_t is the type returned by the subtraction of two pointers, e.g. ptrdiff_t d = write_ptr -

`clang -ansi` extensions

ぃ、小莉子 提交于 2020-01-21 11:23:58
问题 I ran into an issue recently where the following toy example compiles cleanly using clang -ansi : int main(void) { for (int i = 0; 0; ); return i; } but gcc -ansi gives the following error: a.c: In function ‘main’: a.c:3:5: error: ‘for’ loop initial declarations are only allowed in C99 mode a.c:3:5: note: use option -std=c99 or -std=gnu99 to compile your code Compiling with clang -ansi -pedantic shows that a C99 extension is being used. a.c:3:10: warning: variable declaration in for loop is a

`clang -ansi` extensions

流过昼夜 提交于 2020-01-21 11:22:21
问题 I ran into an issue recently where the following toy example compiles cleanly using clang -ansi : int main(void) { for (int i = 0; 0; ); return i; } but gcc -ansi gives the following error: a.c: In function ‘main’: a.c:3:5: error: ‘for’ loop initial declarations are only allowed in C99 mode a.c:3:5: note: use option -std=c99 or -std=gnu99 to compile your code Compiling with clang -ansi -pedantic shows that a C99 extension is being used. a.c:3:10: warning: variable declaration in for loop is a

Is Visual Studio's C4028 warning (formal parameter different from declaration) spurious?

て烟熏妆下的殇ゞ 提交于 2020-01-13 04:33:13
问题 Consider the following function declaration and definition. In the header file: void some_function(int param); In the source file: #include "test.h" void some_function(const int param) {} int main(void) { return 0; } Under Visual Studio 2010, compiling as a pure C project, I see warning C4028: formal parameter 1 different from declaration . But as far as I know, this is perfectly valid C, and fairly common practice. Am I wrong about this, and is VS2010 therefore correct to warn me? Or if it

Fixed-width integers in ANSI C

拥有回忆 提交于 2020-01-04 14:13:47
问题 How do I handle a 4-byte char array as a typical int in ANSI C? Some context: I'm parsing a binary file, where I need to read 4-bytes unsigned integers. I want to make sure that, no matter what platform this parser is compiled in, an "int" is always 4 bytes long. I've read about uint32_t & friends, but I'm limited to ANSI C. Thanks in advance. 回答1: throw some preprocessor commands in there #include <limits.h> #if LONG_BIT == 32 long buffer [BUFFER_SIZE]; #elif WORD_BIT == 32 int buffer

Fixed-width integers in ANSI C

僤鯓⒐⒋嵵緔 提交于 2020-01-04 14:12:51
问题 How do I handle a 4-byte char array as a typical int in ANSI C? Some context: I'm parsing a binary file, where I need to read 4-bytes unsigned integers. I want to make sure that, no matter what platform this parser is compiled in, an "int" is always 4 bytes long. I've read about uint32_t & friends, but I'm limited to ANSI C. Thanks in advance. 回答1: throw some preprocessor commands in there #include <limits.h> #if LONG_BIT == 32 long buffer [BUFFER_SIZE]; #elif WORD_BIT == 32 int buffer

Use variadic functions in C89 without passing number of arguments or a final argument?

天涯浪子 提交于 2020-01-04 05:41:06
问题 Let's say I have a variadic function foo(int tmp, ...) , when calling foo function I need to know how many arguments there are. I'm aware of two ways of finding out how many arguments there are: Use a final argument when calling foo, like -1, so your function call will be like this: foo(tmp, 1, 2, 9, -1) and when you are inside foo and a va_arg call returns -1 you know you have read all the function arguments Add one more argument in foo where the programmer will have the total number of