@@ -56,18 +56,24 @@ them.
5656Consider the following code, which sums the elements of an array after applying
5757the specified transformation:
5858
59- ``` c {8,24 } showLineNumbers
59+ ``` c {14,30 } showLineNumbers
6060// example.c
6161#include < stdio.h>
62- #include < string.h>
6362
64- double transform_and_sum (const double * array, size_t size, const char * option) {
63+ typedef enum {
64+ OPTION_HALF,
65+ OPTION_DOUBLE,
66+ OPTION_UNKNOWN,
67+ } TransformOption;
68+
69+ double transform_and_sum (const double * array, size_t size,
70+ TransformOption option) {
6571 double sum = 0.0;
6672
6773 double factor;
68- if (strcmp( option, "half") == 0 ) {
74+ if (option == OPTION_HALF ) {
6975 factor = 0.5;
70- } else if (strcmp( option, "double") == 0 ) {
76+ } else if (option == OPTION_DOUBLE ) {
7177 factor = 2.0;
7278 }
7379
@@ -80,7 +86,7 @@ double transform_and_sum(const double *array, size_t size, const char *option) {
8086
8187int main() {
8288 double array[ ] = {0.25, 0.25, 0.25, 0.25};
83- printf("Sum is: %f\n", transform_and_sum(array, 4, "unknownOption" ));
89+ printf("Sum is: %f\n", transform_and_sum(array, 4, OPTION_UNKNOWN ));
8490
8591 return 0;
8692}
@@ -89,7 +95,7 @@ int main() {
8995Note how `factor`, an automatic variable, is only explicitly initialized when
9096the received `option` is known. Since the C standard does not guarantee any
9197specific initial value, the state of `factor` is indeterminate in the previous
92- scenario (using an `unknownOption `), leading to different outcomes depending on
98+ scenario (using `OPTION_UNKNOWN `), leading to different outcomes depending on
9399the compiler and its settings:
94100
95101- For instance, `gcc -O2` behaves as if the `if` branch was taken:
@@ -136,34 +142,42 @@ double factor = 1.0;
136142Consider the following code, which sums the elements of an array after applying
137143the specified transformation:
138144
139- ``` fortran {9,20 } showLineNumbers
145+ ``` fortran {17,28 } showLineNumbers
140146! example.f90
147+
148+ module options
149+ integer, parameter :: OPTION_HALF = 1
150+ integer, parameter :: OPTION_DOUBLE = 2
151+ integer, parameter :: OPTION_UNKNOWN = 3
152+ end module options
153+
141154program main
142155 use iso_fortran_env, only: real32
156+ use options, only: OPTION_HALF, OPTION_DOUBLE, OPTION_UNKNOWN
143157 implicit none
144158
145159 real(kind=real32) :: array(4)
146160 array = [0.25, 0.25, 0.25, 0.25]
147161
148- print *, "Sum is:", transform_and_sum(array, "unknownOption" )
162+ print *, "Sum is:", transform_and_sum(array, OPTION_UNKNOWN )
149163
150164contains
151165
152166 real(kind=real32) function transform_and_sum(array, option)
153167 implicit none
154168
155169 real(kind=real32), intent(in) :: array(:)
156- character(len=*) , intent(in) :: option
170+ integer , intent(in) :: option
157171
158172 real(kind=real32) :: sum
159173 real(kind=real32) :: factor
160174 integer :: i
161175
162176 sum = 0.0
163177
164- if (option == "half" ) then
178+ if (option == OPTION_HALF ) then
165179 factor = 0.5
166- else if (option == "double" ) then
180+ else if (option == OPTION_DOUBLE ) then
167181 factor = 2.0
168182 end if
169183
@@ -180,7 +194,7 @@ end program main
180194Note how ` factor ` is only explicitly initialized when the received ` option ` is
181195known. Since the Fortran standard does not guarantee any specific initial
182196value, the state of ` factor ` is indeterminate in the previous scenario (using
183- an ` unknownOption ` ), leading to different outcomes depending on the compiler
197+ ` OPTION_UNKNOWN ` ), leading to different outcomes depending on the compiler
184198and its settings:
185199
186200- For instance, ` gfortran -O2 ` behaves as if the ` if ` branch was taken:
0 commit comments