c99

scanf not to exceed buffer overrun

有些话、适合烂在心里 提交于 2019-12-08 02:58:12
问题 I have a buffer and I don't want the user to enter more characters than the buffer can hold (to avoid a buffer overrun). I am using scanf and have done like this: char buffer[30] = {'\0'}; scanf("%30s", buffer); However, I know I am protected if the user enters more than 30. However, if the user enters more than 30, will the buffer be null terminated? 回答1: scanf() with a "%s" conversion specifier adds a terminating null character to the buffer. But , you're asking for 30 characters, which

ARC warning: Implicit declaration of function 'DLog' is invalid in C99

扶醉桌前 提交于 2019-12-08 01:36:05
问题 I use DLog macro in an ARC project, and I got the warning: Implicit declaration of function 'DLog' is invalid in C99 You can find DLog from http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog How to fix this warning? 回答1: I actually had this warning when calling Dlog without a capital L. I changed it into DLog and everything worked fine. But you seem to have it correct. 回答2: Make sure your Prefix.pch file is included in 'Build Settings' -> 'Prefix Header' 来源:

Bitwise negation of unsigned char

岁酱吖の 提交于 2019-12-07 12:48:18
问题 This is a question relating the c99 standard and concerns integer promotions and bitwise negations of unsigned char. In section 6.5.3.3 it states that: The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E. Am I understanding that correctly when I say that, that means that: unsigned int ui = ~ (unsigned char) ~0; // ui is now

Is `uint_fast32_t` guaranteed to be at least as wide as `int`?

笑着哭i 提交于 2019-12-07 09:22:56
问题 The C standard specifies that integer operands smaller than int will be promoted to int before any arithmetic operations are performed upon them. As a consequence, operations upon two unsigned values which are smaller than int will be performed with signed rather than unsigned math. In cases where it is important to ensure that operation on 32-bit operands will be performed using unsigned math (e.g. multiplying two numbers whose product could exceed 2⁶³) will use of the type uint_fast32_t be

Are C unions never padded at the beginning?

匆匆过客 提交于 2019-12-07 07:05:06
问题 Are there any guarantees in the C99 standard that unions will only ever be padded at the end like structs? And relatedly, will the address of the union always be equal to the address of any of its possible members? 回答1: Yes. As you note, structures never have leading padding. The address of a union always refers to the first element of any component of the union (with suitable casts), so there can't be any padding at the start of a union either. Yes. Suitably cast, the address of a union is

What is the state of C99 support in major compilers / toolchains?

大兔子大兔子 提交于 2019-12-06 23:01:24
问题 A response to a comment I made here made me stop and think: "I don't really know what the state of C99 support is." Wikipedia gives details for a few compilers, but I'm not familiar enough with C99 to know all the bits and pieces of the standard, so I'm looking for a gestalt overview answer to the question: What is the state of C99 support in major compilers / toolchains? 回答1: MSVC: Intentionally not implemented unless it overlaps with C++ GCC: Most of the useful parts are in (and have been

What does (int (*)[])var1 stand for?

六月ゝ 毕业季﹏ 提交于 2019-12-06 22:40:12
问题 I found this example code and I tried to google what (int (*)[])var1 could stand for, but I got no usefull results. #include <unistd.h> #include <stdlib.h> int i(int n,int m,int var1[n][m]) { return var1[0][0]; } int example() { int *var1 = malloc(100); return i(10,10,(int (*)[])var1); } Normally I work with VLAs in C99 so I am used to: #include <unistd.h> #include <stdlib.h> int i(int n,int m,int var1[n][m]) { return var1[0][0]; } int example() { int var1[10][10]; return i(10,10,var1); }

Are global variables refreshed between function calls?

感情迁移 提交于 2019-12-06 21:26:08
问题 Im writing embedded firmware, and find it sometimes hard to decide when I need volatile or not. When I have a function that waits for some boolean flag to be changed by an interrupt, it's obvious that the flag needs to be volatile, because else the function would wait forever, since the compiler doesn't realise the value can be changed by the interrupt. But when I have a short function that just checks a flag in the first line, I would expect the flag doesnt need to be volatile, because its

Using restrict with arrays?

ぐ巨炮叔叔 提交于 2019-12-06 21:22:37
问题 Is there a way to tell a C99 compiler that the only way I am going to access given array is by using myarray[index] ? Say something like this: int heavy_calcualtions(float* restrict range1, float* restrict range2) { float __I promise I won't alias this__ tmpvalues[1000] = {0}; .... heavy calculations using range1, range2 and tmpvalues; .... } By using restrict I promised that I won't alias range1 and range2 but how do I do the same thing for array declared inside my function ? 回答1: Although

How to check that c99 is used, and not gnu99

浪子不回头ぞ 提交于 2019-12-06 16:39:45
I want to add a warning message during the compilation to warn the user it should use gnu99 instead of c99 (I am using anonymous struct, and it seems it doesn't exist at all in c99). I found that: #if __STDC_VERSION__ >= 199901L but this test is true for c99 and gnu99. Which predefined macro could I use? You can check for yourself: $ gcc -std=c99 -dM -E - < /dev/null > c99.txt $ gcc -std=gnu99 -dM -E - < /dev/null > gnu99.txt $ sdiff -s c99.txt gnu99.txt #define __STRICT_ANSI__ 1 < 来源: https://stackoverflow.com/questions/8656175/how-to-check-that-c99-is-used-and-not-gnu99