learning-c/main.c

188 lines
No EOL
6 KiB
C

// Welcome to my journey of learning the fuckass language that is C
//importing basic modules
#include <stdio.h>
#include <stdlib.h>
#include "dvorakmsdos.h"
#ifdef _WIN32
#include <conio.h>
#define GETCH() _getch()
#else
#include <termios.h>
#include <unistd.h>
char getch() { // Fucking painful why is this piece of shit needed on UNIX Based/Unix-like systems 🫩
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt); // save old settings
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO); // disable buffering and echo
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // apply new settings
char ch67 = getchar(); // read one char instantly
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // restore old settings
return ch67;
}
#define GETCH() getch()
#endif
// STRUCTURE TEST
struct man {
char firstname[20];
char surname[20];
int age;
int weight;
char relationship_status[10];
};
// UNION TEST
union woman {
int age;
int weight;
};
// FUNCTION TEST
int multiply(int num_one, int num_two) {
int result = num_one*num_two;
return result;
}
// MAIN FUNCTION
int main() {
// HELLO WORLD SECTION
printf("HELLO WORLD BELOW:\n"); // DIVIDER
char first[] = "Hello";
char second[] = "World";
printf("%s %s", first, second);
// IF/ELSE STATEMENT
printf("\n\nIF/ELSE STATEMENT BELOW:\n"); // DIVIDER
int num = 67;
if (69<num) {
printf("69 is smaller than %d ", num);
} else {
printf("69 is larger than %d ", num);
}
// SWITCH STATEMENT
printf("\n\nSWITCH STATEMENT BELOW:\n"); // DIVIDER
int meme = 1337;
switch (meme) {
case 13:
printf("she said she was twelve!");
break;
case 21:
printf("haha 9+10");
break;
case 67:
printf("lolol 67");
break;
case 69:
printf("nice");
break;
case 420:
printf("weed..");
break;
case 1337:
printf("you are elite");
break;
case 80085:
printf("do NOT put this number into a calculator");
break;
default:
printf("Sorry I do not recognise this meme");
break;
}
// WHILE LOOP
printf("\n\nWHILE LOOP BELOW:\n"); // DIVIDER
int a=1,b=7;
while (a!=b) {
a++;
b-=1;
printf("int a=%d AND int b=%d\n", a, b);
}
// FOR LOOP
printf("\n\nFOR LOOP BELOW:\n"); // DIVIDER
for (int i=0; i<=3; i++) {
printf("i=%d\n", i);
}
// FUNCTION
printf("\n\nFUNCTION (MULTIPLY) BELOW:\n"); // DIVIDER
printf("%d * %d = %d", 5, 30, multiply(5,30));
// ARRAY
printf("\n\nARRAY BELOW:\n"); // DIVIDER
int arr[] = {21, 67, 69, 420};
printf("arr[0]=%d\narr[1]=%d\narr[2]=%d\narr[3]=%d", arr[0], arr[1], arr[2], arr[3]);
// POINTER
printf("\n\nPOINTER BELOW:\n"); // DIVIDER
int victim=7;
int *victim_pointer=&victim;
printf("pointer of int victim is: %p\n", victim_pointer);
int derefrencing_operator=*victim_pointer;
printf("derefrencing operator of *victim_pointer is: %d\n", derefrencing_operator);
int **double_ptr = &victim_pointer;
printf("double pointer of the &victim_pointer is: %p", double_ptr);
// STRING
printf("\n\nSTRING BELOW:\n"); // DIVIDER
char message[22]="Hello, world. Again...";
printf("%s", message);
// STRUCTURES
printf("\n\nSTRUCTURE (man) BELOW:\n"); // DIVIDER
struct man PETER={"Peter", "Griffin", 35, 150, "married"};
printf("Our man is named %s %s, he is %d years old, he weights %dkg and his relationship status is %s.", PETER.firstname, PETER.surname, PETER.age, PETER.weight, PETER.relationship_status);
// UNIONS
printf("\n\nUNION (woman) BELOW:\n"); // DIVIDER
union woman LOIS;
LOIS.age = 34;
LOIS.weight = 60;
printf("Our woman is unknown, she is not %d years old and she weights %dkg. (LOIS.age and LOIS.weight).", LOIS.age, LOIS.weight);
// PRESS ANY KEY TO CONTINUE
printf("\n\nPress any key to continue...");
fflush(stdout); // this forces the buffer to print NOW, fixing the "bug" in which it prints out everything AFTER the next line (scanf)
GETCH();
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
// INPUT (BUT ONLY FOR ONE WORD! STOPS WHEN YOU PUT IN A SPACE)
printf("\n\nINPUT BELOW:\n"); // DIVIDER
char fruit[50]; //MAX 50 CHARACTERS
printf("Pick a fruit (SINGLE WORD, EVERYTHING ELSE GETS IGNORED): ");
fflush(stdout); // same use case as above in the first input part of the code
scanf("%s", fruit);
printf("\nYou choose %s as your fruit!", fruit);
// INPUT (LARGER AMOUNT OF WORDS COMPARED TO SCANF)
printf("\n\nINPUT (larger) BELOW:\n"); // DIVIDER
char vegetable[50];
printf("Pick a vegetable to go in the salat: ");
fflush(stdout); // same use case as above in the first input part of the code
while (getchar() != '\n') {} // clears the input buffer so it doesnt print out excess input from the fruit one
scanf(" %49[^\n]", vegetable); // the "[^\n]" means to read everything except a newline, which means you can input spaces inside now, 49 makes it so it doesnt crash once it has more than 50 characters
printf("\nThe %s will be a perfect choice for this salat!", vegetable);
// END OF THE MAIN.C CODE, HERE YOU WILL BE ABLE TO CONTINUE ON TO OTHER EXPERIMENTS OF MINE
printf("\n\nSWITCH OVER TO\n"); // DIVIDER
printf("1 = DvorakMSDOS.C, 2 = exit\n");
while (getchar() != '\n') {} // same use case as above in the first input part of the code
while (1==1) {
fflush(stdout); // same use case as above in the first input part of the code
char chc = GETCH();
if (chc == '1') {
msmain(); // calls dvorakmsdos.c
return 0;
} else if (chc == '2') {
return 0;
} else {
printf("invalid character, try again!\n");
}
}
}