Post

LANG_C

malloc

void *malloc( size_t size );

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdlib.h>

int main() {
    int *ptr;
    ptr = (int*)malloc( sizeof( int ) ); // Выделение памяти под один int
    if (ptr == NULL) {
        // Обработка ошибки выделения памяти
    } else {
        // Использование выделенной памяти
        *ptr = 10;
    }
    free(ptr); // Освобождение выделенной памяти
    return 0;
}

while

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main( void ) {
  int s = 0, x = 1;

  while( scanf( "%d", &x ) == 1 && x != 0 )
    s += x;

  printf( "s = %d\n", s );

  return 0;
}

Условная компиляция

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define LANG_C
 
#if defined(LANG_C)
#   include <stdio.h>
#else
#   include <iostream>
#endif
 
int main( void )
{
         int x=5;
#ifdef LANG_C
         printf("%d\n", x);
#else
         std::cout << x << std::endl;
#endif
 
    return 0;
}
This post is licensed under CC BY 4.0 by the author.