Questions tagged [c99]

-1

votes
1

answer
47

Views

NULL substitute for a double in C language 99 standard

I am working on a Circularly-Doubly-Linked List with a sentinel in the C language with the 99 standard. The headers I am allowed to use for class are stdio.h, stdlib.h, and assert.h only. The sentinel is a struct and the structs have a value data member which is a double value. I was wondering if...
R00ki3 M1stak3
2

votes
1

answer
28

Views

Undefined behavior with pointer arithmetic on dynamically allocated memory

I'm probably misunderstanding this, but does the c99 spec prevent any form of pointer arithmetic on dynamically allocated memory? From 6.5.6p7... For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an ar...
zagortenay333
1

votes
1

answer
45

Views

Syntax to set an array-of-structs element via initializer?

I have the following C code: typedef struct { int x,y; } Point2D; Point2D data[5]; Later in my code (i.e. not during initialization of data), I want to set individual elements of data from x/y values. The two-statement-code is simple: Point2D pt = {.x = a, .y = b}; data[3] = pt; But is there a way i...
Dreamer
1

votes
1

answer
143

Views

Placemarker and non-placemarker tokens in pre-processor?

C99 standard and having trouble to understand this : c99 - 6.10.3.3 Semantics 3 --- (2nd sentence) Placemarker preprocessing tokens are handled specially: concatenation of two placemarkers results in a single placemarker preprocessing token, and concatenation of a placemarker with a non-placema...
Omkant
1

votes
1

answer
1.3k

Views

MISRA C 2004 and c99

Rule 1.1 of the MISRA C 2004 specifies that the spec covers c90 and not c99. I would like to use the stdint and stdbool libraries instead of coding my own. Has anyone made this exception in their MISRA implementation?
JeffV
1

votes
2

answer
6.2k

Views

c99 Mode for Single Files in for Code::Blocks

I am having problems compiling some code I am working on. I have recently decided to learn C so I can get a better understanding of low level languages. I tried to make a for loop, but it returns the error that I cannot make declarations within the loop without c99 mode. I went into Settings>Compile...
Grimey
1

votes
2

answer
117

Views

Can constant objects with static storage duration and equal, constant initializers be coalesced?

