c99

got compile error when use clock_gettime in c99

强颜欢笑 提交于 2019-12-01 15:53:37
when I use clock_gettime in my code snippet, and compile with flag -std=c99, I got a error like this: warning: implicit declaration of function 'clock_gettime' error: 'CLOCK_REALTIME' undeclared (first use in this function) and I have included the file 'time.h'. anyone knows how to fix it. in your original code with -std=c99, try adding #define _POSIX_C_SOURCE >= 199309L the man page for clock_gettime indicates this is a necessary feature test macro requirement. 来源: https://stackoverflow.com/questions/13069758/got-compile-error-when-use-clock-gettime-in-c99

NULL function pointers

痴心易碎 提交于 2019-12-01 15:51:19
What is the behavior of calling a null function pointer? void (*pFunc)(void) = NULL; pFunc(); Why is it advisable to initialize yet unused function pointers to NULL? In C and C++, this is called undefined behaviour , meaning that this can lead to a Segmentation fault , nothing or whatever such a case will cause based on your compiler, the operating system you're running this code on, the environment (etc...) means. Initializing a pointer to a function, or a pointer in general to NULL helps some developers to make sure their pointer is uninitialized and not equal to a random value, thereby

Threading and Thread Safety in C

社会主义新天地 提交于 2019-12-01 15:12:57
问题 When there is a common set of global data that needs to be shared among several threaded processes, I typically have used a thread token to protect the shared resource: Edit - 7/22/15 (to incorporate atomics as a viable option, per Jens comments) My [First] question is , in C, if I write my routines in such a way as to guarantee each thread accesses one, and only one element of an array: Is there any reason to think that asynchronous and simultaneous access to different indices of the same

C99 Structure Designated Initialisers and other value

给你一囗甜甜゛ 提交于 2019-12-01 15:08:42
I am aware that in C99 you can initialize members of the structure using member name as follows : struct myStruct { int i; char c; float f; }; So following is valid : struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'}; Also it is said that uninitialised members will be set to 0 . So struct myStruct m = {.f = 10.11, .c = 'a'}; here i will be set to 0 But, for the following : struct myStruct m = {.f = 10.11, .c = 'a', 6}; i is still initialized to 0. What is the reason if we do such compound initialization. This is covered in the draft C99 standard section 6.7.8 Initialization , basically if the

NULL function pointers

£可爱£侵袭症+ 提交于 2019-12-01 14:55:44
问题 What is the behavior of calling a null function pointer? void (*pFunc)(void) = NULL; pFunc(); Why is it advisable to initialize yet unused function pointers to NULL? 回答1: In C and C++, this is called undefined behaviour , meaning that this can lead to a Segmentation fault , nothing or whatever such a case will cause based on your compiler, the operating system you're running this code on, the environment (etc...) means. Initializing a pointer to a function, or a pointer in general to NULL

C99 Structure Designated Initialisers and other value

匆匆过客 提交于 2019-12-01 14:00:03
问题 I am aware that in C99 you can initialize members of the structure using member name as follows : struct myStruct { int i; char c; float f; }; So following is valid : struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'}; Also it is said that uninitialised members will be set to 0 . So struct myStruct m = {.f = 10.11, .c = 'a'}; here i will be set to 0 But, for the following : struct myStruct m = {.f = 10.11, .c = 'a', 6}; i is still initialized to 0. What is the reason if we do such compound

Is it guaranteed that Complex Float variables will be 8-byte aligned in memory?

谁说我不能喝 提交于 2019-12-01 13:02:37
In C99 the new complex types were defined. I am trying to understand whether a compiler can take advantage of this knowledge in optimizing memory accesses. Are these objects ( A - F ) of type complex float guaranteed to be 8-byte aligned in memory? #include "complex.h" typedef complex float cfloat; cfloat A; cfloat B[10]; void func(cfloat C, cfloat *D) { cfloat E; cfloat F[10]; } Note that for D , the question relates to the object pointed to by D , not to the pointer storage itself. And, if that is assumed aligned, how can one be sure that the address passed is of an actual complex and not a

What is the meaning of “static” in parameters array types in C?

十年热恋 提交于 2019-12-01 11:11:33
I saw following little complicated function definition. void foo(double A[static 10]) { double B[10]; } Is it valid C & C++ code? Is it new syntax introduced by C99 or C++ standard? What is the purpose of it? When should I use it? What is the need for this? This C99 notation, void foo(double A[static 10]) , means that the function can assume that A points to 10 valid arguments (from *A to A[9] ). The notation makes programs more informative, helping compilers to both optimize the code generated for the function foo and to check that it is called correctly at each call site. In the C99 standard

Return value of a boolean expression in C

假装没事ソ 提交于 2019-12-01 10:47:25
For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g. int foo () { return (bar > 5); } The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do: if (foo() == TRUE) { /* do stuff */ } I know that the best option would be to simply do if (foo()) but you never know. Is there a defined standard value for boolean expressions or is it up to the compiler? If there is, is the standard value something included in C99? what about C89? An operator such as == , !=

Clarification on integer constant expressions

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 08:47:00
Somewhere I've read that integer constant expressions consists integer constants such as: (5 + 5) //integer constant expression That was the only example I have seen. Now, from standard which says: (C99 6.6/6) An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as