Results 1 to 4 of 4

Thread: Intermediate C tutorial

  1. #1

    Intermediate C tutorial

    Intermediate C programming tutorial



    This tutorial is intended to be easy to follow for non-programmers and you will be able to write your own simple
    programs after you finished it.



    Since you will probably have read the Basic C tutorial on this site, i will continue from there.



    The if-statement:

    In the previous tutorial i have told you about the if expressions, of course there are a lot more then just that one.
    Let's start with an example to show you what you could also have done with the if-statement:

    Code:
    #include "stdio.h"		//needed for the basic I/O functions (as discussed previously)
    
    int main(void)			//main function
    {
    	int number;
    
    	printf("Enter a number: ");
    	scanf("%d",&number);	//scan for the number;
    
    	if(number > 100)    	//if the number is bigger then 100
    	{
    		printf("\nYou have entered a number larger then 100.");
    	}
    	else			//if the above statement is not true, then perform the following action.
    	{
    		printf("\nThe number you have entered is not bigger then 100.");
    	}
    	return 0;
    }
    Now compile the program and see what it does. As you can see it checks your input, and if it is bigger then 100, it prints
    the first line to the screen.
    If the first condition is NOT true (it is not bigger then 100) it will print out the second line, it's as easy as that ;-)

    We could even add another check to the code, like this:

    Code:
    #include "stdio.h"		//needed for the basic I/O functions (as discussed previously)
    
    int main(void)			//main function
    {
    	int number;
    
    	printf("Enter a number: ");
    	scanf("%d",&number);	//scan for the number;
    
    	if(number > 100)    	//if the number is bigger then 100
    	{
    		printf("\nYou have entered a number larger then 100.");
    	}
    	else if(number < 50)	//if the above statement is not true, then check this statement.
    		printf("\nThe number you have entered is smaller then 50.");
    	}
    	else			//if both statements above are NOT true, then perform the following action.
    	{
    		printf("\nThe number you have entered is bigger then 50 but smaller then 100.");
    	}
    	return 0;
    }
    As you can see we simply added another statement to the code. These types of statements are not static, meaning you can use
    the if-statement together with an else-statement and leave it that way.
    You could use the if-statement together with an else if-statement and NOT use the else-statement, this is also possible.

    Also you can use as many if-statements, else-statements and else if-statements in your code as you like, just remember that
    in order to use an else-statement or an else if-statement it has to be directly after an if-statement, you can not use them
    on their own.


    Nested if-statements:

    A nested if-statement is an if-statement in an if-statement, i will show you how this works with a little example code:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int number;
    
    	printf("\nEnter a number: ");
    	scanf("%d",&number);
    
    	if(number>50)
    	{
    		if(number>75)
    		{
    			if(number>100)
    			{
    				printf("\nYour number is bigger then 100");
    			}
    			else
    			{
    				printf("\nYour number is between 75 and 100");
    			}
    		}
    		else if(number>65)
    		{
    			printf("\nYour number is between 65 and 75.");
    		}
    		else
    		{
    			printf("\nYour number is between 50 and 65.");
    		}
    	}
    	else
    	{
    		printf("\nYour number is smaller then 50.");
    	}
    	
    	return 0;
    }

    Now don't let this code scare you, we have done nothing more then you have already learned before, all we did was put some
    statements in another. This will allow you to be more specific in the things you want your program to do in
    specific circumstances.



    The Comparison Operators:

    Well, we have used a couple of them already, but there are more operators we can use to let the program decide what to do.
    here i have made an overview of what exactly each operator does:


    > bigger : A > B --> if A is bigger then B
    < smaller : A < B --> if A is smaller then B
    <= smaller or equal : A <= B --> if A is smaller or equal to B
    >= bigger or equal : A >= B --> if A is bigger or equal to B
    == equal : A == B --> if A is equal to B
    != not equal : A != B --> if A is not equal to B


    As you can see now, with these operators you have a lot more control over the way the program reacts to specific things,
    this allows you to be very specific with your output.
    here's an example:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int number;
    
    	printf("\nEnter a number: ");
    	scanf("%d",&number);
    	
    	if(number==1)
    	{
    		printf("\nYou have entered a 1.");
    	}
    	else if(number==2)
    	{
    		printf("\nYou have entered a 2.");
    	}
    	else if(number==3)
    	{
    		printf("\nYou have entered a 3.");
    	}
    	else if(number==4)
    	{
    		printf("\nYou have entered a 4.");
    	}
    	else if(number==5)
    	{
    		printf("\nYou have entered a 5.");
    	}
      	else
    	{
    		printf("\nYou have entered a number bigger then 5 or smaller then 1");
    	}
    	return 0;
    }
    As you can see this program checks to see if your entered number corresponds to one of the numbers from 1-5, and let you
    know with which one it corresponds.
    Nice huh?!
    But of course there is another (more elegant) way of doing this as we will see now.



    The Switch statement:

    The switch statement allows you to do about the same as we have done in the previous program, but it only works a lot more
    efficient as i will show you:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int number;
    
    	printf("\nEnter a number: ");
    	scanf("%d",&number);
    
    	switch(number)		//here we start the switch statement
    	{
    		case 1: 	//check if number equals 1.
    			printf("\nYou have entered a 1.");
    			break;		//this is used to break the switch since we already know number equals 1.
    		case 2:
    			printf("\nYou have entered a 2.");
    			break;		//same here
    		case 9:
    			printf("\nYou have entered a 9.");
    			break;
    		case 3:
    			printf("\nYou have entered a 3.");
    			break;
    		default:		//if non of the equasions above are true
    			printf("\nYou did not enter a 1,2,3 or 9.");
    			break;
    	}
    	return 0;
    }
    Here we see the switch-function which allows you to check a variable for multiple possibilities. The cases are the options
    you have to check them.
    A switch can contain as many cases as you want, but it ALWAYS have to contain the default:!

    Now why is it more efficient then using if-statements all the time?
    Simple, with the if-statements the variable gets checked on every statement, meaning when the first statement is true (it
    is a 1), the other ones are still checked to see if it might be one of those, but since that is impossible, it would be a
    waste of time.
    With the switch statement we have used the break command to let the switch know we have already found what we were
    looking for, so there is no need to check the others.

    With this small code this doesn't really matter in terms of speed, but suppose you would have to write such a program that
    would have to do 10000 checks, it would check 10000 times using the if-method, but it would check only as many times as
    necessary (until it has found the correct one), in the switch-method!

    The switch-function can be used for just about anything, we have only used it now to check for an integer, but the cases
    could also contain characters or strings (among others).

    If we would not have used the break; command to exit the switch, all other cases would have been checked
    (and executed) as well, so you can see why we would need it



    Repeating a command a number of times:

    Suppose we would like to print a line a number of times on the screen, how would we do this??
    we could do it like this:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    	printf("\nHello World");
    
    	return 0;
    }
    Well, works perfect, doesn't it? But what if we would want to print it out 10000 times, or perhaps even a million times?
    That would be a lot of copy and paste work to do that.


    The For-loop:

    So there is another option, it is called a loop, there are several different ones, but the one most used it the for-loop.
    the for-loop does a specific thing, FOR a number of times, here's the code:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int i;
    
    	for(i=0;i<10;i++)			//here the for-loop starts
    	{
    		printf("\nHello World!");
    	}					//here the for loop ends.
    	return 0;
    }
    There we are, the same result but a lot easier to write
    Let me explain:

    for() //this is the for-function, it takes 3 arguments
    i=0 //the first argument, the integer variable i is set to 0 (zero).
    i<10 //as long as i is smaller then 10
    i++ //add 1 to i, i++ is the same as i+1, as well as i-- is the same as i-1. don't ask me why, but it just is
    //decided that this would add or subtract 1 to or from the variable.

    so basically we now have:

    for(i=0;i<10;i++) //i is set to 0, as long as i < 10, add 1 to i (i++) and run the code that is between the curly braces {}.

    it's as simple as that.
    you could also write it in the opposite way:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int i;
    	
    	for(i=10;i>0;i--)
    	{
    		printf("\nHello World");
    	}
    	return 0;
    }
    As you can see the program does exactly the same thing, only in the first version it is counting from 0 to 10, and in the
    second one it is counting from 10 to 0.


    The While-loop:

    The while-loop is another version of a loop, it does something WHILE something is true, here's an example code:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int i=0;
    
    	while(i<10)
    	{
    		printf("\nHello World");
    		i++;				//don't forget to add 1 to i, or you will end up in an eternal loop!
    	}
    	return 0;
    }
    When you run this you will see it will do exactly the same, or better set, its output is exactly the same.
    The difference is that you can use the while loop in other ways that just letting it run a number of times that is set in
    advance. You could use it to test for a character as well:

    Code:
    #include "stdio.h"
    
    int main(void)
    {
    	int i=0;			
    	char buffer[100];
    	char c;
    
    	printf("Enter a string: ");
    	
    	while(c!=':')				//here the while loop runs as long as c is not equal to ':'	
    	{
    		c=getch();			//we are reading a character one at a time from input into the variable c
    		putchar(c);			//here we are putting the input of c back onto the screen again.
    		buffer[i]=c;			//here we are putting the content of c into the array buffer
    		i++;				//here we add 1 to i, since we want the character that is read in c to be
    	}					//put in the array buffer, but we want to move it one location after each
    						//put, cause otherwise we would overwrite the first one every time
    	buffer[i]='\0';				//here we put the closing '\0' at the end of the string, since we do not
    						//want some garbage to be printed in the following line 
    	printf("\nYou have entered: \"%s\"",buffer);
    
    	return 0;
    }
    Don't worry about the buffer and the getch() and putchar() things for now, those will be explained in a following tutorial.
    But as you can see now we have created a loop which runs just as long as you did not press the character : .
    Nice huh?!
    You could also use a loop inside an if-statement and vica versa, this will create a more controlable program.


    These are the loops that are used most in C, but there are more of which we will cover in a following tutorial, just keep
    in mind that there is more out there ;-)


    Now, i think we have covered enough for this tutorial, just try to write a couple of programs yourself to learn it really
    well, since this is the absolute basic you need to write a program that can be useful to someone (or yourself).

    Also take a look at the programs section at my site, since it contains some of the
    programs i have written in C, and they all include the source code, study the source codes and try to understand why the
    program does what it does.


    Until next time, happy programming!


    Regards,


    White Scorpion



    ps, you can also read this tutorial, and others at my site and save it as html-form if you like..

  2. #2
    Not bad, I would say, you should explain loops a little better. You only give a pretty basic explanation on how they work. What about infinite loops?

    Not bad overall, it should help most people that know Jack about C.

  3. #3
    Not bad, I would say, you should explain loops a little better. You only give a pretty basic explanation on how they work. What about infinite loops?

    Not bad overall, it should help most people that know Jack about C
    hi whizkid2300,
    do you mean infinite loops that are created on purpose, or accidentally?

    i was planning to cover the subject on infinite loops that are created on purpose, arrays and pointers, API calls etc in the next tutorial.

  4. #4
    Senior Member
    Join Date
    Dec 2004
    Posts
    320
    Hey scorpian, I am just getting into C#, very helpful. Looking forward to another.
    The fool doth think he is wise, but the wiseman knows himself to be a fool - Good Ole Bill Shakespeare

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •