c89

How to get SIZE_MAX in C89

别等时光非礼了梦想. 提交于 2019-12-01 18:38:08
I'm trying to get SIZE_MAX in C89. I thought of the following way to find SIZE_MAX : const size_t SIZE_MAX = -1; Since the standard (§6.2.1.2 ANSI C) says: When a signed integer is converted to an unsigned integer with equal or greater size, if the value of the signed integer is nonnegative, its value is unchanged. Otherwise: if the unsigned integer has greater size, the signed integer is first promoted to the signed integer corresponding to the unsigned integer; the value is converted to unsigned by adding to it one greater than the largest number that can be represented in the unsigned

`clang -ansi` extensions

不羁的心 提交于 2019-12-01 18:33:27
I ran into an issue recently where the following toy example compiles cleanly using clang -ansi : int main(void) { for (int i = 0; 0; ); return i; } but gcc -ansi gives the following error: a.c: In function ‘main’: a.c:3:5: error: ‘for’ loop initial declarations are only allowed in C99 mode a.c:3:5: note: use option -std=c99 or -std=gnu99 to compile your code Compiling with clang -ansi -pedantic shows that a C99 extension is being used. a.c:3:10: warning: variable declaration in for loop is a C99-specific feature [-pedantic,-Wc99-extensions] for (int i = 0; 0; ); ^ 1 warning generated. What

Can enum member be the size of an array in ANSI-C?

与世无争的帅哥 提交于 2019-12-01 17:09:53
问题 I need to allocate an array according to how many elements the enum have. I did the following: enum { A, B, C, LAST }; char buf[LAST]; That works fine,even with -ansi -pedantic flags. But I'm not sure if it's a GCC or clang(wich supports most,if not all GCC-extensions) extensions or really allowed by the ANSI C standard and will works fine in any C compiler with ANSI-C std. Can someone clarify it? 回答1: Both the C89 (section 3.5.2.2) and C99 (section 6.7.2.2) standards define enums the same

Return value of a boolean expression in C

假装没事ソ 提交于 2019-12-01 10:47:25
For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g. int foo () { return (bar > 5); } The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do: if (foo() == TRUE) { /* do stuff */ } I know that the best option would be to simply do if (foo()) but you never know. Is there a defined standard value for boolean expressions or is it up to the compiler? If there is, is the standard value something included in C99? what about C89? An operator such as == , !=

Return value of a boolean expression in C

余生颓废 提交于 2019-12-01 08:42:25
问题 For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g. int foo () { return (bar > 5); } The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do: if (foo() == TRUE) { /* do stuff */ } I know that the best option would be to simply do if (foo()) but you never know. Is there a defined standard value for boolean expressions or is it up to the compiler? If

Returning the terminal cursor to start-of-line with wrapping enabled

你。 提交于 2019-12-01 03:51:26
I'm writing a filter (in a pipe destined for a terminal output) that sometimes needs to "overwrite" a line that has just occurred. It works by passing stdin to stdout character-by-character until a \n is reached, and then invoking special behaviour. My problem regards how to return to the beginning of the line. The first thing I thought of was using a \r or the ANSI sequence \033[1G . However, if the line was long enough to have wrapped on the terminal (and hence caused it to scroll), these will only move the cursor back to the current physical line. My second idea was to track the length of

mixed declarations and codes

早过忘川 提交于 2019-12-01 03:28:50
When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c" ,gcc emits no error.(can you look a line which starts with char ....,in if loop,) static void remove_negation(char *s,char *s1) { char **cmainp=malloc(sizeof(char*)*1); int len=0;int d=0; int i=0; cmainp[0]=malloc(sizeof(char)*300); len=strlen(s); for(i=0;i<len;++i) { if(s[i]=='-') if(i==0 || s[i-1]==',') /*look*/ {char *p=malloc(sizeof(char)*3); /*look*/ ++i; p[0]=s[i]; p[1]='\0'; strcat(s1,","); strcat(s1,p); free(p); continue; } cmainp[0][d]=s[i]; ++d; } cmainp[0][d+1]='\0'; strcpy(cmainp[0],s); free(cmainp[0]);

Adding or assigning an integer literal to a size_t

こ雲淡風輕ζ 提交于 2019-12-01 03:14:44
In C I see a lot of code that adds or assigns an integer literal to a size_t variable. size_t foo = 1; foo += 1; What conversion takes place here, and can it ever happen that a size_t is "upgraded" to an int and then converted back to a size_t ? Would that still wraparound if I was at the max? size_t foo = SIZE_MAX; foo += 1; Is that defined behavior? It's an unsigned type size_t which is having a signed int added to it (that may be a larger type?) and the converted back to a size_t . Is there risk of signed integer overflow? Would it make sense to write something like foo + bar + (size_t)1

Order of expression evaluation in C

流过昼夜 提交于 2019-11-30 21:29:20
If I have the following expression: c = (a) * (b) What does the C90 standard say about the order evaluation of the subexpression 'a' and 'b'? There is no specified order since the multiplication operator is not a sequence point. Sequence points include the comma operator, the end of a full expression, and function calls. Thus the order of evaluation of (a) and (b) is up to the compiler implementation. Therefore you shouldn't attempt to-do something in (a) that would have a side-effect that you want to be seen in (b) in order to generate a valid result. For instance: int a=5; int b = (a++) * (a

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