c89

Variable declaration placement in C

I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C? The following code compiles successfully with "gcc -std=c89" and "gcc -ansi": #include <stdio.h> int main() { int i;...

Are prototypes required for all functions in C89, C90 or C99?

To be truly standards-compliant, must all functions in C (except for main) have a prototype, even if they are only used after their definition in the same translation unit? ...

Does floor() return something that's exactly representable?

In C89, floor() returns a double. Is the following guaranteed to work? double d = floor(3.0 + 0.5); int x = (int) d; assert(x == 3); My concern is that the result of floor might not be exactly representable in IEEE 754. So d gets something like 2.99999, and x ends up being 2. For the answer to this question to be yes, all integers ...

Why can't gcc find the random() interface when -std=c99 is set?

I do "#include <stdlib.h>" at the top of the source. Example compilation: /usr/bin/colorgcc -std=c99 -fgnu89-inline -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8 f8.c In file included from f8.c:7: ctype-cmp.c: In function ‘randomized’: ctype-cmp.c:48: warning: implicit declaration of function ‘ra...

Better way to forward declare typedef'd structures in C89?

struct SomeStruct; typedef struct SomeStruct SomeStruct; The above works, but is there a simpler (or better) way? ...

How to create VS solution for existing c project so that VS picks new files automatically?

I would like to port an embedded application to x86 to create an emulator. We decided to give VS2008 a try but I'm not sure how to make vs to understand our existing directory structure: arch/ arch/hcs08 -- HAL implementation arch/include -- public interface of HAL arch/x86 -- here should goes the emulation layer os/ os/src -- sour...

Recursive declaration of function pointer in C

I'd like to declare a function that returns a pointer to a function of the same type. I would like to use it to implement state machines like the one below: typedef event_handler_t (*event_handler_t)(event_t*); // compilation error event_handler_t state2(event_t* e); event_handler_t state1(event_t* e) { switch(e->type) { //... cas...

Is there any reason to use C instead of C++ for embedded development?

Question I have two compilers on my hardware C++ and C89 I'm thinking about using C++ with classes but without polymorphism (to avoid vtables). The main reasons I’d like to use C++ are: I prefer to use “inline” functions instead of macro definitions. I’d like to use namespaces as I prefixes clutter the code. I see C++ a bit type safe...

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: <MAGIC><LENGTH><BINARY DATA><CRC> but I don’t want to reinvent the wheel. Please note that: I'm thinking about quite restricted device: 4kb of RAM, no kernel, nor standard C lib. Can you think...

Increase a struct pointer with half the struct size

I just got an interesting problem to take care of, and I see no neat way to solve it. I have two base data structures that represents a complex graph, declared something like this: typedef struct _node_t node_t; typedef struct _graph_t graph_t; struct { /* Data fields omitted */ node_t * pNextByLevel; node_t * pNextByProx...

What techniques/strategies do people use for building objects in C (not C++)?

I am especially interested in objects meant to be used from within C, as opposed to implementations of objects that form the core of interpreted languages such as python. ...

Printing out hex values of a char* array in C gives odd values for binary input.

Here's an odd problem that's been stumping me for a bit. The program is written in C89, and it reads a file into a char* array 16 bytes at a time (using fread and a size of sizeof(char)). The file is fopen'd with the "rb" flags. The array is then passed into a function that basically takes the 16 hex values and sticks it into a string, ...

What C99 features are considered harmful or unsupported.

I usually write C code in C89, now some features of C99 (like intxx_t or __VA_ARGS__ or snprintf) are very useful, and can be even vital. Before I more my requirements from C89 to C99 I wanted to know which of C99 features were widely supported and which ones were not widely supported or even considered harmful. I know we could just ch...

compiling with c89 and c99 does gcc 4.4.2 default to c99?

Hello, I am using gcc 4.4.2 on linux I am just wondering does gcc automatically default to compiling with c99 as its the latest standard? How can I specify if I want to compile with c89 or c99? Many thanks for any advice, ...

c99/c89 standards vs. c coding practice

What code have you written in c that is not defined by either c89 or c99 standards? Specifically, I am looking for techniques like using pointer manipulation to move within a struct instead of using the member operator. This works fine on most compilers, but the compiler is free to add buffer space between struct members, so will not a...

Assertions in C89?

I'm writing C89 on MSFT Visual Studio 2010 Beta. How can I make an assertion, similar to Java's assert keyword? I think I need to define a macro, but I'm not sure how. (It seems like this is something that's been done before, so I'd rather use that than try to roll my own.) Here's a guess: int assert(int truth_value) { // crash the ...

Portable user defined character class division in C89 by a lookup table, would you do this?

static const int class[UCHAR_MAX] = { [(unsigned char)'a'] = LOWER, /*macro value classifying the characters*/ [(unsigned char)'b'] = LOWER, . . . } This is just an idea. Is it a bad one? ...

Static initialization in C

I want to have code that looks something like this... static linked_list* globalListHoldingAllSortsOfGoodies = initialize_linked_list(); /* [In a different file...] */ static int placeholder = add_to_global_list(goodies); But non-constant initialization is impossible in C. Is there some way to get the same affect without breaking C8...

C89: Any beginner mistakes in this code?

I am new to C. (I'm using C89 on Visual Studio 2010.) I wrote a little program that uses a tree to give a histogram of arguments presented to the program. Are there any obvious beginner mistakes I've make? Everything appears to run fine in my extremely limited testing. hist.c #include "tree.h" #include <stdlib.h> #include <stdio.h> in...

C89: signed/unsigned mismatch

Are signed/unsigned mismatches necessarily bad? Here is my program: int main(int argc, char *argv[]) { unsigned int i; for (i = 1; i < argc; i++) { // signed/unsigned mismatch here } } argc is signed, i is not. Is this a problem? ...