C Programming from easy to hard - Tutorial 01 - IntroductionsteemCreated with Sketch.

in tech •  2 years ago  (edited)

C Programming from easy to hard Tutorial 01

img

First things first:

I won't try to explain everything in a 600 pages course, this is a course for people who:

  • Are pretty good at computers in general (No previous programming required)
  • Don't have issues typing at the computer
  • Are not afraid to try new things (Most Important)
  • Are especially intrigued by logical puzzles, since in the end this is what programming is. You don't need to be a math genious, you just need to understand basics. (It's important to note that math required in programming is logical math not the kind of math you will ever learn in school.)
  • If you're already a little familiar with C you should skip the first 3 tutorials at least!

So you want to try C programming, you're either used to other newer languages that make things simpler yet harder since the easier the language the harder it is to understand how everything works. Or you maybe want to start programming the hard way, in either case, I support you. I must state that at first it will seem extremely unfriendly everything will look alien to you, it's only natural and it should not frighten you.

The first step in any programming language, the compiler (Or in some newer programming languages, interpreter)

I honestly am against complex programming environments while you're learning. You might have heard of Microsoft Visual Studio, Bloodshed Dev-C++, Codeblocks, whatever.
It all seems easy, great. It won't be easy when you need to switch to Codeblocks from bloodshed dev C++ once you get in a real working environment and everyone is used to a different environment.

For Windows

It's a bit more complex but bear with me. This simple knowledge is invaluable to anyone who will ever actually make money off programming.

