What is conformance in c ?

posted on by wael

Conformance as defined by the c standard is two parts: conformance of a program and conformance of an implementation . Concerning the conformance of a program , a c program can either be : strictly conforming or conforming . As for the conformance of an implementation it can be either hosted or freestanding .

conformance : strictly conforming , conforming , conforming freestanding , conforming hosted

Conformance of a c program

The c standard defines a c program to be strictly conforming , if first of all it only uses the language features and the standard library features that appears in the c standard . An example of a language feature defined by the c standard is a defined type such as the char type or the int type … An example of a standard library feature defined by the c standard is the limits.h header which defines the limits for the integral types .

In other words a strictly conforming program , doesn't use any feature provided by an implementation , and not defined by the c standard. For example the gcc compiler provides extension to the c language which are not standardized such as a block of code wrapped in parentheses that can return a value .

			

#include <stdio.h>
int main(int argc , char *argv[]){
	int  x =  ({
		int a = 0;	
		int b = 1;
		a + b ;
	});
	printf("%d\n" , x);
	return 0;
}

/*
The block of code declares two 
variables , and compute their sum .
The computed sum is returned , and
is assigned to the variable x .

The output of the program is :
1

This program uses a feature defined
by the gcc implementation of the c 
standard : 
	a block of code wrapped in 
	parentheses that can return a 
	value.
This feature is not part of the c
standard. 
This is no a strictly conforming
program.
*/
					

Second of all a strictly conforming program , shall not produce output dependent on any behavior which is:

  • unspecified
  • implementation defined
  • undefined

An unspecified behavior is either the result of using an unspecified value , or it is a behavior to which the standard defines more than one possibility and it does not state which one is to be chosen at a given instance . An example of unspecified behavior , is the order of evaluation of the argument of a function .

			

#include <stdio.h>

void a_function(int a , int b){
	printf("%d\n",a);
	printf("%d\n",b);
}

int main(int argc,char *argv[]){
	int c = 0;
	a_function(c , c++);	
}


/*
This program output depends on unspecified
behavior .      
The order of  evaluation of a function 
argument is an example of unspecified 
behavior .
a_function(c , c++);
The arguments can either be  evaluated from 
left to right or from right to left . 
In this case while using the gcc compiler
it evaluates the arguments from right 
to left, and the output of the program 
is :
1
0
If it had evaluated the argument from left 
to right then the output of the program will 
be :
0 
0
*/
					

An unspecified value is a valid value for a given type , There are no requirement by the standard on which one is chosen at any given instance . So an unspecified value can be the result of unspecified behavior .

An implementation defined behavior is an unspecified behavior , where an implementation makes a choice, and documents how it made it .

An undefined behavior is a behavior which is not explicitly defined by the c standard or is a behavior that the c standard explicitly states that it is undefined . An example of undefined behavior is integer overflow .

Third of all , a strictly conforming program shall not exceed any minimum implementation limit, for example having more than 127 arguments in one function call.

A conforming program is a program which is accepted by a conforming implementation .

Conformance of an implementation

A conforming hosted implementation is an implementation that accepts any strictly conforming c program .

A conforming freestanding implementation is an implementation that accepts a strictly conforming c program which only use the follow headers from the standard c library :

limits.h
Defines macros related to integer types limits.
float.h
Defines macros related to floating types limits.
stdbool.h
Defines boolean related macros.
stddef.h
Defines some macros and types , such as size_t , NULL …
stdarg.h
Defines macros and a type related to variable length arguments.
iso646.h
Defines replacement for equality , boolean and bitwise operators.
stdalign.h
Defines macros related to alignment.
stdnoreturn.h
Defines a single macro named noreturn . A function defined with this macro , does not return to the caller . An example of a function that does not return to its caller is the exit function which is part of the stdlib.h header

If an implementation also define any of these two macros __STDC_IEC_60559_BFP__ or __STDC_IEC_60559_DFP__ then a strictly conforming program accepted by a freestanding implementation might also use the following c library headers :

math.h
Defines math related functions types and macros.
fenv.h
Defines macros , functions and types related to floating point environment .
stdlib.h
Defines some types , utility functions , and macros . The c program accepted by the freestanding implementation might only use the numeric conversion functions from this header .

A conforming implementation be it freestanding or hosted , can also have additional extensions , such as extension to the language itself , or to the standard library as long as these extension do not alter the behavior of a strictly conforming program .