c11

error C4996: 'scanf': This function or variable may be unsafe in c programming

痞子三分冷 提交于 2019-11-26 22:18:15
问题 I have created a small application to find max number by using user-defined function with parameter. When I run it, it shows this message Error 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. What do I do to resolve this? This is my code #include<stdio.h> void findtwonumber(void); void findthreenumber(void); int main() { int n; printf("Fine Maximum of two number\n

What is the default C mode for the current gcc (especially on Ubuntu)?

走远了吗. 提交于 2019-11-26 21:58:20
When I ask to see the current version of cc I get this. $ cc --version cc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 Copyright (C) 2012 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ What I would like to know is which of c89, c90, c99 or c11 is being used. This is explained in depth in the gcc manual, available (if it's installed) by typing info gcc or online here . The relevant section of the 4.7.2 manual is here . By default, gcc does not conform to any of the

Dynamic array allocation on stack in C

房东的猫 提交于 2019-11-26 21:39:48
问题 I just did a experiment yesterday, and find something confusing: #include <stdio.h> int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the array arr on the stack. The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible? 回答1: Variable length arrays were added to C99. It's described in the C99 rationale: 6.7.5.2 Array

Reading a character with scanf_s

橙三吉。 提交于 2019-11-26 21:07:52
I was just messing around with C and ran into this small problem. As you can see from my output I getting '╠' this character. #include <stdio.h> int main(void) { char c; printf("Do you want to be X's or O's?\n"); scanf_s("%c", &c); printf("You chose %c\n", c); } See program output You are misusing scanf_s() . Microsoft compilers may warn you to use their secure extensions (aka c11 annex k). But, be careful if you do so. scanf_s() is not a direct replacement for scanf() . In this case you have to pass the size of the output buffer as an extra argument. char c; scanf_s("%c", &c, 1); Having to

Operator precedence table for the C programming language

馋奶兔 提交于 2019-11-26 21:07:04
What would a correct operator precedence table that lists all operators in the C language look like? I have made extensive searches on the web, and found many such precedence tables. Alas, I haven't found a single one filling these requirements: Lists all operators in the C language as defined in ISO 9899:2011, without mixing in any C++ operators. Lists the operators in the complete and correct priority order. Explanation Prec. denotes operator precedence , where group 1 has the highest precedence and group 17 the lowest. Assoc. denotes operator associativity , where such is applicable.

Why does auto a=1; compile in C?

北慕城南 提交于 2019-11-26 19:03:16
问题 The code: int main(void) { auto a=1; return 0; } gets compiled without errors by the MS Visual Studio 2012 compiler, when the file has the .c extension. I have always thought that when you use the .c extension, compilation should be according to the C syntax, and not C++. Moreover, as far as I know auto without a type is allowed only in C++ since C++11, where it means that the type is deduced from the initializer. Does that mean that my compiler isn't sticking to C, or is the code actually

Does &((struct name *)NULL -> b) cause undefined behaviour in C11?

泄露秘密 提交于 2019-11-26 18:48:01
Code sample: struct name { int a, b; }; int main() { &(((struct name *)NULL)->b); } Does this cause undefined behaviour? We could debate whether it "dereferences null", however C11 doesn't define the term "dereference". 6.5.3.2/4 clearly says that using * on a null pointer causes undefined behaviour; however it doesn't say the same for -> and also it does not define a -> b as being (*a).b ; it has separate definitions for each operator. The semantics of -> in 6.5.2.3/4 says: A postfix expression followed by the -> operator and an identifier designates a member of a structure or union object.

C11 _Generic: how to deal with string literals?

旧巷老猫 提交于 2019-11-26 16:40:16
问题 Using the _Generic feature in C11, how do you deal with string literals? For instance: #include <stdio.h> #define foo(x) _Generic((x), char *: puts(x)) int main() { foo("Hello, world!"); return 0; } gives this error on clang: controlling expression type 'char [14]' not compatible with any generic association type Replacing char * with char[] gives me error: type 'char []' in generic association incomplete The only ways (to my knowledge) of getting this to compile are: Cast the string literal

Syntax and Sample Usage of _Generic in C11

♀尐吖头ヾ 提交于 2019-11-26 15:08:14
I heard C11 added generics. I've googled a bit, looked at some articles, understood there's a new keyword ( _Generic ) and all. But I can't seem to grasp it all. Is it something like the generics in C# or templates in C++? Can anyone give me a brief explanation of the C11 definition of generics, its syntax and a simple sample usage example? unwind This is a pretty good introduction. Here's the Overview: Generic selection is implemented with a new keyword: _Generic. The syntax is similar to a simple switch statement for types: _Generic( 'a', char: 1, int: 2, long: 3, default: 0) evaluates to 2

Incompatible pointer types passing in _Generic macro

左心房为你撑大大i 提交于 2019-11-26 12:19:56
问题 The following code generates 2 warnings which are described in the question\'s title. #include <stdio.h> static void _print_f(float *f){printf(\"float : %f\\n\", *f);} static void _print_i(int *i) {printf(\"int : %d\\n\", *i);} #define print(num) _Generic((num), \\ int* : _print_i(num), \\ float* : _print_f(num)) int main(void) { print((&(int){10})); print((&(float){10.f})); return 0; } OUTPUT: int : 10 float : 10.000000 I know, this macro could be written like the following: #define print