Questions tagged [c89]

This tag is for questions regarding the international standard ISO 9899:1990, also known as "C89", "C90" or "ANSI C", with amendments and technical corrigenda (as opposed to K&R C, C99, C11 or later C standard revisions).

The first C standard was released 1989 nationally in USA, by their national standard institute ANSI. This release is called C89 or ANSI-C. One year later, the American standard was accepted internationally and published by ISO (ISO 9899:1990). This release is called C90. Technically, it is the same standard as C89/ANSI-C, though formally, C90 replaced C89/ANSI-C, making them obsolete.

Always use the tag for all your C questions, then complement it with the tag for questions that are specific to this version of the standard.

643 questions
14
votes
3 answers

Problem trying to use the C qsort function

#include #include float values[] = { 4, 1, 10, 9, 2, 5, -1, -9, -2,10000,-0.05,-3,-1.1 }; int compare (const void * a, const void * b) { return ( (int) (*(float*)a - *(float*)b) ); } int main () { int i; qsort…
Luiz Fernando
  • 151
  • 1
  • 1
  • 3
14
votes
2 answers

Is casting a pointer to different structs guaranteed to be meaningful in C89?

I have two structs like struct X { int x; double *y; }; struct Y { int a; double *b; char c; }; Is casting a pointer to struct Y to a pointer to struct X guaranteed to behave consistently in a reasonable manner (i.e. x->x and x->y…
math4tots
  • 8,540
  • 14
  • 58
  • 95
14
votes
1 answer

Which section in C89 standard allows the "implicit int" rule?

While using gcc, the code: register a = 3; static b = 3; it is allowed while using the -std=c89 -pedantic-errors flags, although there is a warning. However it receive an error with the -std=c99 -pedantic-errors flags. I wonder which section of…
Victor
  • 1,303
  • 1
  • 10
  • 28
14
votes
5 answers

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

Consider following example: #include int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n",…
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
14
votes
2 answers

Should I use "-ansi" or explicit "-std=..." as compiler flags?

I've read that ANSI C is not exactly the same as ISO C and compilers may differ in interpretation of what "-ansi" is about. (gcc maps it to C90, clang maps it to C89) At the moment I would tend to use "-std=..." over "-ansi" as then it is explicitly…
math
  • 8,514
  • 10
  • 53
  • 61
13
votes
3 answers

How to enforce C89-style variable declarations in gcc?

I work on a code base which is mostly C with a little C++, and is mostly built with gcc but occasionally it needs to be built with MSVC. Microsoft's C compiler is still pretty much C89 with a few minor extensions, and it still doesn't support mixed…
Paul R
  • 208,748
  • 37
  • 389
  • 560
13
votes
3 answers

Is it undefined behavior to exceed translation limits and are there checker tools to find it?

ORIGINAL QUESTION: I'm searching the C90 standard for things to be aware of, when writing hignly portable code, while having low trust in the good will of the compiler vendor, and assuming that my software might kill somebody sometimes, if I do…
Mark A.
  • 579
  • 4
  • 13
13
votes
1 answer

Clang fails to throw error on non-constant array initializers in C89 mode

Is this a bug in Clang? The following code: #include int main(void) { int foo = 42; int bar[1] = { foo }; printf("%d\n", bar[0]); return 0; } Compiles fine using: clang -Wall -Wextra -Werror -pedantic -pedantic-errors…
user529758
13
votes
1 answer

Conforming variant of the old "struct hack" (?)

I believe I've found a way to achieve something like the well-known "struct hack" in portable C89. I'm curious if this really strictly conforms to C89. The main idea is: I allocate memory large enough to hold an initial struct and the elements of…
user529758
13
votes
2 answers

How can I get error message for errno value (C language)?

How can I get error message for errno value (C language)? For example, I can write such file (errno_messages.h): #include char* get_errno_message(void){ switch (errno) { case 0: return ""; break; case EPERM: …
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
12
votes
3 answers

A way to convert byte stream to packet stream in C89 on an embedded device

I’m working on with an embedded device that is connected to PC using rs232 (rs232 over USB). I’m thinking about developing my own protocol: but I don’t want to reinvent the wheel. Please note that: I'm thinking…
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
12
votes
4 answers

Why does this function return the correct length of a string? (Incrementing a char pointer)

This is a function that counts the number of characters in a string: int str_len(const char* s) { int i = 0; while(*(s++)) { i++; } return i; } Why does this return the correct length? Let's say I call this function with a…
lor
  • 123
  • 5
12
votes
1 answer

Workaround for "semicolon in global scope" warning for no-op C macro

In a codebase that can be built as either C or C++, I thought I'd make a macro to take advantage of static_assert in the case it's built as C++11 or higher. (Note: I know there are ways to do this in pre-C11 C, at least if you're willing to take a…
12
votes
3 answers

How to "simulate" C99 in Visual Studio for variables declaration

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,…
eang
  • 1,615
  • 7
  • 21
  • 41
11
votes
2 answers

Is it okay to longjmp before calling va_end?

In this Q&A it is established that you should always call va_end(): What exactly is va_end for? Is it always necessary to call it? But what if a piece of code longjmp's before you reach the va_end? Is there any promise on va_end's part that it will…