You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixed issues in C code examples and added modern standard idioms (C99 & C11).
The C examples used in that file are not purely C and contain several C++ ism.
Furthermore, they do not use C99 idioms that are now very common. Some of the
examples also contain errors.
- typecasts of allocation functions are typical of C++ code but are errors in
pure C.
- names with leading underscore are reserved for the implementation and shall
not be used.
- One example freed an unitialized pointer -> crash guaranteed.
- Where appropriate, modified or extended the C examples with constructs from
C99 and C11. Mainly designated initializers.
D does it by positional dependence too, but an index can be used as well.
911
+
The D syntax is lighter than C99 designated initializers.
904
912
The following all produce the same result:
905
913
906
914
----------------------------
@@ -981,9 +989,18 @@ $(CCODE
981
989
#include <tchar.h>
982
990
tchar string[] = TEXT("hello");
983
991
)
992
+
Furthermore, in praxis `wchar_t` is not usable in portable code as its size
993
+
is implementation dependent. On POSIX conforming machines it generally
994
+
represents an UTF-32 codeunit, on Windows an UTF-16 codeunit. C11 introduced
995
+
C++11 types char16_t and char32_t to overcome this issue.
996
+
984
997
$(H4 The D Way)
985
998
986
-
The type of a string is determined by semantic analysis, so there is no need to wrap strings in a macro call. Alternatively if type inference is used the string can have a $(B c), $(B w) or $(B d) suffix, representing UTF-8, UTF-16 and UTF-32 encoding, respectively. If no suffix is used the type is inferred to be a UTF-8 string:
999
+
The type of a string is determined by semantic analysis, so there is no need
1000
+
to wrap strings in a macro call. Alternatively if type inference is used the
1001
+
string can have a `c`, `w` or `d` suffix, representing UTF-8,
1002
+
UTF-16 and UTF-32 encoding, respectively. If no suffix is used the type is
This is fairly easy to get right because the number of entries is small. But suppose it gets to be fairly large. Then it can get difficult to maintain correctly when new entries are added.
1026
+
This is fairly easy to get right because the number of entries is small.
1027
+
But suppose it gets to be fairly large. Then it can get difficult to
1028
+
maintain correctly when new entries are added. C99 added designated
1029
+
initializers to solve that problem.
1009
1030
1010
1031
$(H4 The D Way)
1011
1032
-----------------------------
@@ -1071,7 +1092,7 @@ if (h != HANDLE_INIT)
1071
1092
$(CCODE
1072
1093
struct Handle__ HANDLE_INIT;
1073
1094
1074
-
void init_handle() // call this function upon startup
1095
+
void init_handle(void) // call this function upon startup
1075
1096
{
1076
1097
HANDLE_INIT.value = (void *)-1;
1077
1098
}
@@ -1087,9 +1108,9 @@ if (memcmp(&h,&HANDLE_INIT,sizeof(Handle)) != 0)
1087
1108
1088
1109
$(H4 The D Way)
1089
1110
1090
-
D has powerful metaprogramming abilties which allow it to implement
1091
-
$(D typedef) as a library feature. Simply import $(B std.typecons) and
1092
-
use the $(B Typedef) template:
1111
+
D has powerful metaprogramming abilities which allow it to implement
1112
+
$(D typedef) as a library feature. Simply import `std.typecons` and
1113
+
use the `Typedef` template:
1093
1114
1094
1115
-----------------------------
1095
1116
import std.typecons;
@@ -1103,7 +1124,7 @@ foo(h); // syntax error
1103
1124
bar(h); // ok
1104
1125
-----------------------------
1105
1126
1106
-
To handle a default value, pass the initializer to the $(B Typedef)
1127
+
To handle a default value, pass the initializer to the `Typedef`
1107
1128
template as the second argument and refer to it with the
0 commit comments