c99

How to find my current compiler's standard, like if it is C90, etc

我的梦境 提交于 2019-11-27 11:11:55
问题 I'm working on a Linux machine. Is there any system command to find the standard followed by the C compiler I'm using? 回答1: This is compiler dependent, I'm supposing you're using GCC. You could check your compiler defined macros using: gcc -dM -E - < /dev/null Check the manual about the flags, specially: __STDC_VERSION__ This macro expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This

Realistic usage of the C99 'restrict' keyword?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:58:48
I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer won't be used to point somewhere else. Can anyone offer some realistic cases where its worth actually using this? Michael restrict says that the pointer is the only thing that accesses the underlying object. It eliminates the potential for pointer aliasing, enabling better optimization by the compiler. For instance, suppose I have a machine with specialized instructions that can multiply vectors of

Setting alias for GCC in Windows PowerShell

半世苍凉 提交于 2019-11-27 09:49:35
I'm trying to set up a "gcc99" alias in Windows PowerShell which is equal to "gcc -std=C99 -pedantic -Wall". The idea is to use fewer keystrokes to ensure that GCC is running in c99 mode. (I've tried my best to adapt the guidelines in the following post to Windows PowerShell: Setting std=c99 flag in GCC ) When I try to compile after setting up such an alias (exhibit 1 below), I receive an error. As reference, I don't receive this error if I use the extended command to compile (exhibit 2 below). As a test, I tried setting gc99 as an alias for "gcc" (no additional values) and it worked fine

Why are compound literals in C modifiable

你说的曾经没有我的故事 提交于 2019-11-27 09:42:24
One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are completely modifiable (and locking at the generated machine code, you see they are pushed on the stack): char* str = (char[]){"Hello World"}; *str = 'B'; // A-Okay! I'm compiling with clang-703.0.29 . Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if it's modifiable? EDIT: An even shorter example would be: "Hello World"[0] = 'B'; // Bus Error! (char[])

Get warning when a variable is shadowed

主宰稳场 提交于 2019-11-27 09:15:30
I generally want to avoid code like this: #include <stdio.h> int main(int argc, char *argv[]){ int n = 3; for (int n = 1; n <= 10; n++){ printf("%d\n", n); } printf("%d\n", n); } How can I find such usage of variables? That means, that in the same function a "more local" variable has the same name as a more global variable? C-Standard : C 99 Both gcc and clang support the -Wshadow flag which will warn about variables that shadow one another. For example the warning I receive from gcc for your code is the following: warning: declaration of ‘n’ shadows a previous local [-Wshadow] for (int n = 1;

C, reading multiple numbers from single input line (scanf?)

≯℡__Kan透↙ 提交于 2019-11-27 08:59:16
I have written an app in C which expects two lines at input. First input tells how big an array of int will be and the second input contains values separated by space. For example, the following input 5 1 2 3 4 99 should create an array containing {1,2,3,4,99} What is the fastest way to do so? My problem is to read multiple numbers without looping through the whole string checking if it's space or a number? Thanks. int i, size; int *v; scanf("%d", &size); v = malloc(size * sizeof(int)); for(i=0; i < size; i++) scanf("%d", &v[i]); Remember to free(v) after you are done! Also, if for some reason

Enumeration object set to a value not equal to any of its respective enumeration constants

半城伤御伤魂 提交于 2019-11-27 07:48:23
问题 What value does an enumeration object have if it is set to a value not equal to any of its respective enumeration constants? Consider the following code: enum foobar{ FOO = 1, BAR = 5 }; enum foobar baz = 5; enum foobar qux = 42; The variable baz is set to the integer value 5 , while the variable qux is set to the integer value 42 . I suspect the variable baz will hold the value BAR , but I'm unsure about the variable qux . No enumeration constant was assigned the value 42 , so what happens

Where is <inttypes.h> in Visual Studio 2005?

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:45:52
问题 I'd like to use the C99 header file inttypes.h in a Visual Studio project (I'd like to printf 64 bit numbers). However, this file does not seem to exist in my install. Is this just not part of VS2005? Are there any alternatives? 回答1: It's at google. VS doesn't come with <inttypes.h> 回答2: No, it is not included in VS 2005. An alternative is Boost's implementation in the Boost::Integer library, specifically boost/cstdint.hpp 回答3: For Visual Studio 2005 see the bug "C99 header <inttypes.h>

Is unsigned char always promoted to int?

心已入冬 提交于 2019-11-27 07:44:52
问题 Suppose the following: unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar; Are foo and bar values guaranteed to be promoted to int values for the evaluation of the expression foo + bar -- or are implementations allowed to promote them to unsigned int ? In section 6.2.5 paragraph 8: For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange

Why is the fgets function deprecated?

折月煮酒 提交于 2019-11-27 07:43:14
From The GNU C Programming Tutorial : The fgets ("file get string") function is similar to the gets function. This function is deprecated -- that means it is obsolete and it is strongly suggested you do not use it -- because it is dangerous. It is dangerous because if the input data contains a null character, you can't tell. Don't use fgets unless you know the data cannot contain a null. Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message. Always use getline or getdelim instead of fgets