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 .