C question
Types
#include <stdio.h>
#include <stdint.h>
 
int main(void) {
   int dir = -12;
   printf("%u\n", dir);
}
- 
 A 4294967284. Мы указали printf() считать dir как безнаковую переменную  
 
#include <stdio.h>
#include <stdint.h>
 
int main(void) {
   int32_t dir = 0x01 << 2;
   printf("%d\n", dir);
}
- 
 A Десятичные 4, сдвиг бита на 2 позиции влево  
 
 
Memory
#include <stdio.h>
#include <stdlib.h>
 
struct descr {
    int id;
};
 
int main(void) {
  struct descr* d;
  for (int i = 0; i < 1024; i++) {
      d = malloc(sizeof(struct descr));
      d->id = i;
      printf("%d\n", d->id);
  }
}
- 
 A Программа корректна, но низкого качества: отсутствует вызов free(), освобождающий память, отсутствует проверка на возвращаемое malloc() значение 
 
#include <stdio.h>
 
void print(void) {
    char* foo = "qwerty123456";
    printf("%s\n", foo);
}
 
int main(void) {
  print();
}
- 
 A DATA (RODATA), сегменте инициализированных данных
 
 
Arrays
#include <stdio.h>
 
int main(void) {
   char* str = "qwerty123456";
   printf("%s\n", str[3]);
}
- 
 A Код является некорректным, так str[3] является char 
 
 
Structures
#include <stdio.h>
#include <stdint.h>
 
struct descr {
    int32_t id;
    int64_t n1;
    int64_t n2;
};
 
int main(void) {
  printf("%lu\n", sizeof(struct descr));
  printf("%lu\n", sizeof(int32_t) + sizeof(int64_t) * 2);
  return 0;
}
- 
 A В общем случае для x64 выведет 24 и 20. Компилятор производит оптимизацию размещения в памяти, после int32 будет пропущено 4 байта (для 64 битной архитектуры)  
 
 
Funcions
#include <stdio.h>
#include <stdint.h>
 
void rotate(int32_t* var) {
    var += 1;
}
 
int main(void) {
   int32_t dir = 12;
   rotate(&dir);
   printf("%d\n", dir);
}
- 
 A printf() выведет 12, поскольку в функции rotate() присходит прибавление к адресу переменной dir  
 
#include <stdio.h>
#include <stdint.h>
 
struct descr {
  int32_t dir;
};
 
void rotate(struct descr* descr) {
    descr->dir++;
}
 
int main(void) {
   struct descr d;
   d.dir = 12;
   rotate(&d);
   printf("%d\n", d.dir);
}
- 
 A  printf() выведет 13, код корректный  
 
#include <stdio.h>
#include <stdint.h>
 
void rotate1(int32_t* var) {
    var += 1;
}
 
static void rotate2(int32_t* var) {
    var += 1;
}
 
int main(void) {
   int32_t dir = 12;
   rotate1(&dir);
   rotate2(&dir);
   printf("%d\n", dir);
}
- 
 A Определение (entry)  для rotate1() будет присутствовать в obj файле, rotate2() - нет  
 
#include <stdio.h>
 
typedef struct  {
  char* message;
} descr_t;
 
void run(const descr_t* descr) {
    descr->message = "qwerty";
}
 
int main(void) {
   descr_t descr;
   run(&descr);
   printf("%s\n", descr.message);
   return 0;
}
- 
 A В общем  случае нет, поскольку аргумент в run() объявлен как константа и это запрещает модификацию структуры
 
 
Operation system
Mutexes and semaphores
IPC