c-is-stressful
c-is-stressful
Wordy ass C tutorial in idiot terms
5 posts
I hate my life, but I hate C programming more. So, I'll try to make others hate it a little less (:
Don't wanna be here? Send us removal request.
c-is-stressful · 2 years ago
Text
C Basics
Let's create a C file. Create a new folder, and create a file with the extension '.c'. In example, if you name your file 'test', it will be named 'test.c'.
If you're using visual studio code, press ctrl+K, then press M immediately after. Then, pick C from the box that appears. You've now successfully created a C file.
Libraries
C programs use libraries. In general, there are 2 ways you can access libraries.
The C standard library These are installed automatically, assuming you've set C up properly.
External libraries These need to be installed externally.
To include the library, you type
#include <libraryname.h>
Where libraryname is replaced with the name of your library. For instance, this is how I include the library 'stdio.h'.
Tumblr media
There shouldn't be a red squiggle. If it appears, it means you've done something wrong in the installation.
Now, you're able to access everything in the library.
For the sake of convenience, I'll list some common functions that will almost certainly be used regardless what program you're writing, along with their associated libraries.
stdio.h
printf
exit
assert.h
Assert
Honestly, that's it. More about it in the future.
The actual program
The program that you run will be written in a main() function. Normally, it looks like this.
Tumblr media
The words in the /* */ are called comments. They are only there to explain code. In general, you should use them to explain why your code has to be this way.
The 'main()' function must be written in this way. The 'int' before the 'main()' is there to declare the return type of your function.
To draw an analogy, let's say you have a machine. You put in money, and you take out food. The machine dispenses soda and snacks in separate slots. You can't really dispense snacks from the soda slot, and vice versa. So, before you want to buy something, you must pick whether you're buying soda or snacks, so that the machine will not dispense things into the wrong slot.
Picking whether you're buying soda or snacks is equivalent to the process of declaring a return type.
The 'return' statement is not strictly necessary. Compilers are essentially required to insert it in for you if you forget, but it's always good to remember to add it in. Why? Don't know. Because of the above, nothing happens when you don't add it. Just decide wahtever you want.
Returning a value of 0 would tell the PC that the program ended successfully. If anything else (Usually 1) is returned, it means the program did not end successfully.
Now, let's actually do something. First, let's include stdio.h
Then, let's print something to terminal. The function to print something to terminal in C is printf().
Let's add it into main.
What should we print? Let's go with 'C is stressful'. We can do it by including C is stressful in the paranthesis behind printf.
Tumblr media
Remember to terminate your statement with a semicolon. This compulsory. Now, as for compiling.
Press ctrl+~. This will bring up a window labelled TERMINAL.
Tumblr media
Don't mind if you don't have the top part. This is a part of something the owner of this laptop installed on their powershell terminal.
I'll go through this only once. If you need to go through more times, just read it again.
If you opened powershell with ctrl+~, you will already be in the working directory of your project. You can skip the step of changing to your working directory.
So, you can directly start with
gcc filename.c -o filename.exe
Tumblr media
For me, I have a file Tutorial.c in the working directory Tutorial. Now, I've compiled it into a file called Tutorial.exe.
So, to run your file
./filename.exe
Tumblr media
Look, now, C is stressful is printed into powershell.
To substitute a string into a file, you can add %s into the string, followed by a comma and the string you want to substitute.
Tumblr media
In this case, printf prints C is stressful. You can do more than one of these.
Tumblr media
Printf substitutes the extra arguments in chronological order. This case also prints C is stressful.
You can also include other special characters, like '\n' which moves on to next line. It's equivalent to pressing enter on your keyboard. \
I'm putting so much effort explaining this because you'll need it to show that your code runs correctly. It's better to see an output and confirm that it's correct than to just see that your main() returns 0 and trust that it does what you expect it to.
0 notes
c-is-stressful · 2 years ago
Text
Using C
C is a compiled language. This means that your computer will compile the language from a program to instructions for your machine.
To draw an analogy, you could think of a compiler as a translator. Let's say an American and a Chinese are talking to each other. Ignoring the fact that most Chinese can speak English, they can't really communicate. So, they need a middleman, a translator.
Now, let's replace the Americans with you, the Chinese with the computer, and the translator, the compiler. You can only understand programming languages, and the computer only understands computer instructions. The compiler helps convert your programming language to computer instructions.
There are many different compilers. Google it if you want to know. I'll use GCC on Windows. GCC stands for GNU Compiler Collection (Not GNU C Compiler, as stated by a few friends). This means that it can also compile other languages. That's one reason why I chose it. Reason 2 would be because it supports older architecture that other compilers don't. Reason 3 would be because there are many people who use it, so it is well documented.
Installation
To install GCC, I'll use MSYS2. You can download it here. On windows, its text should be something like msys2-x86_64-XXXXXXXX.exe, where each X is replaced with a number. This will only install the installer. Run the .exe file.
This tutorial is accurate as of February 2023.
Press Next on the screen titled Setup - MSYS2.
Tumblr media
It will bring you to a screen titled Installation Folder. I personally recommend just installing it to C:\msys64 because it's more convenient to access in the future. It's the default option. Press Next.
Tumblr media
It will bring you to a screen titled Start Menu Shortcuts. To explain what these are, press Winkey or Ctrl+Escape. Those icons you see are start menu shortcuts.
Do whatever you want (the default option is best if you don't know what you're doing), then press next.
Tumblr media
It'll show you a screen titled Installing MSYS2. There will be a loading bar. Once it's full, it's done installing. Wow, how amazing. Who would've thought. Wait for it to complete. It'll say
Installation Finished! All components installed.
when it's completely done.
Tumblr media
Don't worry about the box. It's what shows up if you press Show Details during the installation. It doesn't actually do anything to the installation itself.
Press next.
It'll bring up a screen titled Completing the MSYS2 Setup. Just press Finish. You need not bother unticking the Run MSYS2 now box, because we'll run it later immediately anyways.
A window called 'MSYS2 MSYS' should open. If it's not that, close it, open your start menu, open the folder MSYS2 and open 'MSYS2 MSYS'.
Tumblr media
Wait for this to appear.
Tumblr media
The green part should show your username and laptop name. Censored here because I'm doing this on a work PC that needs to install GCC.
Run
pacman -Syuu
Pacman is not referring to pacman the yellow circle, but short for package manager. This will be how you download and update things in the future.
Tumblr media
Once asked whether to continue with the installation, type 'y' and press enter.
Tumblr media
Once asked whether to complete the update, type 'y' and press enter.
Tumblr media
Open 'MSYS2 MSYS' again. You can either use the UI on the windows start screen, or just use search and type 'MSYS' into it. Type
pacman -Su
and press enter. Press 'y' when prompted.
Tumblr media
Now, type
pacman -S mingw-w64-ucrt-x86_64-gcc
Note: ctrl-v does not work here. Use shift-insert to paste the text into it, instead.
Press y when prompted.
Then, type
pacman -S base-devel
If asked to select what to install, just press enter to install all. Press y when prompted. Now, type
pacman -S development
If asked to select what to install, just press enter to install all. Press y when prompted.
This might take a while, because you're installing many packages.
Just in case, open windows Powershell, and type
gcc --version
and
make --version
If an error appears, gcc is not installed properly. Panic, and restart all steps from 'pacman -S mingw-w64-ucrt-x86_64-gcc'.
Search PATH. The search result should show Edit the system environment variables. Use administrator privileges. It brings up a screen titled System Properties. Click on Environment Variables. It brings up a screen labelled Environment Variables.
Under System Variables, double click 'Path'. It should bring up a screen called Edit Environment Variables. I will assume that you installed MSYS2 into C:\MSYS64, which is the default settings for installation location.
Press New. Type 'C:\msys64\usr\bin'
Press New. Type 'C:\msys64\mingw64\bin'
That should be enough. Press OK. Press OK. Press OK.
Code Editor
Regarding code editors, I'll use Visual Studio Code. It can be installed here. Open it. Once you've opened it, press ctrl+shift+X. Type 'C/C++' into the search bar. Download it. Type 'C/C++ Extension Pack' into the search bar. Download it.
VS Code has many features, including multiple cursors and an integrated terminal. But, this is out of scope already.
How to compile with GCC
Once you're done writing your code, save it, and open powershell. It doesn't matter if you are using the integrated terminal in VS Code, or the system one. Type
cd your/path/here
Where your path here is the path where your file is saved. For instance, if I have a file 'test.c' in my desktop, under user TestUser in C drive, I should type
cd C:\Users\TestUser\Desktop
This path is called the working directory of your project.
From here, you can type
gcc filename.c
Where you replace filename with the name of your file. For instance, if I have file test.c, I wolud type
gcc test.c
By default, the file is compiled to a executable file called 'a.exe'. To change the filename, you can type
gcc filename.c -o filename.exe
Where you replace filename with the name of your file. For instance, if I have file test.c and want file test.exe, I would type
gcc test.c -o test.exe
To run the code, you can either open it in file explorer, or simply type
./filename.exe
Where you replace filename with the name of your file. For instance, if I want to run the file test.exe through the console, I would type
./test.exe
OK, now, we can actually start learning C.
0 notes
c-is-stressful · 2 years ago
Text
Introduction to C
C is a compiled programming language. Which is to say, you must use a software called a compiler that converts your code into machine code. The specifics don't matter. You'll understand more as you get used to programming. The gist is: You write your code, hand it over to a compiler, and (assuming you have no or few errors), it will convert it into commands for your computer to run.
There are many different compilers, but the one I will be using will be gcc, and I'll use MSYS2 as a development and building platform. It is only valid for windows. Google if you're using linux or MacOS.
Compiled languages exist in contrast to interpreted languages, which use an interpreter to skip the compiling stage, going directly to the machine code.
C is a procedural programming language. In simple terms, it means each step is carried one by one, up to down. We'll ignore ways to do multiple things at once like threads for now. It'll probably be explored in the cstdlib library 'threads.h'. No clue for now, though.
C also has a static typing system. This means that every variable has a set type that generally cannot be changed. More on that later.
C also works on all systems. You must re-compile your program on each operating system for your code to work on it, though, because that's just how compiled programming languages are. A analogy would be like operating systems being people of different nationalities. Shouting Russian instructions at a German wouldn't bring across its meaning, shouting German instructions at a Chinese would just make them confused, and shouting Chinese instructions at a Russian would just get them to point the middle finger at you, then ignore you. (Source: Me)
Giving a file compiled on windows would be like shouting Russian at a german. Same for the other two major operating systems.
C is also very versatile. It can probably do everything, except for giving birth. Just kidding. If you program a model of a human body, render it, and make it give birth, you can make it give birth.
Whilst that's for the future when you have god-like programming skills, that hopefully gives the idea of just how versatile C is.
C also has a large community. If I don't do my job properly, you can just google it lmao. It'll probably be confusing at first, but that's your problem now. You can also ask a question on stack overflow or whatever.
Now, the important question:
Why C?
I don't know, honestly. I just looked through the unity menu, saw C#, and wanted to learn that. I also thought that programming languages were like pokemon, with an evolution path like C >> C++ >> C# or something.
Honestly, C is an easier language to learn. It's easier than C#, and far easier than C++.
Lmao, back to the point,
C is a universal language. See my point on different OS' and versatility.
C is efficient. It isn't the most efficient, but it's good enough.
C is portable. See my point on different OS'.
C is a well-established programming language. See my point on a large community.
C has an easy to read syntax, and is relatively easy to learn.
C programs are relatively short compared to other programming languages.
History of C
Unimportant. Google it if you're so curious.
Let's actually start learning it.
0 notes
c-is-stressful · 2 years ago
Text
Introduction to programming
Programming (also known as coding) is the process of writing instructions for a device such as a computer or mobile device. We write these instructions with a programming language, which is then interpreted by the device. These sets of instructions may be referred to by various names, but program, computer program, application (app), and executable are a few popular names.
A program can be anything that is written with code; websites, games, and phone apps are programs. While it's possible to create a program without writing code, the underlying logic is interpreted by the device and that logic was most likely written with code. A program that is running or executing code is carrying out instructions. The device that you're reading this lesson with is running a program to print it to your screen.
Programming Languages
Programming languages enable developers to write instructions for a device. Devices can only understand binary (1s and 0s), and for most developers that's not a very efficient way to communicate. Programming languages are the vehicle for communication between humans and computers.
Programming languages come in different formats and may serve different purposes. For example, JavaScript is primarily used for web applications, while Bash is primarily used for operating systems.
Low level languages typically require fewer steps than high level languages for a device to interpret instructions. However, what makes high level languages popular is their readability and support. JavaScript is considered a high level language.
The following code illustrates the difference between a high level language with JavaScript and a low level language with ARM assembly code.
Javascript:
let number = 10 let n1 = 0, n2 = 1, nextTerm; for (let i = 1; i <= number; i++) { console.log(n1); nextTerm = n1 + n2; n1 = n2; n2 = nextTerm; }
ARM assembly code:
area ascen,code,readonly entry code32 adr r0,thumb+1 bx r0 code16 thumb mov r0,#00 sub r0,r0,#01 mov r1,#01 mov r4,#10 ldr r2,=0x40000000 back add r0,r1 str r0,[r2] add r2,#04 mov r3,r0 mov r0,r1 mov r1,r3 sub r4,#01 cmp r4,#00 bne back end
Believe it or not, they're both doing the same thing: printing a Fibonacci sequence up to 10.
✅ A Fibonacci sequence is defined as a set of numbers such that each number is the sum of the two preceding ones, starting from 0 and 1. The first 10 numbers following the Fibonacci sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21 and 34.
Elements of a program:
A single instruction in a program is called a statement and will usually have a character or line spacing that marks where the instruction ends, or terminates. How a program terminates varies with each language.
Statements within a program may rely on data provided by a user or elsewhere to carry out instructions. Data can change how a program behaves, so programming languages come with a way to temporarily store data so that it can be used later. These are called variables. Variables are statements that instruct a device to save data in its memory. Variables in programs are similar to variables in algebra, where they have a unique name and their value may change over time.
There's a chance that some statements will not be executed by a device. This is usually by design when written by the developer or by accident when an unexpected error occurs. This type of control over an application makes it more robust and maintainable. Typically, these changes in control happen when certain conditions are met. A common statement used in modern programming to control how a program runs is the if..else statement.
✅ You'll learn more about this type of statement in subsequent lessons.
Tools of the Trade:
In this section, you'll learn about some software that you may find to be very useful as you start your professional development journey.
A development environment is a unique set of tools and features that a developer uses often when writing software. Some of these tools have been customized for a developer's specific needs, and may change over time if that developer changes priorities in work, personal projects, or when they use a different programming language. Development environments are as unique as the developers who use them.
Editors
One of the most crucial tools for software development is the editor. Editors are where you write your code and sometimes where you run your code.
Developers rely on editors for a few additional reasons:
Debugging helps uncover bugs and errors by stepping through the code, line by line. Some editors have debugging capabilities; they can be customized and added for specific programming languages.
Syntax highlighting adds colors and text formatting to code, making it easier to read. Most editors allow customized syntax highlighting.
Extensions and Integrations are specialized tools for developers, by developers. These tools weren't built into the base editor. For example, many developers document their code to explain how it works. They may install a spell check extension to help find typos within the documentation. Most extensions are intended for use within a specific editor, and most editors come with a way to search for available extensions.
Customization enables developers to create a unique development environment to suit their needs. Most editors are extremely customizable and may also allow developers to create custom extensions.
Popular Editors and Web Development Extensions:
Visual Studio Code
Code Spell Checker
Live Share
Prettier - Code formatter
Atom
spell-check
teletype
atom-beautify
www.sublimetext
emmet
SublimeLinter
Command Line Tools:
Some developers prefer a less graphical view for their daily tasks and rely on the command line to achieve this. Writing code requires a significant amount of typing and some developers prefer to not disrupt their flow on the keyboard. They will use keyboard shortcuts to swap between desktop windows, work on different files, and use tools. Most tasks can be completed with a mouse, but one benefit of using the command line is that a lot can be done with command line tools without the need of swapping between the mouse and keyboard. Another benefit of the command line is that they're configurable and you can save a custom configuration, change it later, and import it to other development machines. Because development environments are so unique to each developer, some will avoid using the command line, some will rely on it entirely, and some prefer a mix of the two.
Popular Command Line Options:
Options for the command line will differ based on the operating system you use.
💻 = comes preinstalled on the operating system.
Windows
Powershell 💻
Command Line (also known as CMD) 💻
Windows Terminal
mintty
MacOS
Terminal 💻
iTerm
Powershell
Linux
Bash 💻
KDE Konsole
Powershell
Popular Command Line Tools
Git (💻 on most operating systems)
NPM
Yarn
Personal Recommendations
thefuck
vtop
fzf
wikit
All these extensions are beyond the scope of this blog.
Documentation:
When a developer wants to learn something new, they'll most likely turn to documentation to learn how to use it. Developers often rely on documentation to guide them through how to use tools and languages properly, and also to gain deeper knowledge of how it works.
This is where the idea that programmers just copy code from the internet comes from, I think.
Personal Documentation Recommendations:
Devdocs.io
Microsoft C++, C and Assembler
Text in blue is added by me. Text in white is copied from Web-Dev-For-Beginners. There's no real point in rewriting something that's already been written. (It's open source, so no copyrights)
Only posts that have a majority of copied text will have my own text written in blue.
0 notes
c-is-stressful · 2 years ago
Text
Regarding this tumblr
This will be a blog that explains things as simply as possible. It will not be concise, nor will it go too deep into advanced topics.
It'll only cover parts of the basic concepts of C, and it will teach you how to set up the environment for C in Visual Studio Code.
It'll also treat you like an idiot at times.
0 notes