c11

Compile time check against multiple types in C?

房东的猫 提交于 2019-12-02 05:58:40
Currently I have a macro to check a value is a type. #define CHECK_TYPE_INLINE(val, type) \ ((void)(((type)0) != (0 ? (val) : ((type)0)))) This is useful to be able to type-check macro args in some cases. But what if I were to check against multiple types? for example, to check if it's a struct Foo * or struct Bar * . Example, static inline _insert_item(struct List *ls, void *item) { /* function body*/ } /* type-checked wrapper */ #define insert_item(ls, item) \ (CHECK_TYPE_ANY(item, struct Foo *, struct Bar *), \ _insert_item(ls, item)) Is there some good way to do this? Since this is tagged

_Thread_local storage class specifier in C?

五迷三道 提交于 2019-12-02 01:28:48
问题 I read a note in the book C How to Program 7th about some new standard C storage class named _Thread_local : The new C standard adds storage class specifier _Thread_local , which is beyond this book's scope. I looked for it in Google and here but nothing show up. Could someone please provide me some link about it? 回答1: Variables marked with _Thread_local are given "thread" storage duration -- that is, they are allocated when a thread begins, and deallocated when the thread ends. Such

Visual Studio 2017 does not supportC11 new feature _Generic

天大地大妈咪最大 提交于 2019-12-02 00:27:23
Can anybody advise why Visual Studio 2017 does not support the C11 new feature _Generic ? I found it is a very useful feature but cannot used in Visual Studio 2017. Below is the sample code: #include <stdio.h> #define MYTYPE(X) _Generic((X),\ int:"int",\ float:"float",\ double:"double",\ default:"other") int main(void) { int d = 5; printf("%s\n", MYTYPE(d)); printf("%s\n", MYTYPE(2.0*D)); return 0; } The complier will give below warnings and errors: 1>------ Build started: Project: Project1, Configuration: Debug Win32 ------ 1>predef.c 1>c:\users\mia\documents\c\listing 16.13\project1\project1

Does isspace() accept getchar() values?

*爱你&永不变心* 提交于 2019-12-01 23:56:57
问题 isspace() works if the input is representable as unsigned char or equal to EOF . getchar() reads the next character from stdin. When getchar()!=EOF ; are all getchar() returned values representable as unsigned char ? uintmax_t count_space = 0; for (int c; (c = getchar()) != EOF; ) if (isspace(c)) ++count_space; May this code lead to the undefined behavior? 回答1: According to C11 WG14 draft version N1570: §7.21.7.6/2 The getchar function is equivalent to getc with the argument stdin. §7.21.7.5

Does each branch of c11 _Generic generic association's result expressions have to be valid?

江枫思渺然 提交于 2019-12-01 23:45:30
I can't seem to pass the arguments to functions expecting different arguments (or to other _Generic macros which implement a subset of the types of the first one). #define DEBUG_PRINT(x,...) _Generic((x), \ debug_print_options *: DEBUG_PRINT_CUSTOM_TYPE(x, __VA_ARGS__), \ default: DEBUG_PRINT_BASIC_TYPE(x, __VA_ARGS__)) #define DEBUG_PRINT_BASIC_TYPE(x,...) debug_print_printf_specifier((#x), (x), TYPE_TO_PRINTF_SPECIFIER(x), __FILE__, __LINE__, _my_func__, &((struct debug_print_options){__VA_ARGS__})) #define DEBUG_PRINT_CUSTOM_TYPE(x,...) debug_print_custom_to_debug_string((#x), (x), GET

Does C11 allow variable declarations at any place in a function?

扶醉桌前 提交于 2019-12-01 22:06:58
Does the C11 standard (note I don't mean C++11) allow you to declare variables at any place in a function? The code below is not valid in ANSI C (C89, C90): int main() { printf("Hello world!"); int a = 5; /* Error: all variables should be declared at the beginning of the function. */ return 0; } Is it valid source code in C11? Yes. This was already valid in C99 (see the second bullet here ). More or less. C99 introduced the ability to declare variables part way through a block and in the first section of a for loop, and C2011 has continued that. void c99_or_later(int n, int *x) { for (int i =

Can unverified scanf call cause an undefined behavior?

谁说胖子不能爱 提交于 2019-12-01 21:30:01
Does below snippet invoke undefined behavior in case of an error? #include <stdio.h> int main() { int i; /* Indeterminate */ if (scanf("%d", &i) == 1) /* Initialize */ printf("%d\n", i); /* Success! Print read value */ else printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */ return 0; } What if scanf fails, is an uninitialized variable accessed? EDIT Moreover what if I replace scanf("%d", &i) with my_initializer(&i) : int my_initializer(int *pi) { double room_temp_degc = get_room_temp_in_degc(); if(room_temp_degc < 12.0) { // Cool *pi = 42; return 1; } else { return 0; } } In C90

Does isspace() accept getchar() values?

徘徊边缘 提交于 2019-12-01 21:24:17
isspace() works if the input is representable as unsigned char or equal to EOF . getchar() reads the next character from stdin. When getchar()!=EOF ; are all getchar() returned values representable as unsigned char ? uintmax_t count_space = 0; for (int c; (c = getchar()) != EOF; ) if (isspace(c)) ++count_space; May this code lead to the undefined behavior? According to C11 WG14 draft version N1570 : §7.21.7.6/2 The getchar function is equivalent to getc with the argument stdin. §7.21.7.5/2 The getc function is equivalent to fgetc ... §7.21.7.1/2 [ !=EOF case] ...the fgetc function obtains that

Is this a C11 anonymous struct?

老子叫甜甜 提交于 2019-12-01 17:24:42
I was looking into the C11 draft and it says An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. So I constructed the following testcase // struct type with no tag typedef struct { unsigned char a; unsigned char b; // ... Some other members ... unsigned char w; } AToW; union { AToW; // <- unnamed member unsigned char bytes[sizeof(AToW)]; } myUnion; Clang and GCC both complain

Is this a C11 anonymous struct?

馋奶兔 提交于 2019-12-01 17:08:48
问题 I was looking into the C11 draft and it says An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. So I constructed the following testcase // struct type with no tag typedef struct { unsigned char a; unsigned char b; // ... Some other members ... unsigned char w; } AToW; union {