#include <stdio.h>
#include <string.h>
#include <float.h>

void double_to_binary(double number, char binary_representation[65]) {
  // Преобразование указателя на double в указатель на целочисленный тип для доступа к битам
  unsigned long long int binary_bits;
  memcpy(& binary_bits, & number, sizeof(double));

  // Заполнение массива символов двоичным представлением
  for (int i = 63; i >= 0; i--) {
    binary_representation[63 - i] = ((binary_bits >> i) & 1) + '0';
  }

  // Добавление завершающего нуля
  binary_representation[64] = '\0';
}

void output(const char *expression, double n) {
  printf("Разбор выражения: %s\n", expression);

  printf("Значение в десятичной СС: %lf\n", n);
  unsigned long long int n_hex = * ((unsigned long long int * ) & n);
  printf("Значение в шестнадцатеричной СС: 0x%016llX\n", n_hex);
  char binary_representation[65];
  double_to_binary(n, binary_representation);
  printf("Значение в двоичной СС: %s\n", binary_representation);

  printf("\n");
}

void task1() {
  printf("Каковы 16-ричные коды значений 1, 0, DBL_MAX, DBL_MIN, DBL_EPSILON?\n\n");

  output("0", 0.0);
  output("1", 1.0);
  output("DBL_MAX", DBL_MAX);
  output("DBL_MIN", DBL_MIN);
  output("DBL_EPSILON", DBL_EPSILON);

  printf("\n\n");
}

void task2() {
  printf("Каков 16-ричный код значения выражения 1 + DBL_EPSILON?\n\n");

  output("1 + DBL_EPSILON", 1.0 + DBL_EPSILON);

  printf("\n\n");
}

void task3() {
  printf("Существуют ли в double положительные числа, меньшие DBL_MIN? Если существуют, то сколько их?\n\n");

  double smallest = DBL_MIN / 2.0;
  int count = 0;
  while (smallest > 0.0) {
    count++;
    smallest /= 2.0;
  }
  printf("Количество положительных чисел, меньших DBL_MIN: 2^%d\n", count);

  printf("\n\n");
}

void task4() {
  printf("Существуют ли в double положительные числа, большие DBL_MAX? Если существуют, то сколько их?\n\n");

  double larger_value = DBL_MAX + DBL_EPSILON;
  if (larger_value > DBL_MAX) {
    printf("Существуют положительные числа, большие DBL_MAX.\n");
  } else {
    printf("Положительных чисел больше DBL_MAX не существует.\n");
  }

  printf("\n\n");
}

void task5() {
  printf("Какое вещественное число записывается 16-ричным кодом 0000000000000001?\n\n");

  unsigned long long int hex_code = 0x0000000000000001;
  double * ptr = (double *) & hex_code;
  double value = * ptr;
  output("0x0000000000000001", value);

  printf("\n\n");
}

void task6() {
  printf("Равны ли между собой +0 и -0?\n\n");

  double positive_zero = 0.0;
  double negative_zero = -0.0;

  if (positive_zero == negative_zero) {
    printf("+0 и -0 равны между собой.\n");
  } else {
    printf("+0 и -0 не равны между собой.\n");
  }

  output("+0.0", positive_zero);
  output("-0.0", negative_zero);

  printf("\n\n");
}

void task7() {
  printf("Каковы 16-ричные коды значений +∞, -∞, NaN?\n\n");

  output("+∞", 1.0 / 0.0);
  output("-∞", -1.0 / 0.0);
  output("NaN", 0.0 / 0.0);

  printf("\n\n");
}

void task8() {
  printf("Отличаются ли коды «не чисел», получаемых путем различных некорректных вычислений?\n\n");

  double nan1 = 0.0 / 0.0;
  double nan2 = 1.0 / 0.0 - 1.0 / 0.0;
  unsigned long long int nan1_hex = * ((unsigned long long int * ) & nan1);
  unsigned long long int nan2_hex = * ((unsigned long long int * ) & nan2);

  if (nan1_hex == nan2_hex) {
    printf("Коды «не чисел» не отличаются.\n");
  } else {
    printf("Коды «не чисел» отличаются.\n");
  }

  output("0.0 / 0.0", nan1);
  output("1.0 / 0.0 - 1.0 / 0.0", nan2);

  printf("\n\n");
}

void task9() {
  printf("Как из DBL_MAX получить +∞ прибавлением 1?\n\n");

  double double_max = DBL_MAX;
  double * ptr = & double_max;
  unsigned long long * bits = (unsigned long long *) ptr;
  (*bits)++;
  output("DBL_MAX + 1", double_max);

  printf("\n\n");
}

