c89

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

时光怂恿深爱的人放手 提交于 2019-11-28 23:15:20
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 ‘random’ ctype-cmp.c: In function ‘main’: ctype-cmp.c:153: warning: implicit declaration of function ‘srandom’ ais@xcalibur:t$ When I turn off -std=c99, the function isfinite() can not be found. So I do want to use -std=c99 for this and other reasons. Is there some trick

How to compile for a freestanding environment with GCC?

荒凉一梦 提交于 2019-11-28 23:10:19
The code I'm working on is supposed to be possible to build for both hosted and freestanding environments, providing private implementations for some stdlib functions for the latter case. Can I reliably test this with just GCC on a normal workstation/build server? Compile for freestanding environment with GCC The "-ffreestanding" option looked promising, but it seems that it "only" disables built-ins and sets the STDC_HOSTED macro properly, it still provides all system headers. The option "-nostdinc" is too restrictive; I still want to use the headers required for a freestanding implementation

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 21:41:40
问题 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. 回答1: I tend to do something like this: struct foo_ops { void (*blah)(struct foo *, ...); void (*plugh)(struct foo *, ...); }; struct foo { struct foo_ops *ops; /* data fields for foo go here */ }; With these structure definitions, the code implementing foo looks something like this: static void plugh(struct foo *, ...) { ...

Can't get rid of “this decimal constant is unsigned only in ISO C90” warning

拜拜、爱过 提交于 2019-11-28 17:51:50
I'm using the FNV hash as a hashing algorithm on my Hash Table implementation but I'm getting the warning in the question title on this line: unsigned hash = 2166136261; I don't understand why this is happening because when I do this: printf("%u\n", UINT_MAX); printf("2166136261\n"); I get this: 4294967295 2166136261 Which seems to be under the limits of my machine... Why do I get the warning and what are my options to get rid of it? unsigned hash = 2166136261u; // note the u. You need a suffix u to signify this is an unsigned number. Without the u suffix it will be a signed number. Since

C check if file exists

浪子不回头ぞ 提交于 2019-11-28 11:59:48
In a project I have to do in C89 standard I have to check if a file exists. How do I do this? I thought of using FILE *file; if ((file = fopen(fname, "r")) == NULL) { printf("file doesn't exists"); } return 0; but I think there can be more cases then file doesn't exists that will do fopen == NULL. How do I do this? I prefer not using includes rather then . If you can't use stat() in your environment (which is definitely the better approach), just evaluate errno. Don't forget to include errno.h. FILE *file; if ((file = fopen(fname, "r")) == NULL) { if (errno == ENOENT) { printf("File doesn't

ANSI C (ISO C90): Can scanf read/accept an unsigned char?

旧时模样 提交于 2019-11-28 11:57:51
Simple question: Can scanf read/accept a "small integer" into an unsigned char in ANSI C? example code un_char.c: #include <stdio.h> #include <stdlib.h> int main(void) { unsigned char character; scanf("%hhu", &character); return EXIT_SUCCESS; } Compiled as: $ gcc -Wall -ansi -pedantic -o un_char un_char.c un_char.c: In function ‘main’: un_char.c:8: warning: ISO C90 does not support the ‘hh’ gnu_scanf length modifier hh isn't supported by ISO C90. So what scanf conversion can be used in this situation? Jonathan Leffler No: C89 (C90) does not support '%hhu' to read a string of digits into an

Forward declare FILE *

 ̄綄美尐妖づ 提交于 2019-11-28 09:06:36
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" type so that I can declare pointers to it. Obviously using void * and casting it in the source file is a bit hackish. You can't. The standard just states that FILE is "an object type capable of recording all the information needed to control a stream"; it's up to

What does (void)var actually do?

半城伤御伤魂 提交于 2019-11-28 08:24:08
Consider the following main() : int main(int argc, char *argv[]) { return (0); } Upon compilation with cc -Wall -Wextra , warnings saying "unused parameter" get generated. When I do not need to use a parameter in a function (for instance in a signal handler function that makes no use of its int parameter), I am used to doing the following: int main(int argc, char *argv[]) { (void)argc; (void)argv; return (0); } (For that particular main() , I sometimes see other people do: argv = argv - argc + argc ) But what does (void)var actually do ? I understand that (void) is a cast, so I guess I am

Casting an int pointer to a char ptr and vice versa

风流意气都作罢 提交于 2019-11-28 07:37:59
The problem is simple. As I understand, GCC maintains that chars will be byte-aligned and ints 4-byte-aligned in a 32-bit environment. I am also aware of C99 standard 6.3.2.3 which says that casting between misaligned pointer-types results in undefined operations. What do the other standards of C say about this? There are also many experienced coders here - any view on this will be appreciated. int *iptr1, *iptr2; char *cptr1, *cptr2; iptr1 = (int *) cptr1; cptr2 = (char *) iptr2; There is only one standard for C (the one by ISO), with two versions (1989 and 1999), plus some pretty minor

How to sum large numbers?

主宰稳场 提交于 2019-11-28 05:18:41
I am trying to calculate 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + ... + 1 * 2 * ... * n where n is the user input. It works for values of n up to 12. I want to calculate the sum for n = 13 , n = 14 and n = 15 . How do I do that in C89? As I know, I can use unsigned long long int only in C99 or C11. Input 13, result 2455009817, expected 6749977113 Input 14, result 3733955097, expected 93928268313 Input 15, result 1443297817, expected 1401602636313 My code: #include <stdio.h> #include <stdlib.h> int main() { unsigned long int n; unsigned long int P = 1; int i; unsigned long int sum = 0; scanf("