Some unfamiliar features in C language are well known in C Language
Designated initializers (c99)
C99 allows you to initialize the variable specified in the struct, as shown in figure
Struct Foo {
Int x;
Int y;
Int z;
};
Foo = {. z = 3,. x = 5 };
X is initialized to 0 by default.
The specified initialization can also be applied to arrays, as shown in the following three equivalent array definitions.
Int a [5] = {[1] = 2, [4] = 5 };
Int a [] = {[1] = 2, [4] = 5 };
Int a [5] = {0, 2, 0, 0, 5 };
Restricted pointers (c99)
The qualifier restricted is used to limit a pointer (for example, to tell the compiler that the memory access To the pointer can only be performed through the pointer under any circumstances, and the other pointers are invalid. For example:
Int f (const int * restrict x, int * y ){
(* Y) ++;
Int z = * x;
(* Y )--;
Return z;
}
Will be optimized by the compiler
Int f (const int * restrict x, int * y ){
Return * x;
}
Static and restrictive array parameters (c99)
Void f (int a [static 10]) {
/*...*/
}
If the pointer passed to f () is null or less than 10, the compiler will be warning!
Another example is void f (int a [const]) {
/*...*/
}
A cannot be modified.
Generic expression (c11)
With X Maroc (http://en.wikipedia.org/wiki/X_Macro), the compiler selects the appropriate form at compile time
# Define cbrt (X) _ Generic (X ),\
Long double: cbrtl ,\
Default: cbrt ,\
Float: cbrtf \
) (X)
The default format is cbrt (). If the real parameter is float, It will be replaced with cbrtf (), etc.
Switch condition judgment case (c11)
The case statement can appear in the if-else structure and loop structure.
Switch ()
{
Case 1 :;
//...
If (B = 2)
{
Case 2 :;
//...
}
Else case 3:
{
//...
For (B = 0; B <10; B ++)
{
Case 5 :;
//...
}
}
Break;
Case 4:
//...
Break;
}
Add operations without the "+" sign
# Include <stdio. h>
Int add (int a, int B ){
If (! = 0 & B! = 0)
Return printf ("% * c", a, '\ R', B,' \ R ');
Else return! = 0? A: B;
}
Int main (){
Int A = 0, B = 0;
Printf ("Enter the two numbers to add \ n ");
Scanf ("% d", & A, & B );
Printf ("Required sum is % d", add (A, B ));
Return 0;
}
Use the printf () return value or bitwise
Use return in the void returned value function body
Static void foo (void ){}
Static void bar (void ){
Return foo (); // Note this return statement.
}
Int main (void ){
Bar ();
Return 0;
}
# Include in strange places
# Include <stdio. h>
Void main ()
{
Printf
# Include "fragment. c"
}
("Dayum! \ N "); # text in Fragment. c
I can't speak about it either ..
Switch-case (gcc extension) with a range)
Switch (c ){
Case 'A'... 'Z ':
Doa ();
Break;
Case 1... 5:
Dob ();
}
C is executed when it is a-z character (Doa); Dob () is executed when it is 1-5 ();
The shortest C language program passed by the compiler
Main;
The author did not mention the compiling environment, although my compiler will report an error ..
Four equivalence values
A [I] = * (a + I) = * (I + a) = I [a];
Antique keyboard benefits
The following table is equivalent.
So it appears like this (: Garbled code
! E ()??!??! C ();
Default replacement (C ++)
# Include <iostream>
Int main (){
Int x = 2;
Switch (x ){
Case 1:
Cout <"1 ";
Break;
Abc1:
Cout <"2 ";
}
}
The default value in the c ++ switch-case structure can be replaced by any label.
Strange equivalence
#1 "1.c"
#1 "<command-line>"
#1 "/usr/include/stdc-predef.h" 1 3 4
#1 "<command-line>" 2
#1 "1.c"
Int main () {return 0 ;}
Equivalent
# Ifdef SOME_MACRO
Void foo (){}
# Endif
Int main () {return 0 ;}
Both can be compiled (in fact, it is a pre-processor --)