Hello, World!

dinsdag 27 oktober 2020

Your first program!

Hi welcome to my journey through C programming!

I know this might be cliché but everywhere you look as a beginning programmer you start with this little piece of code called “Hello World”
Its easy to start you of with programming, but it gives you very little information and it tells you nothing about what is happening.
So lets change that. Here we go.

When creating a program, any program, you start with this little line of code: #include <stdio.h>.
The stdio.h is a header file and you need it to tell your program various things to help it function.
There are various normalized header files that can be accessed this way and we will see some of those on this journey through C.
We use the #include to include header files.
There are 2 ways to include header files.

  1. First there is the one we are using here #include <stdio.h> This is used for system header files. It searches for a file named file in a standard list of system directories.
  2. The second variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used forfile.

The stdio.h header is a sort of library that contains variable types, macros and functions to make your programming life easier.
Its name stdio stands for standard input output and it contains the printf function.
The standard input by default is your keyboard and shall from now on be referred to as stdin.
The standard output by default is your display monitor and shall from now on be referred to as stdout.

The int main(void) is a function that is present in every C program and is the point where every C program starts.
Every function is formatted in a specific way:

The return type specifies the type of variable a function returns.
In this case the return type is int short for integer and it tells the function that it should return a number.

The function name can be almost anything you want it to be, but there are a few rules that have to be followed.

  1. The first character of a name in C is restricted to A-Z, a-z or ‘_’.
    After that, any subsequent character can be A-Z, a-z, 0–9 or ‘_’.
  2. You can’t use reserved words such as “for” and “while” as identifiers.
  3. Predefined functions are found in the header file and will give an error

so why cant a function name start with a number?
The reason is that the C compiler considers the whole token to be a number if the first character is a digit.

Also its good to note that although it is allowed to use a underscore to start a function name it is allmost never used because underscores are often used as the starting character for names of extra variables declared by the assembler over which the compiler has no control. This can lead to conflicts and problems.

The parameters are variables that are being passed on to the function.

The printf() function is very commonly used in C there are other functions that also can be used like puts() and write()
For now we will stay with the printf() function, the printf() function prints a string of characters to your stdout.
You can do a lot of things with the printf function and we will go in to further detail on a later post.
Here it just prints a string of hard coded characters.

The return (0); is used to return a value after the function has done its job or in case of the main function it returns a 0 when the program has run its course.

So now you know what everything means, do we know in depth what it means no there is much more to learn but it would be too much to deal with right away.
I already feel like I went to far into the deep end with somethings.
If you’d like to, you can make a couple of exercises on your own.
I’ve taken the liberty to put some exercises down below so have at it.

Guidelines for Exercises

Ok here are some guidelines and restrictions on how to make the exercises they are here for your benefit.

  1. Use proper indentations.
  2. Do not! I repeat do not copy paste the assignments.
    This will result in poor memorization of the code you are trying to write.
    Type it out so the information will be stored in your memory permanently.
  3. Don’t make the exercises in one go.
    Yes, I know its weird, but take your time in between exercises.
    Make a exercise and take a 15 – 20 min. break and then make another one.
    This will also help with memorization.

Exercises

Posted by

Blog

Leave a Reply

Your email address will not be published. Required fields are marked *