Download and install mingw (Or minimalist GNU for windows/GNU stands for Gnu's not UNIX, that old Operating System.)
Once you have installed it, make sure to search in the windows search bar for mingw installation manager, access it and open up the Mingw Base System -> Mingw Compiler Suite -> mingw32-gcc
img
Check the box, apply the changes(TOP LEFT MENU: Installation->Apply changes)
Good, now you have to add this to your system's path.
Go to your system's properties (Or search for environment path in windows 10)
Go to Enviroment variables
Select the path in SYSTEM variables (NOT USER VARIABLES!!!!)
Add C:\MinGW (Or of course wherever you decided to install mingw)
Apply the changes, and you're good to go.
To check if the compiler is working properly, open up a terminal/Command prompt and type in gcc then press enter.
it should say something like : ERROR, no imput file(s)!, in which case everything is all right. Otherwise go back and see where something went wrong.

For Linux (Ubuntu)

Open up a terminal, enter sudo apt-get install gcc
install it then the commands are the same as windows so follow them from there on.

Your programming environment

You'll need a text editor such as Notepad++ Or, if you're on Linux Notepadqq(same thing really!)
You can customize the theme so it's not too white for your eyes color your font the way you want etc. it's pretty nifty.
second thing is the file manager and of course the console/terminal (Command prompt on windows, Terminal on Linux)
Really any IDE you will find will be an combination of these things in the end all of them will be an alternate look of this:
img

Make a folder, Wherever you want in which you will make your very first program. A standard Hello World program.
Inside it make a text file and name it main.c (Make sure your file extensions are shown otherwise it will end up being main.c.txt!)
Write the following inside

#include <stdio.h>
int main()
{
    printf("Hello World!");
    return 0;
}

Compiling your program:

That's your first program, but it's just text or human readable code, we need to compile it to an executable that your computer can understand. (Don't worry I'll explain what does what very soon.)
Now go to your command prompt or terminal and type in cd "path to your main.c" without the ""
(If you're not familiar with command prompt, put the folder with the file on your desktop and type in cd Desktop after you open a console. then type in "cd foldername" where foldername is the name of the folder that holds main.c)
Once you've reached the path to your main.c type in gcc main.c -o T01
It should compile successfully if you've written everything exactly as written above in the file.
Even a misplaced comma can break an entire program, this is the strictness we deal with in programming.
img
The program should output "Hello World" then close.
Of course once you've compiled it there will be an T01.exe made near the main.c and you could double click it but it would close too fast for you to see the output. (or perhaps at the time you're reading this, even see the command prompt open.
For now however we are focusing on the console.

Congratulations! You've made your very first program in C!

Let's study how it works:
#include <stdio.h> - contains basic functions like printf for example, every commmand like printf is located in such a library.
int main() - the function at which your program starts, you can not have a program without this! your program will connect to anything else you do through this function.

It seems extremely simplistic, try playing with it, and i'll also show you how to have it write on a new line too "\n".

main.c
#include <stdio.h>
int main()
{
    printf("Hello World!\nThis is my first program!\n");
    printf("I'm on my way to becoming a programmer!");
    return 0;
}

Putting a "\n" tells the printf function that you want to write on the next line not on on the same one.
Now, having one printf next to another is pretty redundant really although it looks simpler now over time it's just more typing for no reason, I'll show you how to write it so it reads the next line in code too.

#include <stdio.h>
int main()
{
    printf("Hello World!\n"\
           "This is my first program!"\n);
           return 0;
}

Now you know how to write messages in order. I'll continue this lesson explaining a few basic things.
Everything probably looks weird, so here's a little more translation into english
#include <library_here.h> - accesses a file included in your c compiler installation, containing a few basic tools like printf for example.
int main() - is the main function what's inside the brackets or in between are other commands/functions to be executed.
for example:

#include <stdio.h>
int hello()
{
    printf("Hello World!");
}
int main()
{
    hello();
    return 0;
}

printf("message") - printf("message to write to console/terminal/etc.")-will get more into it later, I need to show you the variable types first.
and return 0 - basically closes the program, we'll get into this later as it is too complex for someone just starting output
After the end of every command, not function, you add a ";" telling the compiler your command ended.
It does not apply for functions hence the lack of it after closing the brackets "{}"

It should be noted programming is similar to logical math, or puzzles really that's what it is. you're placing lego blocks on top of each other to let the user see a house for example. The user isn't aware of the composition of the blocks he just sees the final view.

Say you need to remake notepad for example, you have a little piece of code for saving one for loading one for displaying the text one for not losing text on reboot etc. You do have the necesarry tools you're being helped by both the operating system and programming languages and toolkits to make your job easier, you're just connecting them together.

Now we need to get at variable types, they are as follows:

int = number such as 123
float = number such as 123.0001
double = number such as 123.0000001
char = a character such as "A" or "a" plus symbols such as "/[email protected]#*(%&)"
if you need a simple number you use int, if you need more precision you use float if you need even more precision you use double.
A few mathematical examples:

/*By the way this is a comment, a piece of text the compiler ignores but you can keep notes inside code this way!
It's really easy!*/
//Or use this for a single line comment!
#include <stdio.h>
int main()
{
    int a = 2; //This is how we declare and assign a value to an integer or an int
    int b; // this is how we declare an int, it has no value assigned to it yet.
    b = 0 // we make sure it's value is 0 so no random memory errors appear.
    int c = a + b; //we can also declare and assign the result of an operation such as in this case a (2) + b (0)
    printf("A = %i\n", a); //&i in printf expects a variable of type int after the text ("&i", a) where a is the one we declared earlier and assigned 2 to it.
    printf("B = %i\n"\
           "C = %i which is the result of A + B", b,c); // this is how you would structure it correctly so you don't open up 100 printfs consuming more memory and basically wasting space.
}

You can do the same for addition substraction multiplication and division with no issue, there's a math library for more advanced math such as pow() (2 at the power of 2), sqrt() (square root of x) etc.
Notice where I assigned the value of 0 to b, it's necesarry in C to keep in mind not to use any unassigned values.

Unassigned values unless specified by you will cause memory errors, these are your worst enemy.

img
if I don't assign 0 to b, it might be 0, it might be 1, it might be 1020121 and so on, it's random, because it accesses whatever value was previously used in the space of the memory address that int b was assigned.
You don't need to worry much about this for now, I'll get to it when we have to use pointers and allocate memory.

That's it for now, since at school you've noticed everyone makes you do 20 problems of one type then another 20 of another type (Addition, substraction, etc.) slowly getting to the more complicated stuff that's how you should work when learning c++ for the first time.
Play around, make one function that says hello world, another that says hello then good bye them being in two separate functions.
Right now you only have the ability to display and make basic math with text. play around it doesn't matter what the text says the point is you must get used to the syntax and writing code.
The next tutorial will show you more on how to also receive input from a client, and make some decisions based on that input automatically.

Screenshots, title, mistakes, etc. are of my own work along with everything written here.
If you've found this helpful please make sure to img and upvote for more tutorials!
Any inspirations, mostly jokes are reffered here:
commapic
memerr

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

great tutorial, this might just be what i need to start programing!

Glad you liked it, the notepad/command prompt/console is optional after you master them really it's just so you know how they work that matters you can use any IDE such as visual studio etc.

ty for the tutorial. Nice little hello world!

Create a great day,
@kozan