C2x: Future norme C


J'attrape un écho lointain,
Que va-t-il se passer dans mon siècle.
(Hameau, Boris Pasternak)

, C . : TIOBE , . C2x — C.


, , .


, --


, , C, .


1989 C — (ANSI) (ISO) . C C89, ANSI C — , .


, : C89, C99, C11, C18. , C2x.


— WG14, . “Committee”, «».


, N2353. , , . , .


, , , , , . : , (, , ), , .


. . , . , , . «» — , « » .



strdup strndup


, , , , C. , ? , C , POSIX.


, 20 strdup strndup!


#include <string.h>

char *strdup (const char *s);
char *strndup (const char *s, size_t size);

, .



C : . , , , .


- , ----. , . :


[[attr1]] struct [[attr2]] S { } [[attr3]] s1 [[attr4]], s2 [[attr5]];

attr1 s1 s2, attr2 struct S, attr3struct s1, attr4s1, attr5s2.


, . . :


  1. deprecated . .
  2. fallthrough switch, .
  3. nodiscard .
  4. , maybe_unused ( (void) unused_var).
  5. noreturn , .

(K&R)


1989 , « K&R» ( — « », — « C»), , , , void-.


, :


long maxl (a, b)
    long a, b;
{
    return a > b ? a: b;
}

C! , :


/*     */
int no_args();

/*      */
int no_args(void);


, , . , , C (. two's complement) .


, (!) «» — .



, , , . .



- C . , , main.


:


int main(int, char *[])
{
    /*   ! */
    return 0;
}

, !



, -- , , , , «» : true, false, alignas, alignof, bool, static_assert . <stdbool.h> , , .



:


const int music[] = {
   #embed int "music.wav"
};

, , , .


, NULL, nullptr


, NULL nullptr, ((void*)0) -. NULL :


/*       NULL?       . */
int execl(path, arg1, arg2, (char  *) NULL);

/*    */
int execl(path, arg1, arg2, nullptr);

, Linux man 3 exec, .



— C. , .


, , , :


[[ oob_return_errno ]] int myabs (int x) {
    if(x == INT_MIN ) {
        oob_return_errno ( ERANGE , INT_MIN ) ;
    }
    return (x < 0) ? -x : x;
}

oob_return_errno. , - :


  1. (struct {T return_value; int exception_code}).
  2. , .
  3. .
  4. errno, .

, :


bool flag;
int result = oob_capture(&flag , myabs , input) ;
if (flag) {
    abort ();

flag, errno . , , .


, , , , .



«Effective C» Hacker News. , . , , , - .


typeof


typeof . :


#define max(a,b)                                \
    ({ typeof (a) _a = (a);                     \
    typeof (b) _b = (b);                        \
    _a > _b ? _a : _b; })

(Martin Sebor), Red Hat , , .


.


defer


, Clang GCC, , , - .


C , cleanup(<cleanup function>):


int main(void)
{
    __attribute__((cleanup(my_cleanup_function))) char *s = malloc(sizeof(*s));
    return 0;}

(. Robert Seacord), «Effective C» , , defer Go:


int do_something(void) {
    FILE *file1, *file2;
    object_t *obj;
    file1 = fopen("a_file", "w");
    if (file1 == NULL) {
      return -1;
    }
    defer(fclose, file1);

    file2 = fopen("another_file", "w");
    if (file2 == NULL) {
      return -1;
    }
    defer(fclose, file2);

    /* ... */

    return 0;
  }

fclose file1 file2 do_something.


!



C — : , , .


C . 20 . , , - .


, , Valgrind C!


PS , " ". mikeus , , : Modern C.


All Articles