Wednesday 24 September 2014

C Language Tutorial Series Part 1

Hi, long time no see. So, here we are on the C language tutorial  part 1. This is a tutorial, a crash course in C and nothing else. I am not gonna detail on how to get C compiler and how to run the compiled code. This tutorial would be based on examples and not much explanation would be given.

Here are a few things to note, before we get started:

NOTE: These things should not be forgot and paid attention to.

TIPS: These things are little tricks that you could use (optional stuff).



YOUR FIRST PROGRAM: (Why is it the Hello World Program???)
Well, let's get started. The first program is:
#include <stdio.h>
int main() {
    printf("Hello World!\n");
    return 0;
}
 Here's the explanation:

1. The statement #include <stdio.h> is used to import the contents of the file stdio.h. This file contains most commonly used functions and variables that we are going to use.

2. The statement int main() { } defines a function named main with the return type int (Integer, a number with no decimal point like 23, -97, etc...). It takes no parameters as indicated by empty round brackets().

NOTE: Return type means that if the system ran this program, then the program upon completion would return an integer.

NOTE: All functions must have 4 components:
1. A return type (if the function does not return anything, then return type is void)
2. A name (The name must not begin with a number or any special character; it can begin with an underscore and the name must not be something that C uses)
3. Parameters (The round brackets can contain parameter that the function requires to run, if the function does not require parameters, then simply keep the brackets empty or write void)
4. The body of the function must be enclosed in curly brackets{} (The brackets should be one before the function block and one after the function block)

TIPS: main can have arguments, then main becomes int main(int argc, char* argv[]). By giving main parameters, it means that the program requires some extra input to run. In that case, argv[0] points to name of the program and the options start from argv[1]. This means that argc becomes 2 if there is only one option, 3 if there are two options and so on.

3. The statement printf("Hello World\n") prints the string "Hello World!" on screen. "\n" stands for new line. It means that the if your do another printf, then the output goes to the next line (Think pressing Enter/Return Key on your keyboard).

4. The statement return 0 is the end of the main function. As defined, main should return an int, so we choose zero (basically, it could be anything you want, but it's standard practice to put zero).

The reason for doing this program as the very first program is that it clearly shows the syntax of the language.

Here's a sample run of the program:



CALCULATOR v1.0:
Well, then now you can print stuff on screen. How about we do a little math. Here is the code:
#include <stdio.h>
int main() {
    float c = 0.0;
    float a, b;
    printf("Enter a number: ");
    scanf("%f", &a);
    printf("Enter another number: ");
    scanf("%f", &b);
    printf("Addition: %0.2f\n", (a+b));
    printf("Subtraction: %0.2f\n", (a-b));
    printf("Multiplication: %0.2f\n", (a*b));
    printf("Division: %0.2f\n", (a/b));
    printf("Remainder(Integer Division): %d\n", ((int)a%(int)b));
    printf("Multiply first number by 23: %0.2f\n", (a*23));
    printf("Multiply number 12 by 13: %d\n", (12*13));
    return 0;
}
 Here's the explanation:

 Everything up to main is the same.

1. The line float c = 0.0 and float a, b declare variables a, b and c. Think of variables as boxes in which you put stuff in. float(Numbers with decimal points like 2.3, -9.87, etc..) here refers to the data-type of the stuff we want to put. As you can see, we can give value to variable at the time of declaration if we want.


NOTE: The standard data types are:
1. char
2. short
3. int
4. long
5. float
6. double


The printf just containing strings are the same as before.

2. The line scanf("%f", &a) is used to get a value from the user (basically the keyboard) and then put that value in a.

3. The printf from line 9-14 are different from what you used earlier. The %0.2f used prints the contents of the calculations done after the comma in the string and writes that to the screen as a float with only 2 digits after the decimal point. The %d is used to print integer values (no decimal points). %c is used to print characters.

NOTE: If you just use %f in printf, then you might get a long list of trailing zeroes.


4. The part ((int)a%(int)b) in line 13, first converts variable a (float) into an integer. The same goes for variable b and then perform integer division (just gives the remainder after dividing).

Here's a sample run of the program:




NOTE: Be careful with the input, you might get a floating point exception (more on that in part 2).

Now, continue to part 2. As always thanks for reading and please comment (if you get any problems or not).

No comments:

Post a Comment

Stuff that most try to do