c89

C89 vs c99 GCC compiler

Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two? #include <stdio.h> int main () { // Print string to screen. printf ("Hello World\n"); } gcc -o helloworld -std=c99 helloworld.c vs gcc -o helloworld -std=c89 hellowor...

C: Trouble with Strings

I am new to C89, and don't really understand how strings work. I am developing on Windows 7. Here is what I am trying to do, in Java: String hostname = url.substring(7, url.indexOf('/')); Here is my clumsy attempt to do this in C89: // well formed url ensured void get(char *url) { int hostnameLength; char *firstSlash; ch...

C89: Need to declare functions before referncing them?

I'm new to C89, and it appears that you must declare a function before calling it. So this is unacceptable: void foo() { bar(); } void bar() { // do stuff } Because bar() is defined after foo() in the file. Is there any way to get around this? Do I need a header file? ...

C89: getaddrinfo() on Windows?

I'm new to C89, and trying to do some socket programming: void get(char *url) { struct addrinfo *result; char *hostname; int error; hostname = getHostname(url); error = getaddrinfo(hostname, NULL, NULL, &result); } I am developing on Windows. Visual Studio complains that there is no such file if I use these incl...

Passing structs by pointer in C89

I am working with a C89 compiler and I'm coming across some pointer typing error. Calling code: struct cpu_state_type cpu_state; //Stuff here.... foo() { print_out_cpu(&cpu_state); } Print_out_cpu is defined elsewhere and the H file is #included in. struct cpu_state_type { int r[12]; }; void print_out_cpu(struct cpu_state_type...

Why didn't C have a boolean data type prior to C99?

I realise you can just #define some integers, but why didn't C have a dedicated boolean data type before C99? It's such a common occurence in programming and logic, I don't understand the absense of an explicit type and notation. ...

Recommended Clang command line options

The Manual for Clang seems to be work in progress, so could you help me formulate the definitive command line options for compiling ANSI-C (AKA C89, C90) with maximum strictness and relevant/helpful warnings? Clang is a compiler front end for the C, C++, and Objective-C programming languages. It uses the Low Level Virtual Machi...

C variable declarations after function heading in definition

When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations that followed the "function heading" in the definition. Is this valid in ISO C (C99)? when should this be done in production code instead of just declaring the variables within the "function heading?" Why is it being done here? I refer to...

VS 2008 and C99

Hi, I read with interest the post "How universally is C99 supported ?". One of the comments therein points that Microsoft doesn't support C99. But the comment symbol // works with VS 2008 and this symbol is in C99. I have two questions: To what extent VS 2008 support C99? Is it ok in the same code to mix C89 and C99 syntax together? So...

Syntax error while copying an multidimensional array to another in C

We are programming a ST269 microcontroller which has two IR distance sensors. To calibrate these sensors we made one table for each sensor with the distance we measured and the corresponding value we get from the ADC. Now we want to use one function to approximate the values in between. So we defined two two-dimensional arrays (one for ...

How do I know whether c89 or c99 is applied by cl.exe?

I just came across this term in this post, how do I check which rule is actually used by my compiler? Or is it possible to specify a rule for cl.exe? ...

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 code and variable definitions à la C++/C99. So I need to find a way to prevent developers ...

Use variadic functions in C89 without passing number of arguments or a final argument?

Let's say I have a variadic function foo(int tmp, ...), when calling foo function I need to know how many arguments there are. I'm aware of two ways of finding out how many arguments there are: Use a final argument when calling foo, like -1, so your function call will be like this: foo(tmp, 1, 2, 9, -1) and when you are inside foo and...

A good C equivalent of STL vector?

I've noticed that at several places in our code base we use dynamically expanding arrays, i.e. a base array coupled with an element counter and a "max elements" value. What I want to do is replace these with a common data structure and utility functions, for the usual object-oriented reasons. The array elements can be either basic dat...

Does either ANSI C or ISO C specify what -5 % 10 should be?

I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? ...

Is the "struct hack" technically undefined behavior?

What I am asking about is the well known "last member of a struct has variable length" trick. It goes something like this: struct T { int len; char s[1]; }; struct T *p = malloc(sizeof(struct T) + 100); p->len = 100; strcpy(p->s, "hello world"); Because of the way that the struct is laid out in memory, we are able to overlay ...

Forward declare FILE *

How do I forward declare FILE * in C? I normally do this using struct MyType;, but naturally this doesn't appear to be possible. If behaviour differs between C standards or compilers and with C++, this is also of interest. Update0 Why I want to do this aside: What I'm asking is how to forward declare a non-struct/"typedef'd struct" ty...