-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.html
More file actions
5934 lines (5885 loc) · 235 KB
/
c.html
File metadata and controls
5934 lines (5885 loc) · 235 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="icon" type="image/gif" href="/favicon.gif"/>
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />
<title>Lorem Ipsum</title>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style type="text/css" id="internal-style">
@import url(hyperpolyglot.css);
</style>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="content-language" content="en"/>
</head>
<body>
<div id="container-wrap-wrap">
<div id="container-wrap">
<div id="container">
<div id="header">
<h1><a href="/"><span>Hyperpolyglot</span></a></h1>
</div>
<div id="content-wrap">
<div id="main-content">
<div id="page-title">Lorem Ipsum</div>
<div id="page-content">
<p><a name="top"></a><em>a side-by-side reference sheet</em></p>
<p><a href="#grammar-invocation">grammar and invocation</a> | <a href="#variables-expressions">variables and expressions</a> | <a href="#arithmetic-logic">arithmetic and logic</a> | <a href="#strings">strings</a> | <a href="#regexes">regexes</a> | <a href="#dates-time">dates and time</a> | <a href="#fixed-length-arrays">fixed-length-arrays</a> | <a href="#resizable-arrays">resizable arrays</a> | <a href="#dictionaries">dictionaries</a> | <a href="#functions">functions</a> | <a href="#execution-control">execution control</a> | <a href="#concurrency">concurrency</a> | <a href="#file-handles">file handles</a> | <a href="#files">files</a> | <a href="#file-fmt">file formats</a> | <a href="#directories">directories</a> | <a href="#processes-environment">processes and environment</a> | <a href="#option-parsing">option parsing</a> | <a href="#libraries-namespaces">libraries and namespaces</a> | <a href="#objects">objects</a> | <a href="#user-defined-types">user-defined types</a> | <a href="#cpp-macros">c preprocessor macros</a> | <a href="#net-web">net and web</a> | <a href="#unit-tests">unit tests</a> | <a href="#debugging-profiling">debugging and profiling</a></p>
<table class="wiki-content-table">
<tr>
<th colspan="4"><a name="version"></a><a href="#version-note">version</a></th>
</tr>
<tr>
<th></th>
<th><a href="#c">c</a></th>
<th><a href="#go">go</a></th>
<th><a href="#java">java</a></th>
</tr>
<tr>
<td><a name="version-used"></a><a href="#version-used-note">version used</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>C11</em>, <em>gcc 4.8</em>, <em>clang 3.5</em></span></td>
<td><span style="color: gray"><em>1.10</em></span></td>
<td><span style="color: gray"><em>java 1.7</em></span></td>
</tr>
<tr>
<td><a name="show-version"></a><a href="#show-version-note">show version</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ gcc <span style="white-space: pre-wrap;">--</span>version</td>
<td>$ go version</td>
<td>$ javac -version</td>
</tr>
<tr>
<td><a name="implicit-prologue"></a><a href="#implicit-prologue-note">implicit prologue</a></td>
<td>#include <errno.h<br />
#include <stdlib.h<br />
#include <stdio.h<br />
#include <string.h<br />
#include <time.h<br />
#include <wchar.h</td>
<td>import "fmt"</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="4"><a name="grammar-invocation"></a><a href="#grammar-invocation-note">grammar and invocation</a></th>
</tr>
<tr>
<th></th>
<th>c</th>
<th>go</th>
<th>java</th>
</tr>
<tr>
<td><a name="hello-world"></a><a href="#hello-world-note">hello world</a></td>
<td>$ cat hello.c<br />
#include <stdio.h<br />
<br />
int main(int argc, char <span style="white-space: pre-wrap;">**</span>argv) {<br />
<span style="white-space: pre-wrap;"> </span>printf("Hello, World!\n");<br />
}<br />
<br />
$ gcc hello.c<br />
<br />
$ ./a.out<br />
Hello, World!</td>
<td>$ cat hello.go <br />
package main<br />
import "fmt"<br />
<br />
func main() {<br />
<span style="white-space: pre-wrap;"> </span>fmt.Printf("Hello, World!\n")<br />
}<br />
<br />
$ go build hello.go<br />
<br />
$ ./hello<br />
Hello, World!</td>
<td>$ cat Hello.java<br />
public class Hello {<br />
<span style="white-space: pre-wrap;"> </span>public static void main(String[] args) {<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>System.out.println("Hello, World!");<br />
<span style="white-space: pre-wrap;"> </span>}<br />
}<br />
<br />
$ javac Hello.java<br />
<br />
$ java Hello</td>
</tr>
<tr>
<td><a name="file-suffixes"></a><a href="#file-suffixes-note">file suffixes</a><br />
<span style="color: gray"><em>source, header, object file</em></span></td>
<td>.c .h .o</td>
<td>.go <span style="color: gray"><em>none</em></span> <span style="color: gray"><em>none</em></span></td>
<td>Foo.java<br />
<span style="color: gray"><em>none</em></span><br />
Foo.class<br />
<br />
<span style="color: gray">Foo.java <em>must define a single top level class</em> Foo</span></td>
</tr>
<tr>
<td><a name="stmt-terminator"></a><a href="#stmt-terminator-note">statement terminator</a></td>
<td>;</td>
<td>; <span style="color: gray"><em>or sometimes newline</em></span><br />
<br />
<span style="color: gray"><em>a new line terminates a statement when the last token on the line is<br />
<span style="white-space: pre-wrap;"> </span>(1) an identifier,<br />
<span style="white-space: pre-wrap;"> </span>(2) a numeric, character, or string literal,<br />
<span style="white-space: pre-wrap;"> </span>(3) one of the keywords</em> break, continue,<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>fallthrough, <em>or</em> return, //<br />
<span style="white-space: pre-wrap;"> </span>(4) one of// ++, <span style="white-space: pre-wrap;">--</span>, ), ], <em>or</em> }</span></td>
<td>;</td>
</tr>
<tr>
<td><a name="top-level-stmt"></a><a href="#top-level-stmt-note">top level statements</a></td>
<td> </td>
<td> </td>
<td><span style="color: gray"><em>each file contains the following elements in order:<br />
<br />
(1) optional package directive<br />
(2) zero or more import directives<br />
(3) one public class definition and zero or more private class definitions</em></span></td>
</tr>
<tr>
<td><a name="block-delimiters"></a><a href="#block-delimiters-note">block delimiters</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>{ <span style="color: gray"><em>...</em></span> }</td>
<td>{ <span style="color: gray"><em>...</em></span> }</td>
<td>{ }</td>
</tr>
<tr>
<td><a name="eol-comment"></a><a href="#eol-comment-note">end-of-line comment</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;">//</span> <span style="color: gray"><em>comment</em></span></td>
<td><span style="white-space: pre-wrap;">//</span> <span style="color: gray"><em>comment</em></span></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> comment</span></td>
</tr>
<tr>
<td><a name="multiple-line-comment"></a><a href="#multiple-line-comment-note">multiple line comment</a></td>
<td>/* <span style="color: gray"><em>comment line</em></span><br />
<span style="color: gray"><em>another line</em></span> */</td>
<td>/* <span style="color: gray"><em>comment line</em></span><br />
<span style="color: gray"><em>another line</em></span> */</td>
<td><span style="color: gray">/* comment<br />
another comment */</span></td>
</tr>
<tr>
<th colspan="4"><a name="variables-expressions"></a><a href="#variables-expressions-note">variables and expressions</a></th>
</tr>
<tr>
<th></th>
<th>c</th>
<th>go</th>
<th>java</th>
</tr>
<tr>
<td><a name="local-var"></a><a href="#local-var-note">variable</a></td>
<td><span style="color: gray">/* if inside function, memory allocated on stack: */</span><br />
int i;<br />
int j = 3;<br />
<br />
<span style="color: gray">/* memory allocated on heap: */</span><br />
int *ptr = malloc(sizeof *ptr);<br />
<span style="color: gray">/* if malloc fails, it returns NULL and sets errno to ENOMEM */</span><br />
*ptr = 7;</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> memory allocated on stack:</span><br />
var i int<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> allocated on stack; type inferred from literal:</span><br />
j := 3<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> memory allocated on heap:</span><br />
ptr := new(int)<br />
*ptr = 7</td>
<td>int i;<br />
int j = 3;</td>
</tr>
<tr>
<td><a name="free-heap"></a><a href="#free-heap-note">free heap</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>free(ptr);</td>
<td><span style="color: gray"><em>none; uses garbage collection</em></span></td>
<td><span style="color: gray"><em>garbage collected</em></span></td>
</tr>
<tr>
<td><a name="global-var"></a><a href="#global-var-note">global variable</a></td>
<td><span style="color: gray">/* in foo.c, outside of any function: */</span><br />
int x = 7;<br />
<br />
<span style="color: gray">/* to declare in bar.c: */</span><br />
extern int x;</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> foo.go:</span><br />
package foo<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> capitalized top-level identifiers are exported:</span><br />
var X = 7<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> bar.go:</span><br />
package bar<br />
import foo<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> package scope:</span><br />
var y = 3<br />
<br />
func baz() {<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> local scope:</span><br />
<span style="white-space: pre-wrap;"> </span>var z = 5<br />
<br />
<span style="white-space: pre-wrap;"> </span>fmt.Println(foo.X + y + z)<br />
}</td>
<td><span style="color: gray"><em>foo/Foo.java:</em></span><br />
package foo;<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> globals must be declared inside a<br />
<span style="white-space: pre-wrap;">//</span> class:</span><br />
public class Foo {<br />
<span style="white-space: pre-wrap;"> </span>public static int bar;<br />
}<br />
<br />
<span style="color: gray"><em>UseFoo.java:</em></span><br />
import foo.Foo;<br />
<br />
public class UseFoo {<br />
<span style="white-space: pre-wrap;"> </span>public static void main(String[] args) {<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>System.out.println(Foo.bar);<br />
<span style="white-space: pre-wrap;"> </span>}<br />
}</td>
</tr>
<tr>
<td><a name="uninitialized-var"></a><a href="#uninitialized-var-note">uninitialized variable</a></td>
<td><span style="color: gray"><em>The behavior of reading from uninitialized stack variables or unitialized memory allocated by</em> malloc <em>is undefined.<br />
<br />
Global and static variables are zero-initialized.<br />
<br />
Heap variables allocated by</em> calloc <em>have their bytes zeroed.</em></span></td>
<td><span style="color: gray"><em>Every type has a zero value. For numeric types it is zero and for strings it is the empty string.</em></span></td>
<td><span style="color: gray"><em>Compilation error if local variables are not explicitly initialized.<br />
<br />
Global variables and object attributes will be zero-initialized if not explicitly initialized. Zero values are zero for numeric types, false for booleans, and null for reference types.</em></span></td>
</tr>
<tr>
<td><a name="compile-time-const"></a><a href="#compile-time-const-note">compile time constant</a></td>
<td><span style="color: gray">/* usually preprocessor is used: */</span><br />
#define PI 3.14</td>
<td>const Pi = 3.14</td>
<td>final double pi = 3.14;</td>
</tr>
<tr>
<td><a name="immutable-var"></a><a href="#immutable-var-note">immutable variable</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>const int i = rand();</td>
<td><span style="color: gray"><em>none</em></span></td>
<td>import java.util.Random;<br />
<br />
Random rnd = new Random();<br />
final int i = rnd.nextInt(100);</td>
</tr>
<tr>
<td><a name="assignment"></a><a href="#assignment-note">assignment</a></td>
<td>i = 3;</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> defines variable of appropriate type:</span><br />
i := 3<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> variable must already be declared:</span><br />
i = 3</td>
<td>int n;<br />
n = 3;</td>
</tr>
<tr>
<td><a name="parallel-assignment"></a><a href="#parallel-assignment-note">parallel assignment</a></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> define variables of appropriate type:</span><br />
m, n := 3, 7<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> x and y must already be declared:</span><br />
x, y = 2, 8</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="swap"></a><a href="#swap-note">swap</a></td>
<td>int x = 1, y = 2, tmp;<br />
<br />
tmp = x;<br />
x = y;<br />
y = tmp;</td>
<td>x, y = y, x</td>
<td>int x = 1, y = 2, tmp = 0;<br />
<br />
tmp = x;<br />
x = y;<br />
y = tmp;</td>
</tr>
<tr>
<td><a name="compound-assignment"></a><a href="#compound-assignment-note">compound assignment</a></td>
<td><span style="color: gray"><em>arithmetic:</em></span><br />
+= -= *= /= %=<br />
<br />
<span style="color: gray"><em>bit:</em></span><br />
<span style="white-space: pre-wrap;"><<= >>= &= |= ^=</span></td>
<td><span style="color: gray"><em>arithmetic:</em></span><br />
+= -= *= /= %=<br />
<br />
<span style="color: gray"><em>bit:</em></span><br />
<span style="white-space: pre-wrap;"><<= >>= &= |= ^=</span></td>
<td>+= -= *= /= %=<br />
<span style="white-space: pre-wrap;"><<= >>= </span>&= ^= |=<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">>>=</span> //is arithmetic right shift,// <span style="white-space: pre-wrap;">>>>=</span> <em>is logical right shift</em></span></td>
</tr>
<tr>
<td><a name="incr-decr"></a><a href="#incr-decr-note">increment and decrement</a></td>
<td><span style="color: gray"><em>premodifiers:</em></span><br />
++i <span style="white-space: pre-wrap;">--</span>i<br />
<br />
<span style="color: gray"><em>postmodifiers:</em></span><br />
i++ i<span style="white-space: pre-wrap;">--</span></td>
<td><span style="color: gray"><em>postmodifiers only; cannot be used in expressions:</em></span><br />
i++ i<span style="white-space: pre-wrap;">--</span></td>
<td>int n = 1;<br />
int one = n++;<br />
int three = ++n;<br />
int two = <span style="white-space: pre-wrap;">--</span>n;</td>
</tr>
<tr>
<td><a name="addr"></a><a href="#addr-note">address</a></td>
<td>int i = 3;<br />
int* ptr = &i;</td>
<td>i := 3<br />
<br />
var ptr *int<br />
ptr = &i<br />
ptr2 := &i</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="dereference"></a><a href="#dereference-note">dereference</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>int i2 = *ptr;</td>
<td>i2 := *ptr</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="type-size"></a><a href="#type-size-note">type size</a></td>
<td><span style="color: gray">/* put type inside parens: */</span><br />
sizeof (int)<br />
<br />
<span style="color: gray">/* expressions and values don't require parens: */</span><br />
sizeof 1 + 1</td>
<td>import "unsafe"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> use expression or name of variable with type:</span><br />
unsafe.Sizeof(i)<br />
unsafe.Sizeof(1 + 1)</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="addr-arith"></a><a href="#addr-arith-note">address arithmetic</a></td>
<td>int a[] = {3, 2, 1, 0};<br />
<br />
for (int *p = a; *p; ++p) {<br />
<span style="white-space: pre-wrap;"> </span>printf("%d\n", *p);<br />
}</td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="null"></a><a href="#null-note">null</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray">/* pointer types only: */</span><br />
NULL</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> cannot be stored in numeric or string variable:</span><br />
nil</td>
<td>null</td>
</tr>
<tr>
<td><a name="null-test"></a><a href="#null-test-note">null test</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>ptr == NULL</td>
<td>ptr == nil</td>
<td>ref == null</td>
</tr>
<tr>
<td><a name="conditional-expr"></a><a href="#conditional-expr-note">conditional expression</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>x > 0 ? x : -x</td>
<td><span style="color: gray"><em>none</em></span></td>
<td>x > 0 ? x : -x</td>
</tr>
<tr>
<th colspan="4"><a name="arithmetic-logic"></a><a href="#arithmetic-logic-note">arithmetic and logic</a></th>
</tr>
<tr>
<th></th>
<th>c</th>
<th>go</th>
<th>java</th>
</tr>
<tr>
<td><a name="boolean-type"></a><a href="#boolean-type-note">boolean type</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>int<br />
<br />
<span style="color: gray">/* includes type for consistency with C++: */</span><br />
#include <stdbool.h<br />
<br />
bool</td>
<td>bool</td>
<td>boolean</td>
</tr>
<tr>
<td><a name="true-false"></a><a href="#true-false-note">true and false</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>1 0<br />
<br />
<span style="color: gray">/* includes identifiers for consistency with C++: */</span><br />
#include <stdbool.h<br />
<br />
true false</td>
<td>true false</td>
<td>true false</td>
</tr>
<tr>
<td><a name="falsehoods"></a><a href="#falsehoods-note">falsehoods</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>0 0.0 NULL false</td>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td><a name="logical-op"></a><a href="#logical-op-note">logical operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>&& @</td>
<td>@@ !</td>
<td>&& @</td>
<td>@@ !</td>
<td>&& @</td>
<td>@@ !</td>
</tr>
<tr>
<td><a name="relational-op"></a><a href="#relational-op-note">relational operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>== != < > <= >=</td>
<td>== != < > <= >=</td>
<td>== != < > <= >=</td>
</tr>
<tr>
<td><a name="int-type"></a><a href="#int-type-note">integer type</a></td>
<td>signed char <span style="color: gray"><em>1+ bytes</em></span><br />
short int <span style="color: gray"><em>2+ bytes</em></span><br />
int <span style="color: gray"><em>2+ bytes</em></span><br />
long int <span style="color: gray"><em>4+ bytes</em></span><br />
long long int <span style="color: gray"><em>4+ bytes</em></span><br />
<br />
<span style="color: gray"><em>types with portable sizes are defined in</em> stdint.h:</span><br />
int8_t int16_t int32_t int64_t</td>
<td>int <br />
int8 <br />
int16 <br />
int32 <br />
int64</td>
<td>byte n1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 1 byte</span><br />
short n2; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2 bytes</span><br />
int n3;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4 bytes</span><br />
long n4;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 8 bytes</span></td>
</tr>
<tr>
<td><a name="unsigned-type"></a><a href="#unsigned-type-note">unsigned type</a></td>
<td>unsigned char: <span style="color: gray"><em>1+ bytes</em></span><br />
unsigned short int <span style="color: gray"><em>2 bytes+</em></span><br />
unsigned int <span style="color: gray"><em>2 bytes+</em></span><br />
unsigned long int <span style="color: gray"><em>4+ bytes</em></span><br />
unsigned long long int <span style="color: gray"><em>4+ bytes</em></span><br />
<br />
<span style="color: gray"><em>types with portable sizes are defined in</em> stdint.h:</span><br />
uint8_t uint16_t uint32_t uint64_t</td>
<td>uint8 (byte)<br />
uint16<br />
uint32<br />
uint64</td>
<td>char n1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span>2 bytes</span></td>
</tr>
<tr>
<td><a name="float-type"></a><a href="#float-type-note">float type</a></td>
<td>float <span style="color: gray"><em>4 bytes</em></span><br />
double <span style="color: gray"><em>8 bytes</em></span><br />
long double <span style="color: gray"><em>16 bytes</em></span><br />
<br />
<span style="color: gray"><em>registers may be larger on some systems</em></span></td>
<td>float32<br />
float64</td>
<td>float x1;<span style="white-space: pre-wrap;"> </span><span style="color: gray"><span style="white-space: pre-wrap;">//</span> 4 bytes</span><br />
double x2; <span style="color: gray"><span style="white-space: pre-wrap;">//</span> 8 bytes</span></td>
</tr>
<tr>
<td><a name="arith-op"></a><a href="#arith-op-note">arithmetic operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;">+</span> - * / %</td>
<td><span style="white-space: pre-wrap;">+</span> - * / %</td>
<td><span style="white-space: pre-wrap;">+</span> - * / %</td>
</tr>
<tr>
<td><a name="int-div"></a><a href="#int-div-note">integer division</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>3 / 7</td>
<td>3 / 7</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> evaluates to 2:</span><br />
7 / 3</td>
</tr>
<tr>
<td><a name="int-div-zero"></a><a href="#int-div-zero-note">integer division by zero</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>system dependent; process often sent a</em> SIGFPE <em>signal</em></span></td>
<td><span style="color: gray"><em>on Unix, process sent a</em> SIGFPE <em>signal</em></span></td>
<td><span style="color: gray"><em>throws</em></span> java.lang.ArithmeticException</td>
</tr>
<tr>
<td><a name="float-div"></a><a href="#float-div-note">float division</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>3 / (float)7</td>
<td>3 / float32(7)</td>
<td>7 / (float)3</td>
</tr>
<tr>
<td><a name="float-div-zero"></a><a href="#float-div-zero-note">float division by zero</a></td>
<td><span style="color: gray">/* these are float values but not literals: */</span> <br />
inf, nan, or -inf</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> these are float values but not literals:</span><br />
+Inf, NaN, <span style="color: gray"><em>or</em></span> -Inf<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> to get the float values:</span><br />
import "math"<br />
<br />
math.Inf(1) math.Nan() math.Inf(-1)</td>
<td>Float.POSITIVE_INFINITY<br />
Float.NaN<br />
Float.NEGATIVE_INFINITY<br />
<br />
<span style="color: gray"><em>constants with same names defined in</em></span> Double</td>
</tr>
<tr>
<td><a name="power"></a><a href="#power-note">power</a></td>
<td>#include <math.h> <br />
<br />
pow(2.0, 3.0)</td>
<td>import "math"<br />
<br />
math.Pow(2.0, 3.0)</td>
<td>Math.pow(2.0, 32.0);</td>
</tr>
<tr>
<td><a name="sqrt"></a><a href="#sqrt-note">sqrt</a></td>
<td>#include <math.h<br />
<br />
sqrt(2);</td>
<td>include "math"<br />
<br />
math.Sqrt(2)</td>
<td>Math.sqrt(2)</td>
</tr>
<tr>
<td><a name="sqrt-negative-one"></a><a href="#sqrt-negative-one-note">sqrt -1</a></td>
<td>#include <math.h<br />
<br />
<span style="color: gray">/* nan */</span><br />
double x = sqrt(-1.0);</td>
<td>import "math"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> NaN</span><br />
x := math.Sqrt(-2.0)<br />
<br />
import "math/cmplx"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> (0+1.41421356i)</span><br />
z := cmplx.Sqrt(-2.0)</td>
<td>Double.NaN</td>
</tr>
<tr>
<td><a name="transcendental-func"></a><a href="#transcendental-func-note">transcendental functions</a></td>
<td>#include <math.h<br />
<br />
exp log log2 log10<br />
sin cos tan<br />
asin acos atan<br />
atan2</td>
<td>include "math"<br />
<br />
math.Exp math.Log math.Log2 math.Log10<br />
math.Sin math.Cos math.Tan<br />
math.Asin math.Acos math.Atan<br />
math.Atan2</td>
<td>Math.exp Math.log <span style="color: gray"><em>none</em></span> Math.log10<br />
Math.sin Math.cos Math.tan<br />
Math.asin Math.acos Math.atan<br />
Math.atan2</td>
</tr>
<tr>
<td><a name="transcendental-const"></a><a href="#transcendental-const-note">transcendental constants</a></td>
<td>#include <math.h<br />
<br />
M_PI<br />
M_E</td>
<td>import "math"<br />
<br />
math.Pi<br />
Math.E</td>
<td>Math.E<br />
Math.PI</td>
</tr>
<tr>
<td><a name="float-truncation"></a><a href="#float-truncation-note">float truncation</a></td>
<td>#include <math.h<br />
<span style="white-space: pre-wrap;"> </span><br />
double d = 3.77;<br />
<span style="white-space: pre-wrap;"> </span><br />
long trunc = (long)d;<br />
long rnd = round(d);<br />
long flr = floorl(d);<br />
long cl = ceill(d);</td>
<td>include "math"<br />
<br />
x = 3.77<br />
<br />
trunc := int(x)<br />
<span style="color: gray"><em>none</em></span><br />
flr := int(math.Floor(x))<br />
cl := int(math.Ceil(x))</td>
<td>(long)3.77<br />
Math.round(3.77)<br />
(long)Math.floor(3.77)<br />
(long)Math.ceil(3.77)</td>
</tr>
<tr>
<td><a name="absolute-val"></a><a href="#absolute-val-note">absolute value</a></td>
<td>#include <math.h><span style="white-space: pre-wrap;"> </span><span style="color: gray">/* fabs() */</span><br />
<br />
int i = abs(-7);<br />
float x = fabs(-7.77);</td>
<td>include "math"<br />
<br />
<span style="color: gray"><em>none</em></span><br />
math.Abs(-7.77)</td>
<td>Math.abs(-7)<br />
Math.abs(-7.77)</td>
</tr>
<tr>
<td><a name="int-overflow"></a><a href="#int-overflow-note">integer overflow</a></td>
<td> </td>
<td> </td>
<td><span style="color: gray"><em>modular arithmetic</em></span></td>
</tr>
<tr>
<td><a name="float-overflow"></a><a href="#float-overflow-note">float overflow</a></td>
<td> </td>
<td> </td>
<td>Float.POSITIVE_INFINITY</td>
</tr>
<tr>
<td><a name="float-limits"></a><a href="#float-limits-note">float limits</a><br />
<span style="color: gray"><em>largest finite float, smallest positive float</em></span></td>
<td> </td>
<td> </td>
<td>Float.MAX_VALUE<br />
Float.MIN_VALUE<br />
Double.MAX_VALUE<br />
Double.MIN_VALUE</td>
</tr>
<tr>
<td><a name="complex-type"></a><a href="#complex-type-note">complex type</a></td>
<td>float complex <span style="color: gray"><em>8 bytes</em></span><br />
double complex <span style="color: gray"><em>16 bytes</em></span><br />
long double complex <span style="color: gray"><em>32 bytes</em></span></td>
<td>complex64<br />
complex128</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="complex-construction"></a><a href="#complex-construction-note">complex construction</a></td>
<td>#include <complex.h<br />
<br />
double complex z;<br />
z = 1.0 + 2.0 * I;<br />
<br />
<span style="color: gray">/* C11: */</span><br />
double complex z = CMPLX(1.0, 2.0);</td>
<td>var z complex128 = 1.0 + 2.0i</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="complex-decomposition"></a><a href="#complex-decomposition-note">complex decomposition</a><br />
<span style="color: gray"><em>real and imaginary component, argument, absolute value, conjugate</em></span></td>
<td>#include <complex.h<br />
<br />
double x;<br />
double complex w;<br />
<br />
x = creal(z);<br />
x = cimag(z);<br />
x = carg(z);<br />
x = cabs(z);<br />
w = conj(z);</td>
<td>import "math/cmplx"<br />
<br />
var x float64<br />
var w complex128<br />
<br />
x = real(z)<br />
x = imag(z)<br />
x = cmplx.Phase(z)<br />
x = cmplx.Abs(z)<br />
w = cmplx.Conj(z)</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="random-num"></a><a href="#random-num-note">random number</a><br />
<span style="color: gray"><em>uniform integer, uniform float</em></span></td>
<td><span style="color: gray">/* lrand48 returns value in [0, 2**31 - 1]: */</span>**<br />
long n = lrand48(() % 100;<br />
<br />
<span style="color: gray">/* Value in interval [0.0, 1.0): */</span><br />
double x = drand48();</td>
<td>import "math/rand"<br />
<br />
n := rand.Intn(100)<br />
x := rand.Float64()</td>
<td>import java.util.Random;<br />
<br />
Random rnd = new Random();<br />
<br />
int i = rnd.nextInt(100);<br />
double x = rnd.nextDouble();<br />
double y = rnd.nextGaussian();</td>
</tr>
<tr>
<td><a name="random-seed"></a><a href="#random-seed-note">random seed</a></td>
<td>srand48(17);</td>
<td>import "math/rand"<br />
<br />
rand.Seed(17)</td>
<td>import java.util.Random;<br />
<br />
Random rnd = new Random();<br />
<br />
rnd.setSeed(17);<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> seed can also be passed to constructor</span></td>
</tr>
<tr>
<td><a name="bit-op"></a><a href="#bit-op-note">bit operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span></td>
<td><span style="white-space: pre-wrap;"><< >> & |</span> <span style="color: gray"><em>none</em></span> ^</td>
<td><span style="white-space: pre-wrap;"> << >> & | ^ ~ </span><br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">>></span> //is arithmetic right shift, <span style="white-space: pre-wrap;">>>></span> is logical right shift//</span>//</td>
</tr>
<tr>
<td><a name="binary-octal-hex"></a><a href="#binary-octal-hex-note">binary, octal, and hex literals</a></td>
<td><span style="color: gray"><em>none</em></span><br />
052<br />
0x2a</td>
<td><span style="color: gray"><em>none</em></span><br />
052<br />
0x2a</td>
<td>0b101010<br />
052<br />
0x2a</td>
</tr>
<tr>
<td><a name="radix"></a><a href="#radix-note">radix</a><br />
<span style="color: gray"><em>convert integer to and from string with radix</em></span></td>
<td> </td>
<td> </td>
<td>Integer.toString(42, 7)<br />
Integer.parseInt("60", 7)</td>
</tr>
<tr>
<th colspan="4"><a name="strings"></a><a href="#strings-note">strings</a></th>
</tr>
<tr>
<th></th>
<th>c</th>
<th>go</th>
<th>java</th>
</tr>
<tr>
<td><a name="str-type"></a><a href="#str-type-note">string type</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>char *<br />
wchar_t *<br />
<br />
<span style="color: gray">wchar_t <em>is typically 32 bits on Linux and 16 bits on Windows.</em></span></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> immutable:</span><br />
string<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> mutable:</span><br />
[]byte</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> immutable:</span><br />
java.lang.String<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> mutable:</span><br />
java.lang.StringBuffer<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> StringBuffer <em>has</em> append(), delete(),<br />
<span style="white-space: pre-wrap;">//</span> deleteCharAt(), insert(), replace(), setCharAt().</span></td>
</tr>
<tr>
<td><a name="str-literal"></a><a href="#str-literal-note">string literal</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray">/* string in initialized data segment: */</span><br />
char *s = "hello";<br />
wchar_t *ws = L"hello";<br />
<br />
<span style="color: gray">/* string in heap: */</span><br />
char *s2 = strdup(s);<br />
wchar_t *ws2 = wcsdup(ws);<br />
<br />
<span style="color: gray">/* if strdup cannot allocate memory, it returns NULL and sets<br />
<span style="white-space: pre-wrap;"> </span>errno to ENOMEM. */</span></td>
<td>"hello"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> raw string literal:</span><br />
<span style="white-space: pre-wrap;">`hello`</span><br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> convert literal to mutable string:</span><br />
[]byte("hello")</td>
<td>"don't say\"no\""</td>
</tr>
<tr>
<td><a name="newline-in-str-literal"></a><a href="#newline-in-str-literal-note">newline in string literal</a></td>
<td><span style="color: gray">/* compiler concatenates literals<br />
<span style="white-space: pre-wrap;"> </span>separated by whitespace: */</span><br />
char *s = "first line\n"<br />
<span style="white-space: pre-wrap;"> </span>"second line";</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">//</span> backquote literals only:</span><br />
let s := <span style="white-space: pre-wrap;">`first line</span><br />
second line<span style="white-space: pre-wrap;">`</span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="str-literal-esc"></a><a href="#str-literal-esc-note">string escapes</a></td>
<td>\a \b \f \n \r \t \v \" \' \? \\<br />
\<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span> \x<span style="color: gray"><em>hh</em></span> \u<span style="color: gray"><em>hhhh</em></span> \U<span style="color: gray"><em>hhhhhhhh</em></span></td>
<td><span style="color: gray"><em>Double quote literals only:</em></span><br />
<br />
\a \b \f \n \r \t \v \\ \"<br />
\<span style="color: gray"><em>ooo</em></span> \x<span style="color: gray"><em>hh</em></span> \u<span style="color: gray"><em>hhhh</em></span> \U<span style="color: gray"><em>hhhhhhhh</em></span></td>
<td>\b \f \n \r \t<br />
\\ \" \'<br />
\u<span style="color: gray"><em>hhhh</em></span> \<span style="color: gray"><em>o</em></span> \<span style="color: gray"><em>oo</em></span> \<span style="color: gray"><em>ooo</em></span></td>
</tr>
<tr>
<td><a name="compare-str"></a><a href="#compare-str-note">compare strings</a></td>
<td><span style="color: gray">/* == and < compare memory addresses: */</span><br />
<br />
strcmp("hello", "world") == 0<br />
strcmp("hello", "world") < 0<br />
<br />
wcscmp(L"hello", L"world") == 0<br />
wcscmp(L"hello", L"world") < 0</td>
<td>"hello" == "world"<br />
"hello" < "world"</td>
<td>"hello".compareTo("world")</td>
</tr>
<tr>
<td><a name="str-to-num"></a><a href="#str-to-num-note">string to number</a></td>
<td><span style="color: gray">/* conversion functions:<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>strtol strtoll <br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>strtoul strtoull <br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>strtof strtod strtold */</span><br />
#include <limits.h<br />
<br />
char *s = "101 dalmations";<br />
char *rest;<br />
long n = strtol(s, &rest, 10);<br />
<br />
if (n == 0 && errno == EINVAL)<br />
<span style="white-space: pre-wrap;"> </span>printf("invalid input\n");<br />
else if ((n == LONG_MAX @</td>
<td>@@ n == LONG_MIN) && errno == ERANGE)<br />
<span style="white-space: pre-wrap;"> </span>printf("overflow\n");<br />
else<br />
<span style="white-space: pre-wrap;"> </span>printf("%ld %s\n", n, rest);<br />
<br />
<span style="color: gray">/* wide string conversion functions:<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>wcstol wcstoll <br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>wcstoul wcstoull <br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>wcstof wcstod wcstold */</span></td>
<td>import "strconv"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span>2nd arg is base, 3rd arg is size of int in bits:</span><br />
i, _ := strconv.ParseInt("17", 10, 32)<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2nd arg is size of float in bits:</span><br />
x, _ := strconv.ParseFloat("3.14", 32)</td>
<td>Byte.parseByte("14")<br />
Short.parseShort("14")<br />
Integer.parseInt("14")<br />
Long.parseLong("14")<br />
Float.parseFloat("14.7")<br />
Double.parseDouble("14.7")</td>
</tr>
<tr>
<td><a name="num-to-str"></a><a href="#num-to-str-note">number to string</a></td>
<td>long n = 1234;<br />
<br />
char buf[100];<br />
snprintf(buf, 100, "%ld", n);<br />
<br />
wchar_t buf2[100];<br />
swprintf(buf2, 100, L"%ld", n);<br />
<br />
<span style="color: gray">/* some format specifiers:<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>%d %ld %lld<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>%u %lu %llu<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>%.3f %.3e */</span></td>
<td>import "strconv"<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> 3rd arg is precision after decimal point;<br />
<span style="white-space: pre-wrap;">//</span> 4th arg is size of float in bits:</span><br />
strconv.FormatFloat(3.14, 'f', 4, 32)<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">//</span> 2nd arg is base:</span><br />
strconv.FormatInt(7, 10)</td>
<td>Integer.toString(14)<br />
Long.toString(14)<br />
Double.toString(14.7)</td>
</tr>
<tr>
<td><a name="split"></a><a href="#split-note">split</a></td>
<td><span style="color: gray">/* strtok_r modifies 1st arg */</span><br />
char *s = strdup("foo,bar baz");<br />
char *sep = " ,";<br />
char *tok, *last;<br />
<br />
<span style="color: gray">/* tok is never an empty string: */</span><br />
for (tok = strtok_r(s, sep, &last);<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>tok;<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>tok = strtok_r(NULL, sep, &last))<br />
<span style="white-space: pre-wrap;"> </span>printf("token: %s\n", tok);<br />
<br />
<span style="color: gray">/* also wcstok */</span></td>
<td>import "strings"<br />
<br />
s := "foo bar baz"<br />
parts := strings.Split(s, " ")</td>
<td>"Bob Ned Amy".split(" ")</td>
</tr>
<tr>
<td><a name="str-join"></a><a href="#str-join-note">join</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td>import "strings"<br />
<br />
parts := []string{"foo", "bar", "baz"}<br />
s := strings.Join(parts, " ")</td>
<td>String[] a = {"foo", "bar", "baz"};<br />
String s = String.join(" ", a);</td>
</tr>
<tr>
<td><a name="str-concat"></a><a href="#str-concat-note">concatenate</a></td>
<td>char *s1 = "hello";<br />
char *s2 = " world";<br />
size_t len = strlen(s1) + strlen(s2) + 1;<br />
char *s3 = (char *)calloc(len, sizeof *s3);<br />
<br />
strcpy(s3, s1);<br />
strcat(s3, s2);<br />
<br />
<span style="color: gray">/* also wcscpy and wcscat */</span></td>
<td>"hello" + " world"</td>
<td>"hello" + " world"</td>
</tr>