Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C The C Tutorial Series - Tutorial 1: Printing, Variables And Data-Tyes, And Comments

Mathematical

Silver Coder
[UWSL][UWSL]Introduction[/UWSL][/UWSL]
Welcome to the first tutorial in the C Tutorial Series, here on CF. In today's tutorial, we will be covering the following things:
  • Printing out text
  • Variables and their different data-types(char, int, float, and double)
  • Commenting your code
These are the prime basics that you will be learning - Alongside getting user-input - That will be required for you to go forward in learning C. Anyways, let's begin.

[UWSL][UWSL]Printing Text[/UWSL][/UWSL]
Of course, we're starting off with printing. If you have prior programming-experience, there is no doubt that the first thing you did was print the words: "Hello world!" - Fun fact: The Hello World program was actually written by the co-author of The C Programming-Language book, Brian W. Kernighan.

To write this famous program in C, you simply write:
C:
#include <stdio.h>

int main(void) {
     printf("Hello world!\n");

     return 0;
}
Now that we've written that, save it as Hello.c. But now, how do we run it? Well, first we need to compile it. Instructions:
  • If you're on a GNU/Linux system, you open-up a terminal and write: gcc Hello.c -o Hello. This will output a file called: Hello. Run it using: ./Hello and you'll get your output.
  • If you're on MS Windows, you'll need something called MinGW. Head over to: http://mingw.org/ - And download it. There are tutorials online on how to download MinGW and then set it up. Unfortunately, I will not be covering this here.
  • If you're on MacOS, what version you're on will depend on how this procedure will go. I recommend checking out this SO thread to see how: https://stackoverflow.com/questions/32337643/how-to-run-c-program-on-mac-os-x-using-terminal

Hopefully, your program has compiled successfully. If it has, you should've seen: "Hello world" - Printed out onto your terminal. If not and you got an error, go back and check to see where you went wrong.

Note: One of the most common errors in programming is missing-symbols - This includes the semi-colon, which C requires at the end of each statement(Line). Make sure you don't miss any symbols as just a single missing symbol can prevent an entire program from compiling.

Let's analyze what this program does:
  • Line 1: #include <stdio.h> - This includes the Input/Output header from C's standard library. A lot of important functions are in here, including printf(), so make sure you include it in your programs, as you more than likely will need something from it.
  • Line 3: int main(void) - This is your main function. It's the heart of your program and is always the first function to be called upon start-up. Your program cannot start without this function, so remember to include it in every program.
  • Line 4: printf("Hello world!\n"); - This statement uses the printf() function from stdio.h. In this program, it is simply saying, "Hello world!" to the user. \n indicates to the computer that a newline must be made.
  • Line 6: return 0; - This is a return-statement. You'll use it at the end of functions to return a value back to the calling function. The 0, means false - So with the return-statement, it's simply saying that the program is no longer running at this point. C does not have booleans implemented by default, unless you include: <stdbool.h>.

Now that we have analyzed our code, you should hopefully now understand what is going on. If not, refer to The C Programming-Language, 2nd Edition, pages 6 and 7.

[UWSL][UWSL]Commenting Your Code[/UWSL][/UWSL]
Before we move on, it is important that I talk to you about comments. Comments are essential and should not be forgotten about when writing your code. Comments are you used to document what goes on in your code, so that when you return to it after a period of time or when somebody else such as a co-worker analyzes your code, that you and your fellow workers will be able to understand what's actually happening. By not including your comments, you are setting yourself up for a confusing ride of trying to understand what a complicated piece of code does.

C has two ways of writing comments:
  • // - A single-line comment. As you can guess, it can only be used for one line.
  • /**/ - A multi-line comment. This can go on for as many lines as you need it to.

