DEV Community

Amirshokh
Amirshokh

Posted on • Edited on

Язык программирования Си. Обзор(Synopsis)

Book

Computer Science Studies, [04.05.2022 0:57]
#include <stdio.h>

int main(void)
{
    typedef int arr_type1[4];
    typedef arr_type1 arr_type2[3];
    typedef arr_type2 arr_type3[5];
    arr_type3 some_array;
    printf("%zd", sizeof some_array / sizeof(int));
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [04.05.2022 1:01]
#include <stdio.h>

int main(void)
{
    int arr[3][3] = { 0, [0][1] = 1, [1] = 10, [2] = 20, 30 };
    int (* ptr)[3] = arr;
    int * ptr1[] = { ptr[1], &(* (ptr + 2))[0], (* (ptr + 2) + 1) };
    int ** ptr2[] = { [5] = ptr1 };
    int a = **(* (ptr2 + 5) + 1);
    printf("a = %d", a);
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [04.05.2022 1:01]
#include <stdio.h>

int main(void)
{
    int arr1[0];
    int * ptr1 = arr1;

    int arr2[0] = { 12 };
    int * ptr2 = arr2;

    if (ptr1 == ptr2)
        printf("Statement is true.");
    else
        printf("Statement is false.");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [04.05.2022 1:03]
#include <stdio.h>

void main();

void main(void)
{
    int (((max))());
    printf("%d", (int)((max)(5, 3, 9)));
    return;
}

int max(a, b)
    int a, b;
{
    return a > b ? a : b;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [04.05.2022 1:08]
#include <iostream>

template <typename T>
class Array
{
private:
  size_t m_size;
  T* m_ptr;
public:
  Array(size_t size) : m_size(size) { m_ptr = new T[size]; }
  ~Array() { delete[] m_ptr; }
  T& operator [](size_t index) { return *(m_ptr + index); }
};

int main()
{
  size_t size = 10;
  Array<int> arr(size);
  arr[1] = 0;
  std::cout << arr[1];
  return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [09.05.2022 12:04]
#include "stdio.h"

void main(void)
<%
    #define str1 "Side A"
    char side_a<::> = str1;

    char notstr<::> = <%'H', 'E', 'L', 'L', 'O', '!'%>;

    #define str2 "Side B"
    char side_b<::> = str2;

    return (void)printf("%s", notstr + 4);
%>
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [09.05.2022 12:07]
#include <stdio.h>
#define str "Hello"

int main(void)
{
    const char * ptr = "Hello";
    if (str == ptr)
        printf("Statement is true");
    else
        printf("Statement is false");
}
Enter fullscreen mode Exit fullscreen mode

Computer Science Studies, [09.05.2022 12:09]
Знание Си среди господства сиподобных языков, да ещё и на продвинутом уровне, это всё равно, что знать хорошо древнюю латынь, пусть и с малой лексикой, но с общими корнями. Вот C++ уже выступает как современная латынь. Её как бы очень мало где используют, но тем не менее она развивается наряду с остальными современными языками. Но вот в чём польза знания латыни, знают только те, кто знают эту самую латынь.
Если же уже попробовать найти аналогию на язык англосаксонов, ставший лингва-франка 21 века, то есть английский, то в голову первым делом приходит Python.

Computer Science Studies, [10.05.2022 6:02]
#include <stdio.h>
int n;
int main(void)
{
    printf("%d\n", n);
    int n = 8;
    for (int n = 1; n < 3; n++, printf("%d\n", n))
    {
        printf("%d\n", n);
        int n = 6;
        n += n;
        printf("%d\n", n);
    }
    printf("%d\n", n);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [25.05.2022 0:46]
#include <stdio.h>

int main(void)
{
    long long arr[5] = {1, 2, 3, 4, 5};
    printf("%d", sizeof (4)[arr]);
    return 0;
}

Computer Science Studies, [25.05.2022 0:51]
static unsigned long int next = 1;

int rand(void)
{
    next = next * 1103515245 + 12345;
    return (unsigned int) (next / 65536) % 32768;
}

void srand(unsigned int seed)
{
    next = seed;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [25.05.2022 1:00]
struct FILE
{
    struct FILE * fp;
    char * file_name;
    int error_code;
    int is_eof;
    char buffer[512];
    char * buffer_set;
    int copied_bytes;
    int buffer_position;
};
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [09.06.2022 4:17]
#include <stdio.h>

int main(void)
{
    if (sizeof 'A' == sizeof 2U == sizeof "")
        printf("Statement is true!");
    else
        printf("Statement is false!");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [09.06.2022 4:20]
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    struct book { char (* (name)); } (book1) = (struct book) { .name = { {"H" "e"}, {"l" "l" "o"} } };
    {
        (fprintf(stdout, "%zd\n%s", (sizeof(book1)), ((book1).name)));
    }
    return EXIT_SUCCESS;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [10.06.2022 12:39]
#include <stdio.h>
#include <string.h>

#define PTR_DIGITS 16

int main(int argc, char ** argv)
{
    int var1, var2;
    int * ptr1 = &var1, * ptr2 = &var2 - 1;

    char str1[PTR_DIGITS + 1];
    sprintf(str1, "%p", ptr1);
    char str2[PTR_DIGITS + 1];
    sprintf(str2, "%p", ptr2);

    printf("%.*s!", printf("Statement is "), strcmp(str1, str2) == 0 ? "true" : "false");
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [11.06.2022 17:23]
#include <stdio.h>

int fun(int i) { return i; }
int (* return_fun(int (* ptrtofun)(int)))(int) { return ptrtofun; }

int main(void)
{
    int i;
    printf("Output%2$n%s", ": ", &i);
    printf("%d\n", return_fun(**fun)(i));

    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [11.06.2022 18:04]
#include <stdio.h>

int main(void)
{
    //def_arr == int arr[5]
    typedef int ARR_OF_5_INT[5];
    ARR_OF_5_INT def_arr;
    int arr[5];

    //def_ptr == * def_arr == int (* arr)[5]
    typedef ARR_OF_5_INT * PTR_TO_ARR_OF_5_INT;
    PTR_TO_ARR_OF_5_INT def_ptr;
    int (* ptr)[5];

    //def_arr_of_ptr == def_ptr[10] == (* def_arr)[10] == int (* (arr[10]))[5]
    typedef PTR_TO_ARR_OF_5_INT ARR_OF_10_PTR_TO_ARR_OF_5_INT[10];
    ARR_OF_10_PTR_TO_ARR_OF_5_INT def_arr_of_ptr;
    int (* (arr_of_ptr[10]))[5];

    _Bool statement = sizeof def_arr_of_ptr == sizeof arr_of_ptr;
    printf("%d", statement);
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [11.06.2022 18:16]
#include <stdio.h>

int square(int n) { return n * n; }

int main(void)
{
    typedef int fun(int);
    fun * f1 = **square;

    typedef int * (* ptrtofun)(int);
    ptrtofun f2 = (int (*)(int))square;

    if (f1 == f2)
        printf("Statement is true!");
    else
        printf("Statement is false!");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [13.06.2022 23:43]
Пример богатства синтаксиса. Далее компилируемый пример на C, где все функции одинаковы.

Computer Science Studies, [13.06.2022 23:43]
((a))(){}c(int(b)){}b(b)int(b);{}(d)(d)int(d);{long;}int e(e)int(e);{}void*NULL=0;;;;char(str)[0]="\
";int(main)(int argc){.1+.2==.3;{}int;3;00000.3+9.4l+.5e+2;*(int*)NULL;*"A";'A'+9;&main;(~8)+1==-8;}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [14.06.2022 1:29]
#include <stdio.h>
#include <stdint.h>

union data_union {
    uint64_t data_var;
    struct data_struct {
        unsigned char _Alignas(_Alignof(uint32_t)) a;
        unsigned char _Alignas(uint16_t) b;
        uint16_t c : 4;
        uint16_t d : 4;
        uint16_t e;
    } data_struct;
} data_union = { .data_struct = 1, 2, 3, 4, 5 };

int main(void)
{
    uint64_t var = 0x0005004300020001;
    if (data_union.data_var == var)
        puts("Statement is true");
    else
        puts("Statement is false");
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [16.06.2022 20:42]
#include <stdio.h>

#define SQUARE1(X) X*X
#define SQUARE2(X) (X*X)
#define SQUARE3(X) (X)*(X)
#define SQUARE4(X) ((X)*(X))
int square5(int x) { return x * x; }

int main(void)
{
    int x = 2;
    int y1 = SQUARE1(x + 3);
    int y2 = 100 / SQUARE2(x);
    int y3 = 100 / SQUARE3(x + 3);
    int y4 = SQUARE4(++x);
    int y5 = square5(x++);
    printf("%d\n", x + y1 + y2 + y3 + y4 + y5);
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [18.06.2022 0:05]
#include <stdio.h>

#undef __DATE__
#ifndef __DATE__
    #define LINE
#endif

#if defined (__LINE__)
    #define LINE __LINE__
    #if __STDC_VERSION__ == 199901L
        #line 11
    #elif __STDC_VERSION__ == 201112
        #line 13 "main.c"
    #else
        #error Required C99 or higher
    #endif
#endif

int main(void) { printf("%d\n", LINE); }
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [18.06.2022 0:33]
#include <stdio.h>

#define TYPENAME(X) _Generic((X), char: "char", short: "short", int: "int", long: "long", long long: "long long", float: "float", double: "double", long double: "long double", default: "other")

#define PRINT_TYPENAME(X) printf(#X "\tis %s\n", TYPENAME(X))

int main(void)
{
    PRINT_TYPENAME('A');
    PRINT_TYPENAME(*"");
    PRINT_TYPENAME(03L + -1UL);
    PRINT_TYPENAME(+4ULL * 3.0f);
    PRINT_TYPENAME(1E6L);
    PRINT_TYPENAME((short)5 + (short)1);
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [19.06.2022 20:56]
#include <stdio.h>
#include <tgmath.h>
#include <string.h>

#define DIGITS 20

int main(void)
{
    char num1[DIGITS];
    sprintf(num1, "%.16f", sqrt(2.F));
    char num2[DIGITS];
    sprintf(num2, "%.16f", (sqrt)(02.f));

    //√2 == 1,414213562373095...
    printf("%s %s %s\n", num1, strcmp(num1, num2) == 0 ? "==" : "!=",  num2);
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [19.06.2022 20:57]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void rip() { puts("Rest in Peace."); }
void _Noreturn static _Noreturn inline inline dead(int bullet) { atexit(atexit(rip)); exit(bullet); }
void invitation() { puts("Try once more."); }
void alive() { puts("Unfortunately, you are alive."); }

int main(void)
{
    srand(time(0));
    puts("Welcome to the Russian Roulette!");
    if (rand() % 6 == 0) dead(1);
    atexit(atexit(invitation));
    atexit(alive);
    puts("Have got into a frenzy?");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Computer Science Studies, [19.06.2022 20:57]
#include <stdio.h>

int main(void)
{
    int a = 5, b = 2;
    struct c { int c; } c = { 6 };
    void * p[] = { [1](int *)&a, [0]&b, [2]&c };

    printf("%d %d %d", **(int **)p, *(int *)p[1], ((struct c *)*(p + 2))->c);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)