Learning C: Part 1, The basic structure/standard library for C!
h
MocaCDeveloper (530)

Introduction

Hi! I am MocaCDeveloper, you can call me Moca!

I am going to teach C to anyone who is willing to learn C(or is looking forward to learning C).
I will sectionize each part of the language, from the basic structure, to basic types, to other standard everyday used libraries within the language!

I will describe very thoroughly over everything I talk about, from what it does, to different ways you can use it to why you need it and when to use it!
Don't be afraid of being left behind in the dust if you get lost, comment the question you have below and I will answer you!
Don't be afraid to comment some suggestions to make the tutorial better either! I will take into mind any suggestion anyone has!

So, without further ado, lets Get Started!!

The basic "Hello World" program

So, in C, the standard(or rather basic) knowledge of the language is that there NEEDS to be a main function in order for the compiler to execute the code completely prone to any errors!
Note: The default compile on Repl is clang-7

Lets take a look at the "Hello World" program in C:

#include <stdio.h> // 1
// 2
int main() { // 3
    printf("Hello World"); // 4
    return 0; // 5
}// 6

Ok, confused yet? If you are, you're in luck, if you're not, stick with me!

Lets "interpret" this line by line!

Line 1

Line 1:

#include <stdio.h>

Line 1 uses what is called a "macro" in C. The "macro" #include is used to "import", or use a C library.
When using the #include "macro", you need to specify the header file name.
Now, in C, when you include another file, you are going to need to put the file name in between '<' and '>'.
Within the '<' and '>', you are going to be including what is called a "header file" into the file!
In C, a header file is a file with the file extension of '.h'. We will discuss header files later on, for now, just know that all header files in C need the '.h' file extension!

Now, what is the file 'stdio.h', and why do we need it?
Well, 'stdio.h' is the most standard library of C! It is literally used in almost every application/project you will see in C! The file 'stdio.h' is the standard I/O of the language. It allows you to print stuff to the screen and get input, as well as other stuff but the printing is the normal use of the header file.
We need this header file so that we can print stuff to the screen, it is commonly used to find bugs as well(using the printing method the header file contains). If you don't have this file, you cannot print or get input.

Additional information: The header file 'stdio.h' also allows you to use stdin and stdout to specify what you're getting. We will discuss this later on in this tutorial!

Line 3

Line 3:

int main() {

This is a interesting line, right?
This is what is called the "main function", or the function that enables our project to run properly.
Why do we need this?
The main function in C serves as a starting point for the execution, since the language is compiled.

The main function includes that of the following:
'int', 'main', '()' and '{}'.

Lets walk through each part of the setup of the main function:
First, the 'type declaration' of the function. Before I explain this, we need to know one thing(this is very common knowledge for the language C!):
C is a strictly typed language, meaning you need to specify the type of functions and variables being used!

Ok, lets continue shall we!
So, we need to first declare the 'type' of the function. In this case, we are declaring it as 'int'. We will explain why later! For now, just know that you use 'int' as the type declaration for the main function!

Next, the function name. The function name NEEDS to be 'main' for the project to start executing. One typo, the project will not run and give an error!

After the function name, like any other language, you need to put '()'. We will go over main function arguments later on, for now, just put a pair of parentheses.

And, finally, to 'start' the function, you need to put a pair of curly braces!

So, lets go over what we learned so far!

In a basic C project, we need the standard I/O file! So, we will use the C macro #include in order to "include", or use, the standard I/O file(being 'stdio.h').
Every project in C NEEDS, and REQUIRES a main function.
Although the main function is usually implemented into the file by default, we are still going to cover this!
The main function is just like any other function in C!

Functions in C are setup as so:
returnType funcName(funcArgs) {funcCode}
And, in this case, for our main function, we need it to have a return type of int, with the function name 'main', no arguments, and the curly braces!

line 4

Line 4:

printf("Hello World");

This is going to be brief!

Line 4 is using the printing method that the standard I/O library offers to us.
The function 'printf' is a function that is included in the 'stdio.h' file. This function prints to the screen. We will discuss some more advanced ways of using the function later on, but for now, just know the function to print to the screen is "printf".

Now, while in other languages you can use the single quotes to print strings, in C you can't due to the fact it is a strictly typed language!
Single quotes in C basically tells the compiler to look for a character, and you will get an error if you put more than one character within the single quotes. Double quotes in C signifies that you are working with a string! Now, strings in C are a bit of a hassle to work with, but we will get to that later. Just know, string variables in C are not like string variables in other languages!

So, now that we know that single quotes signifies a character and double quotes signifies a string, I am sure we all can conclude as to why we put Hello World inside double quotes in the "printf" function!

NOTE: Semicolons are required at the end of variable declaration,print statements, return statements etc BUT NOT AT THE END OF A FUNCTION

Line 5

Line 5:

return 0;

Ok, now, this is where you learn as to why we declare the main function as type int!

Functions that are type int return integers. In C, returning zero in the main function means the code compiled successfully and to exit the project with the exit code of zero, signifying that it completed successfully.

Now, while you can give it other numbers such as '-1', the basic practice is to return 0 in the main function to signify that the main function compiled/ran successfully!

Now, this is just for the main function! You do not need to return 0 in every function declared as type int!
Some function you will want to return an integer of the users age times 10.
The main function is usually the only function you will want to return zero!

line 6

Line 6 is the closing curly brace of the main function, if you don't have it or accidentally deleted it, your function will fail to run and you will get an error!

Overview

In this tutorial, you should have learned the following:

  • The standard I/O of the language, and how to "import" it, or use it, in the file.
  • How to setup your main function(as well as what the type declaration 'int' signifies for your function)
  • How to use the standard output method(printf)
  • Why we return 0 in the main function

Remember:
C #include in C is a "macro". We will talk more about C macros later on!
The header file you want to use in the file is going to be put inside less than and greater than signs('<', '>')
Every C project needs a main function. The main function will have a return type of int, and you will put C return 0 at the end of the main function.
printf is a C function you have access to when you include the standard I/O header file('stdio.h')
A string in C needs to have double quotation, not single. Single quotation indicates a character.

Extra Information

Within the stdio.h header file, you will have access to stdin and stdout.
Those 2 will be useful when you get into writing to files, getting input..etc!

Quickly, before you go! You Can print characters with printf, but make sure you put single quotes and not double!

You are viewing a single comment. View All
CodeLongAndPros (1579)

@EpicGamer007 Nope, not in CMD.EXE or anywhere else