c99

How to implement memmove in standard C without an intermediate copy?

前提是你 提交于 2019-11-26 19:56:09
From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap ; the copy is always done in a non-destructive manner. From the C99 standard: 6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, theycompare equal. If the objects pointed to are

Flexible array member in C-structure

做~自己de王妃 提交于 2019-11-26 19:09:19
Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; }; This is a valid structure declaration. I am looking for some practical use of this kind of syntax. To be precise, how is this construct any more or less powerful than keeping a double* as the 2nd element? Or is this another case of 'you-can-do-it-in-multiple-ways'? Arpan The C FAQ answers precisely this question. The quick answer is that this structure will include the double array inside the structure rather than a pointer to an array outside the structure. As a quick example, you could use your structure as in this

Does the C preprocessor strip comments or expand macros first? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-26 19:00:03
This question already has an answer here: In which step of compilation are comments removed? 2 answers Consider this (horrible, terrible, no good, very bad) code structure: #define foo(x) // commented out debugging code // Misformatted to not obscure the point if (a) foo(a); bar(a); I've seen two compilers' preprocessors generate different results on this code: if (a) bar(a); and if (a) ; bar(a); Obviously, this is a bad thing for a portable code base. My question: What is the preprocessor supposed to do with this? Elide comments first, or expand macros first? Unfortunately, the original ANSI

state machines tutorials [closed]

喜你入骨 提交于 2019-11-26 18:03:17
I am just wondering if anyone know of some good tutorials on the Internet for developing state machines. Or ebooks? I am starting working on state machines and just need something general to get me started. qrdl State machines are very simple in C if you use function pointers. Basically you need 2 arrays - one for state function pointers and one for state transition rules. Every state function returns the code, you lookup state transition table by state and return code to find the next state and then just execute it. int entry_state(void); int foo_state(void); int bar_state(void); int exit

GCC with -std=c99 complains about not knowing struct timespec

*爱你&永不变心* 提交于 2019-11-26 17:41:38
When I try to compile this on Linux with gcc -std=c99 , the compiler complains about not knowing struct timespec . However if I compile this without -std=c99 everything works fine. #include <time.h> int main(void) { struct timespec asdf; return 0; } Why is this and is there a way to still get it to work with -std=c99 ? Explicitly enabling POSIX features The timespec comes from POSIX, so you have to 'enable' POSIX definitions: #if __STDC_VERSION__ >= 199901L #define _XOPEN_SOURCE 600 #else #define _XOPEN_SOURCE 500 #endif /* __STDC_VERSION__ */ #include <time.h> void blah(struct timespec asdf)

What is the correct type for array indexes in C?

馋奶兔 提交于 2019-11-26 17:37:11
What type for array index in C99 should be used? It have to work on LP32, ILP32, ILP64, LP64, LLP64 and more. It doesn't have to be a C89 type. I have found 5 candidates: size_t ptrdiff_t intptr_t / uintptr_t int_fast*_t / uint_fast*_t int_least*_t / uint_least*_t There is simple code to better illustrate problem. What is the best type for i and j in these two particular loops. If there is a good reason, two different types are fine too. for (i=0; i<imax; i++) { do_something(a[i]); } /* jmin can be less than 0 */ for (j=jmin; j<jmax; j++) { do_something(a[j]); } P.S. In the first version of

func() vs func(void) in c99

早过忘川 提交于 2019-11-26 17:35:42
void func() In practice, an empty parameter means any argument is accepted. void func(void) accepts no argument. But in Standard C99, I find such lines: 6.7.5.3 Function declarators (including prototypes) 14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

Setting std=c99 flag in GCC

橙三吉。 提交于 2019-11-26 17:28:04
问题 I was wondering if there were any files in which I could set the -std=c99 flag, so that I would not have to set it for every compilation. I am using GCC 4.4 on Ubuntu. 回答1: Instead of calling /usr/bin/gcc , use /usr/bin/c99 . This is the Single-Unix-approved way of invoking a C99 compiler. On an Ubuntu system, this points to a script which invokes gcc after having added the -std=c99 flag, which is precisely what you want. 回答2: How about alias gcc99= gcc -std=c99 ? 来源: https://stackoverflow

ftell at a position past 2GB

删除回忆录丶 提交于 2019-11-26 17:16:12
问题 On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99 standard, is this undefined behavior since ftell must return a long int (maximum value being 2**31-1 )? 回答1: on long int long int is supposed to be AT LEAST 32-bits, but C99 standard does NOT limit it to 32-bit. C99 standard does provide convenience types like int16_t & int32_t etc that map to correct bit sizes for a target platform. on ftell/fseek

C99 complex support with visual studio

独自空忆成欢 提交于 2019-11-26 17:06:09
问题 I would like to use complex numbers as defined in C99, but I need to support compilers which do not support it (MS compilers come to mind). I don't need many functions, and implementing the needed functions on compilers without support is not too difficult. But I have a hard time implementing the 'type' itself. Ideally, I would like to do something like: #ifndef HAVE_CREAL double creal(complex z) { /* .... */ } #endif #ifndef HAVE_CREALF float creal(float complex z) { /* ... */ } #endif But I