void task10() {
  printf("Что получится, если к ∞ прибавить ∞?\n\n");

  double positive_infinity = 1.0 / 0.0;
  double result = positive_infinity + positive_infinity;
  output("∞ + ∞", result);

  printf("\n\n");
}

void task11() {
  printf("Что получится, если из ∞ вычесть ∞?\n\n");

  double positive_infinity = 1.0 / 0.0;
  double result = positive_infinity + positive_infinity;
  output("∞ - ∞", result);

  printf("\n\n");
}

void task12() {
  printf("Что получится, если число умножить на ∞?\n\n");

  double positive_infinity = 1.0 / 0.0;
  double result = 3 * positive_infinity;
  output("3 * ∞", result);

  printf("\n\n");
}

void task13() {
  printf("Что получится, если число поделить на ∞?\n\n");

  double positive_infinity = 1.0 / 0.0;
  double result = 3 / positive_infinity;
  output("3 / ∞", result);

  printf("\n\n");
}

void task14() {
  printf("Равна ли бесконечность бесконечности?\n\n");

  if (3.0 / 0.0 == 1.0 / 0.0) {
    printf("Бесконечности равны.\n");
  } else {
    printf("Бесконечности не равны.\n");
  }

  printf("\n\n");
}

void task15() {
  printf("Равно ли «не число» другому «не числу»?\n\n");

  if (0 * 1 / 0.0 == (1.0 / 0.0 - 1.0 / 0.0)) {
    printf("«не число» равно другому «не числу».\n");
  } else {
    printf("«не число» не равно другому «не числу».\n");
  }

  printf("\n\n");
}

void task16() {
  printf("Что больше – бесконечность или «не число»?\n\n");

  double positive_infinity = 1.0 / 0.0;
  double negative_infinity = - 1.0 / 0.0;
  double nan_value = 0.0 / 0.0;

  if (positive_infinity > nan_value) {
    printf("+∞ больше, чем NaN.\n");
  } else if (positive_infinity == nan_value) {
    printf("+∞ равна NaN.\n");
  } else {
    printf("+∞ меньше, чем NaN.\n");
  }

  if (negative_infinity > nan_value) {
    printf("-∞ больше, чем NaN.\n");
  } else if (negative_infinity == nan_value) {
    printf("-∞ равна NaN.\n");
  } else {
    printf("-∞ меньше, чем NaN.\n");
  }

  printf("\n\n");
}

void task17() {
  printf("Равно ли «не число» самому себе?\n\n");

  if (0.0 / 0.0 == 0.0 / 0.0) {
    printf("«не число» (0.0 / 0.0) равно самому себе.\n");
  } else {
    printf("«не число» (0.0 / 0.0) не равно самому себе.\n");
  }
  if ((0 * 1 / 0.0) == (0 * 1 / 0.0)) {
    printf("«не число» (0 * 1 / 0.0) равно самому себе.\n");
  } else {
    printf("«не число» (0 * 1 / 0.0) не равно самому себе.\n");
  }
  if ((1.0 / 0.0 - 1.0 / 0.0) == (1.0 / 0.0 - 1.0 / 0.0)) {
    printf("«не число» (1.0 / 0.0 - 1.0 / 0.0) равно самому себе.\n");
  } else {
    printf("«не число» (1.0 / 0.0 - 1.0 / 0.0) не равно самому себе.\n");
  }

  printf("\n\n");
}

void task18() {
  printf("Какому вещественному числу формата double соответствует двоичная последовательность, содержащая только единицы?\n\n");

  unsigned long long int binary_sequence = 0xFFFFFFFFFFFFFFFF;
  double result = * ((double * ) & binary_sequence);
  output("NaN", result);

  printf("\n\n");
}

void task19() {
  printf("Можно ли из 0 получить «не число» вычитанием 1?\n\n");

  double nan_value = 0.0;
  double zero = 0.0;
  double * ptr = & zero;
  unsigned long long * bits = (unsigned long long *) ptr;
  (*bits)--;
  output("NaN", zero);
  
  printf("\n\n");
}

int main() {
  task1();
  task2();
  task3();
  task4();
  task5();
  task6();
  task7();
  task8();
  task9();
  task10();
  task11();
  task12();
  task13();
  task14();
  task15();
  task16();
  task17();
  task18();
  task19();

  return 0;
} 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}