It is advised that you do not comment obvious pieces of code. For example, with a for-loop that increments a variable called A by one, ten times - That does not need a comment. That is self-explanatory code. If the code involves more advanced and complicated features or functions though, such as pointers or a function from one of C's expansive built-in libraries, comments will be a requirement. Here's an example showcasing a poor use of a comment:
C:
char Name[50] = ""; // This variable will store the name of the user.
The example above is quite self-explanatory. It stores a string(In this case, the name of the user - We will be covering strings and other data-types after we're done with comments). We do not need to comment it as again, it's self-explanatory and we know what it's going to be holding.

There are many good examples of using comments. One of them, is putting in a copyright-notice at the beginning of your file. This is quite a common practice, so if you're going to be releasing programs, make sure you include a copyright-notice at the beginning of each file using comments. Another good practice, is using comments for explaining what a function does. This will give the person who's reading-over the code an idea of what the function does. Again, there are many good examples of using comments, but what I've listed above are just some good examples of how to use comments.

[UWSL]Variables And Data-Types[/UWSL]
A variable is an address with a name("Identifier") that stores a piece of data. This can be any kind of data, including:
  • An integer value.
  • A single-character or a string of characters.
  • A floating-point or double value.
  • A boolean value - True or false.

These are all of C's basic data-types - There are a couple of other data-types, such as static, which unfortunately I have no intention of covering mainly due to how complex certain data-types can be(Although, this may change in the future). Each one of them has a different purpose and are outputted differently too. When "declaring" a variable, the data-type comes first, then your identifier is written after. Optionally, you can choose to "initialize" a variable with a value. This can be done on the same line you declare the variable or at a later line in your program.

Here are some examples of what each variable holds and how they are declared and initialized:
C:
/* Note: Integers, floating-points, and doubles, can also hold negative-values.
They cannot hold negative-values if they are declared with the [unsigned] data-type. */
int Number = 55;
long int Long_Num = 538295; // A long integer. Uses 32-bits unlike [int] and can hold much larger numbers. Use [long long int] for extremely large values.
/* A short integer. Uses 16-bits, alongside [int], which may also use 32-bits. You'll typically use [short] for a special-case, such as working with a less
powerful device and wanting to keep memory-usage low. */
short int Short_Num = -25;
float Floating_Point = 2.4f;
double Double_Value = 1.5;
char Single_Char = 'C';
char String[20] = "Hello world!";
_Bool IsActive = true; // <stdbool.h> header-file required for this data-type.

Now that we know what a variable is; what the basic data-types are in C; and how we declare and initialize variables, how do we print out the values? Well, to do that, we need to use the printf() function that we used earlier. Each data-type uses a different "formatting-specifier". Here they are:
  • %d or %i for signed integer-values. %u for unsigned integer-values.
  • %f for floating-point numbers
  • %lf for double-values.
  • %c[/CODE] for single-characters; [ICODE]%s for a string of characters.

There are other formatting-specifiers available for use, but the ones listed are what we're going to be sticking with for now, as we only know the basic data-types.

Let's print out a number. Write the following into a C file and then compile and run it(Instructions for compiling can be found earlier in this publication):
C:
#include <stdio.h>

int main(void) {
     int Lucky_Num = 51;
    
     printf("The lucky-number is: %d\n", Lucky_Num);

     return 0;
}

In the code above, we declare and initialize an integer variable called Lucky_Num, with the value of 51 assigned to it. Then, we print out a piece of text, saying what that lucky-number is. We use %d to format the integer-value so that it can be rendered. But, we supply an "argument" to printf(), that argument being our integer variable that we declared and initialized earlier. If we did not supply that number to printf() or instead never assigned a value to the variable, we would have gotten either a garbage-value because we never supplied an argument(This "garbage-value" can be any random number - It should be noted there is more to this, so I will leave this subject for you to study on), or 0(If we had declared a variable and supplied it as an argument, but never assigned a value). Alongside not supplying an argument, we would have gotten a warning too. Feel free to play around with this and see what results it yields. Perhaps try changing the data-type of the variable to see what else you get, based on the data-type.

[UWSL]Wrapping-Up[/UWSL]
Well, that's it for today. Let's recap on what we learned:
  • We learned how to write and compile our first C program.
  • We learned how to print text to the terminal.
  • We learned about comments and how important they are when documenting code.
  • We learned the basic-data types available in C, what kind of data they store, and how to declare and initialize variables.
  • We learned how to output data from a variable into the terminal, using the format-specifiers available in printf().

Now that we're done, I advise that you play around with what you learned. It is always a good thing to practice your newly-found knowledge. You may also be interested in further studying upon anything that you learned, as this will increase your knowledge on the subject.

In case you haven't yet, I recommend picking-up The C Programming-Language, 2nd Edition, by Brian W. Kernighan and Dennis Ritchie. This is the essential reference to C and you cannot go without it. It offers much more in-depth explanations to many subjects and will teach you certain things that I may not even cover at all or just barely scratch the surface of. You can continue without it on the second tutorial that I will be publishing soon, but from Tutorial 3 and beyond, I expect you to have this book handy as things will start to get more complicated and theoretical.

If you have any feedback or criticism, don't be afraid to give them to me. I take all of my feedback into consideration and by giving feedback, you can help improve any future tutorials that I publish online, for anyone to read. If you notice any issues or mistakes in the tutorials, point them out to me too, and I'll try my best to fix them.
 
Back
Top Bottom