Consider two objects with static storage duration and equal, constant initializers: static const int a = 50; static const int b = 50; Is it valid for a compiler to combine these such that &a == &b? (For context, I was thinking of using static constant objects to get unique addresses to use as sentin...
icktoofay
1

votes
2

answer
193

Views

Can anybody please explain the behavour of C preprocessor in following examples?

I am implementing a C macro preprocessor (C99)... I am surprised by the following behaviour.... Ex1: #define PASTE(x) X_##x #define EXPAND(x) PASTE(x) #define TABSIZE 1024 #define BUFSIZE TABSIZE PASTE(BUFSIZE) EXPAND(BUFSIZE) expands to: X_BUFFSIZE X_1024 Ex2: #define EXPAND(s) TO_STRING(s) #define...
Sam
1

votes
1

answer
180

Views

Scope of enum itself in Objective-C?

Consider the following code: enum TableSections { kSection1 = 0, kSection2 = 1, }; What is the scope of the identifier TableSections? If this code is in an implementation file, is TableSections only defined within that compilation unit, defined globally, or something else? And is the answer specifi...
Isaac
1

votes
3

answer
496

Views

Initialization of the structure containing pointer to another structure in C99

I've some structures definitions below : typedef struct { uint16_t a ; } my_type1_t ; typedef struct { uint16_t b ; } my_type2_t ; typedef struct { my_type1_t* a_ptr ; my_type2_t* b_ptr ; } both_t ; typedef struct { both_t* both_ptr ; } gathered_t and some instances of the above structures :...
Lazureus
1

votes
1

answer
636

Views

How is thread-safe errno initialized if #define substitutes errno symbol?

I'm trying to understand how glibc initializes errno without the preprocessor substituting the errno symbol. I first tried to implement a simple version myself based on csu/errno-loc.c and csu/errno.c: myerrno.h #ifndef MYERRNO_H #define MYERRNO_H extern int *myerrno_location(void); #define myerrno...
Vilhelm Gray
1

votes
4

answer
121

Views

Is the remove function guaranteed to delete the file?

The wording of the C99 standard seems a bit ambiguous regarding the behavior of the remove function. In section 7.19.4.1 paragraph 2: The remove function causes the file whose name is the string pointed to by filename to be no longer accessible by that name. A subsequent attempt to open that file us...
Vilhelm Gray
1

votes
1

answer
131

Views

Where does string-literal begin and end?

The C99 standard says the implementation limit for characters of a string literal is 4095(?). But where exactly does a literal end and begin? printf( "First part" "second part!\r\n" ); Would this be a single string literal? Or are this 2 string literals?
dhein
1

votes
1

answer
48

Views

Building a string character by character

I am consuming a character stream (unseekable) which contains separated strings: e.g. abc|def|ghijkd with the separator |. Hence when I am parsing a token, I keep adding the characters of the stream to a string until I find a pipe, and then I start a new string. I don't want to copy around the strin...
Hyperboreus
1

votes
1

answer
105

Views

In C99, can I use a return value without first assigning it to a variable?

I am replacing macros in a large C99 code base with inline functions to see if the compiler can do a better job optimizing. There are a lot of macros which expand to a data structure. The code uses them not as functions, but as a replacement for the data structure itself. For example... #define Av...
Schwern
1

votes
2

answer
2.3k

Views

How do I suppress PC-Lint errors for C99-style initialization of structure members?

I am using PC-Lint 8.00x with the following options: +v -wlib(1) +fan +fas I receive a number of error messages from PC-Lint when I run code similar to the following: typedef union { struct { unsigned int a : 4; unsigned int b : 4; unsigned int c : 4; unsigned int d : 4; } bits; unsigned short value...
embedded_guy
1

votes
2

answer
200

Views

Warning: Expression Result Unused in RenderScript

I don't get it. Unless I'm missing something, I use it on the next line. blockAverage >> blockSize; //Warning:expression result unused return rsPackColorTo8888(blockAverage);
John Moffitt
1

votes
2

answer
229

Views

Buffer allocation in stack with strlen()

I have the following code: void function(char *str) { int i; char buffer[strlen(str) + 1]; strcpy(buffer, str); buffer[strlen(str)] = '\0'; printf("Buffer: %s\n", buffer); } I would expect this code to throw a compile time error, as the 'buffer' being allocated on the stack has a runtime dependent l...
1

votes
2

answer
163

Views

long double subnormals/denormals get truncated to 0 [-Woverflow]

In the IEEE754 standarad, the minimum strictly positive (subnormal) value is 2−16493 ≈ 10−4965 using Quadruple-precision floating-point format. Why does GCC reject anything lower than 10-4949? I'm looking for an explanation of the different things that could be going on underneath which determ...
Harvinder
1

votes
1

answer
120

Views

What is an equivalent of 'unlocked' I/O functions in C99?

When I use --std=c99 GCC defines __STRICT_ANSI__ and when it is on, the BSD and System V features don't kick in. It implies __USE_MISC and __USE_POSIX are left undefined. -- stdio.h -- ... #if defined __USE_POSIX || defined __USE_MISC extern int getc_unlocked (FILE *__stream); extern int getchar_unl...
soerium
1

votes
3

answer
53

Views

getting a runtime error in the code

GETTING A RUNTIME ERROR IN THE given code INITIALISING VARIABLES #include #include int main(){ int k; int n; int i; int *a; int b; int t; int f=0; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&k); dynami a=malloc(sizeof(int)*n); for(i=0;i
ASEN
1

votes
2

answer
410

Views

Dealing with __attribute__ in MSVC

I was wondering what the best way to deal with code containing GCC's __attribute__ extension when using MSVC. Is the following a safe way of dealing with this: #define __attribute__(x) /* blank - should simply ignore thanks to C preprocessor */ Thanks!
Liam Deacon
1

votes
2

answer
813

Views

Local variable length array

during some code refactor in C++ i meet following local variable length arrays void some_function(uint8_t length, uint8_t id, uint8_t * bytes)) { uint8_t string[length + 8]; //some transformation on string [1-8] elements do_something(string); } I am not familiar with C99 but using Variable-length ar...
1

votes
2

answer
297

Views

How can this be dereferencing ‘void *’ when the pointer was declared with a type?

I'm trying to make an array with file-level or global scope whose size is determined at runtime. Various articles like this one suggest this pattern for such a dynamic array: static MISCTYPE *handles; static int nthreads; int main(int argc, char **argv) { // nthreads set from from argv handles = mal...
ewall
1

votes
2

answer
114

Views

Storing the value for a random number C

//Prompt the user to enter a number printf("Please enter a number: "); //Read number scanf("%d", &number); //Display number printf("You entered %d", number); printf("\n"); //Get a random number and scale to range 3..37 srand(time(NULL)); random_num = (37 - 3) + 1; //Display random number printf("You...
NoCash4College
3

votes
2

answer
389

Views

C99 stdint.h error in VS2013

Is there any reason why the code below shouldn't work? (Compile as C Code) code #include int main() { int var = 10; if (var == 0) return 1; uint8_t data; return 0; } Error messages Error 3 error C2065: 'data' : undeclared identifier Error 2 error C2146: syntax error : missing ';' before identif...
LordAlgar
1

votes
2

answer
332

Views

Visual Studio C function syntax error

I'm trying to implement a C function like this in Visual Studio, I've followed Creating a C Project in Visual Studio void changeParameters(int &a, int n) { for (int i=0; i
onmyway133
11

votes
7

answer
2.7k

Views

What is wrong with using turbo C?

I always find that some people (a majority from India) are using turbo C. I cannot find any reason to use such outdated compiler... But I don't know what reasons to give when trying to tell them to use modern compiler(gcc,msvc,...).
Nyan
1

votes
4

answer
122

Views

How to pass infinity values to a function and test the result

I have this function: #include complex double f(complex double x, complex double y) { return x*y; } I would like to call it with x = inf + i inf and y = i inf and see what the result is. In particular I want to test if the result is an infinity value (as it should be) or if it is NaN + iNaN. The r...
eleanora
4

votes
2

answer
87

Views

Can't have label on mid-stream declaration in C?

So this in C99: label: int ret = function(of, stuff); gives a compile-time error, whereas this: label: ; int ret = function(of, stuff); works just fine. Is this a compiler bug? Or is this a bug in the definition of the C standard? Or if this is part of the C99 standard, perhaps someone would rise to...
Mark Adler
3

votes
1

answer
51

Views

What “discontinuities in the error” are present in this trigonometric argument reduction?

I have been reading C99 Rationale, where I found this mysterious excerpt (emphasis is mine): 7.12.4 Trigonometric functions Implementation note: trignometric argument reduction should be performed by a method that causes no catastrophic discontinuities in the error of the computed result. In particu...
5

votes
2

answer
1.7k

Views

When to use variable length array in C, but when a dynamic allocation?

I find out about Variable Length Array in C99, but it looks like it behave almost the same as malloc + free. The practical differences I found: Too big array handling: unsigned size = 4000000000; int* ptr = malloc(size); // ptr is 0, program doesn't crash int array[size]; // segmentation fault, prog...
Grzegorz Bazior
16

votes
6

answer
2.3k

Views

What can human beings make out of the restrict qualifier?

If I got the C99 restrict keyword right, qualifying a pointer with it is a promise made that the data it references won't be modified behind the compiler's back through aliasing. By contrast, the way I understand the const qualifier is as compiler-enforced documentation that a given object won't be...
Jérémie Koenig
17

votes
1

answer
1k

Views

Adding two floating-point numbers

I would like to compute the sum, rounded up, of two IEEE 754 binary64 numbers. To that end I wrote the C99 program below: #include #include #pragma STDC FENV_ACCESS ON int main(int c, char *v[]){ fesetround(FE_UPWARD); printf("%a\n", 0x1.0p0 + 0x1.0p-80); } However, if I compile and run my program...
Pascal Cuoq
17

votes
2

answer
684

Views

Bit-fields and sequence points

For an implementation that packs f0 and f1 into the same byte, is the program below defined? struct S0 { unsigned f0:4; signed f1:4; } l_62; int main (void) { (l_62.f0 = 0) + (l_62.f1 = 0); return 0; } I am interested in the answer for C99 and for C11 if there is reason to think that it is different...
Pascal Cuoq
17

votes
6

answer
2.3k

Views

Free static checker for C99 code

I am looking for a free static checker for C99 code (including GCC extensions) with the ability to explicitly say "these preprocessor macros are always defined." I need that last part because I am compiling embedded code for a single target processor. The compiler (Microchip's C32, GCC based) sets...
detly
17

votes
2

answer
329

Views

Will “&a+1 > &a” cause an undefined behaviour

Does c99/c++03 guarantee that &a+1 > &a is always true? for example, there's a (c-like) std::copy, and int a = 0 ; int b[9] ; std__copy(&a , &a+1 , b) ; Does this always work?
exprosic
17

votes
3

answer
27.8k

Views

How to use make and compile as C99?

I'm trying to compile a linux kernel module using a Makefile: obj-m += main.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean Which gives me: main.c:54: warning: ISO C90 forbids mixed declarations and code I need t...
djTeller
17

votes
1

answer
11.3k

Views

Is stdout line buffered, unbuffered or indeterminate by default?

Section 7.9.13/7 of c99 states that: At program start-up, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened,...
paxdiablo
17

votes
4

answer
6.5k

Views

Is it a good idea to use C99 VLA compared to malloc/free?

Is it a good idea to use C99 VLA? When is it appropriate to use VLA compared to malloc/free? (since VLA may blow up stack?)
Nyan

View additional questions