c99

Array as compound literal [duplicate]

牧云@^-^@ 提交于 2019-11-30 22:46:30
This question already has an answer here: Why are compound literals in C modifiable 1 answer In C99 we can use compound literals as unnamed array. But are this literals constants like for example 100 , 'c' , 123.4f , etc. I noticed that I can do: ((int []) {1,2,3})[0] = 100; and, I have no compilation error and is guessable that the first element of that unnamed array is modified with 100. So it seems as array as compound literal are lvalue and not constant value. It is an lvalue, we can see this if we look at the draft C99 standard section 6.5.2.5 Compound literals it says ( emphasis mine ):

Function overloading in C using GCC - compiler warnings

拟墨画扇 提交于 2019-11-30 19:18:57
I am attempting to implement function overloading in C , and I am very close. I am using C99 so the _Generic keyword introduced in C11 is not available to me. I have developed some working code, but when I compile it I get a couple warnings. Working example: #include <stdio.h> #define print(x) \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int ), print_int(x) , \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char[]), print_string(x), \ (void)0)) void print_int(int i) { printf("int: %d\n", i); } void print_string(char* s) { printf("char*: %s\n", s); } int main

Convert C99 code to C89

陌路散爱 提交于 2019-11-30 18:36:29
How can I convert c99 source code automatically to c89? I want to compile c99 libraries with Visual C++, but MSVC only supports c89. Many changes are only syntactic, such as struct initializers, and you could write a tool to "de-c99" code automatically. Does this preprocessor exist? Clang-based source-to-source translator: https://github.com/libav/c99-to-c89/ The commercial Comeau C/C++ compiler can do this. Alternatively, use a proper C compiler (eg GCC or Clang via MinGW, Pelles C, Intel) and link the resulting object files. However, not all of these (in particular MinGW) support Microsoft's

C99 remove stricmp() and strnicmp()?

眉间皱痕 提交于 2019-11-30 18:36:18
问题 Is the functions stricmp() and strnicmp() removed in C99? I always get warning implicit declaration of funtion stricmp() (and also strnicmp() ) when I try to compile it against C99. For example, the simple code below get me that warning. #include<string.h> #include<stdio.h> char arr[100]="hello"; char arr2[100]="hEllo"; int main() { int n=-1; printf("%d\n",n); n=strnicmp(arr,arr2,3); // the same when use the function stricmp(); printf("%d\n",n); getchar(); return 0; } When I try to compile

How to “simulate” C99 in Visual Studio for variables declaration

感情迁移 提交于 2019-11-30 18:32:00
I'm using Visual Studio 2012 to develop simple Win32 C programs. I know that the VS compiler only supports C89, but I'd like to know if there is a way to override this limitation. In particular I'd like to declare variables anywhere in my code, instead of only at the beginning of scope blocks (as C89 requires). Thanks in advance. The choices I see: stick with MSVC and switch to C++ stick with MSVC and use a precompiler that translates C99 to C90 ( Comeau , c99-to-c89 ) switch to a toolchain that supports more recent revisions of the C language (Intel, MinGW, Clang, Pelles-C,...) This seems

C type casts and addition precedence

寵の児 提交于 2019-11-30 17:27:44
What's the precedence in the next expression? item = (char*)heap + offset; Is it (char*)(heap + offset) or ((char*)heap) + offset ? Jacob Cast trumps binary addition according to the precedence table. It's ((char *)heap) + offset . Casts have much higher precedence than addition. ((char*)heap) + offset 来源: https://stackoverflow.com/questions/3354446/c-type-casts-and-addition-precedence

Sequence points and side effects: Quiet change in C11?

旧城冷巷雨未停 提交于 2019-11-30 13:48:40
问题 C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 72) Furthermore, the prior value shall be read only to determine the value to be stored. 73) with the footnotes 72) A

Can “sizeof(arr[0])” lead to undefined behavior?

a 夏天 提交于 2019-11-30 13:45:40
问题 There is a well known pattern of figuring out array length: int arr[10]; size_t len = sizeof(arr) / sizeof(arr[0]); assert(len == 10); This pattern applies to static arrays and auto arrays of constant size. It also applies to variable length arrays in C99. I want to apply similar idea for figuring out dynamic array size in bytes: size_t known_len = 10; int *ptr = malloc(known_len * sizeof(int)); size_t size = known_len * sizeof(ptr[0]); assert(size == known_len * sizeof(int)); This is nicer

Are there any existing C implementations having padding bit in (un)signed integer representation?

♀尐吖头ヾ 提交于 2019-11-30 13:01:38
问题 As per C99, there maybe padding bits in signed int or unsigned int representation . So I wonder are there still any implementations having such outdated things? 回答1: Quoting the C99 rationale (PDF) section 6.2.6.2 §20: Padding bits are user-accessible in an unsigned integer type. For example, suppose a machine uses a pair of 16-bit shorts (each with its own sign bit) to make up a 32-bit int and the sign bit of the lower short is ignored when used in this 32-bit int . Then, as a 32-bit signed

Understanding restrict qualifier by examples

大兔子大兔子 提交于 2019-11-30 11:31:17
The restrict keyword's behavior is defined in C99 by 6.7.3.1: Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T. If D appears inside a block and does not have storage class extern, let B denote the block. If D appears in the list of parameter declarations of a function definition, let B denote the associated block. Otherwise, let B denote the block of main (or the block of whatever function is called at program startup in a freestanding environment). In what follows, a pointer expression E is said to be