// filename:c2011-6-7-9-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf // C2011 6.7.9 Initialization // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.xx, 2013 // compile errors and/or wornings: // 1 (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) // Target: x86_64-apple-darwin11.4.2 //Thread model: posix // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 2 gcc-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. #include #include // Example 1 int i = 3.5; double complex c = 5 + 3 * I; // Example 2 int x[] = { 1, 3, 5 }; // Example 3 int y[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 }, }; // Example 4 int z[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; // Example 5 struct { int a[3], b; } w[] = { { 1 }, 2 }; // Example 6 short q[4][3][2] = { { 1 }, { 2, 3 }, { 4, 5, 6 } }; short q1[4][3][2] = { 1, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 4, 5, 6 }; short q2[4][3][2] = { { { 1 }, }, { { 2, 3 }, }, { { 4, 5 }, { 6 }, } }; // Example 7 typedef int A[]; // OK - declared with block scope A a = { 1, 2 }, b = { 3, 4, 5 }; int a1[] = { 1, 2 }, b1[] = { 3, 4, 5 }; // Example 8 char s[] = "abc", t[3] = "abc"; char s1[] = { 'a', 'b', 'c', '\0' }, t1[] = { 'a', 'b', 'c' }; char *p = "abc"; // Example 9 enum { member_one, member_two }; const char *nm[] = { [member_two] = "member two", [member_one] = "member one", }; // Example 10 struct div { int quot; int rem; }; typedef struct div div_t; div_t answer = { .quot = 2, .rem = -1 }; // Example 11 struct { int a[3], b; } w1[] = { [0].a = {1}, [1].a[0] = 2 }; // Example 12 #define MAX 10 int a2[MAX] = { 1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0 }; // Example 13 union {int any_member; /* ... */ } u = { .any_member = 42 }; int main(void) { return printf("6.7.9 Initialization %d %f %+fi %d %d\n",i, creal(c), cimag(c),a[0],x[0]); } // 1.LLVM3.2 warning may be // 1.1 //c2011-6-7-9-ex.c:14:9: warning: implicit conversion from 'double' to 'int' changes value from 3.5 to 3 [-Wliteral-conversion] //int i = 3.5; // ~ ^~~ //1 warning generated. // 1.1 cc -std=c11c2011-6-7-9-ex.c //c2011-6-7-9-ex.c:14:9: warning: implicit conversion from 'double' to 'int' changes value from 3.5 to 3 [-Wliteral-conversion] //int i = 3.5; // ~ ^~~ //c2011-6-7-9-ex.c:33:35: warning: suggest braces around initialization of subobject [-Wmissing-braces] //struct { int a[3], b; } w[] = { { 1 }, 2 }; // ^ // {} // ................ // 1.2 cc -std=c11 -Wall c2011-6-7-9-ex.c // 19 warnings generated. // // 2. GCC4.9 gcc-4.9 -std=c11 c2011-6-7-9-ex.c // no warning // 2.1 gcc-4.9 -std=c11 -Wall c2011-6-7-9-ex.c //c2011-6-7-9-ex.c:33:8: warning: missing braces around initializer [-Wmissing-braces] // struct { int a[3], b; } w[] = { { 1 }, 2 }; // ^ //c2011-6-7-9-ex.c:33:8: warning: (near initialization for 'w[0].a') [-Wmissing-braces] //c2011-6-7-9-ex.c:37:1: warning: missing braces around initializer [-Wmissing-braces] // { 1 }, // ^ //c2011-6-7-9-ex.c:37:1: warning: (near initialization for 'q[0][0]') [-Wmissing-braces] //c2011-6-7-9-ex.c:42:1: warning: missing braces around initializer [-Wmissing-braces] // 1, 0, 0, 0, 0, 0, // ^ //c2011-6-7-9-ex.c:42:1: warning: (near initialization for 'q1[0]') [-Wmissing-braces] // output may be // 6.7.9 Initialization 3 5.000000 +3.000000i 1 1