Topic : C Tutorial
Author : Ryan Parsons
Page : << Previous 2  Next >>
Go to page :


increased by one and then it is displayed to the screen by the printf statement. The second example shows how to add one to the value after everything else in the line of code is performed; therefore, the value of number is printed to the screen by the printf statment, then its value is increased by one. The same idea can be used with subtraction using --. A full example follows:

     #include

     int main(void)
     {
     int a = 1, b = 2, c = 3, d = 4;
    
     printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
     printf("a++ = %d\n", a++);
     printf("++b = %d\n", ++b);
     printf("c-- = %d\n", c--);
     printf("--d = %d\n", --d);
     printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
     return 0;
     }



Part two


Part two is going to start out with the different ways to use loops (while, for, do-while) in your programs. Then it will show some of the different ways of getting user input. There will be some useful math functions too, and after that there will be some information about the "if", "switch", and "goto" statements.Then it will end with a brief segment on the uses of pointers.




Lesson one


This lesson is going to show how to use the while and do-while loops.

     #include stdio.h

     int main(void)
     {
     int n = 0;

     while(n<5)
      {
      printf("n = %d\n", n);
      n++;
      }
     do
      {
      printf("n = %d\n", n);
      n--;
      }while(n > 0);
     return 0;
     }


Alright, after stdio.h is included and the main function is declared, an integer variable n is declared and assigned a value of zero. The next line is the while loop. A while loop will continue to perform the data inside the brackets as long as the condition inside the parentheses are still being met. The condition can be equal to('==' instead of '='; if '=' was used, the value would be assigned to the variable each time and the loop would continue forever), less than '<', or greater than '>'. If you want more than one condition to be met or one of several conditions, then you use '&&' for 'and' (a < 3 && b > 4)-a is less than three and b is greater than four; or you can use '||' for 'or' (a < 3 || b > 4)-a is less than three or b is greater than four. Once the condition is met inside the loop, the rest of the loop is still performed. Note: it usually is wise to make sure that your loop will end so the rest of the program is performed. Ok, so back to the example, it says that while the variable n is less than four, it will perform the printf statement then it will increment the value of n by one. Next in the program is a do while loop. It is almost the same thing as a while loop except that it always will go through the loop at least once. It has its ups and downs to it though.

Lesson two


This lesson teaches one of the more important parts of most programs, how to obtain input from the user. There are ways to obtain numbers, characters, and even strings(but we'll save that for later).

     #include stdio.h

     int main(void)
     {
     char x;
     int y;

     printf("Enter a character:\n");
     scanf("%c", &x);
     printf("Enter a number:\n");
     scanf("%d", &y);
     printf("The number you entered was %d\nThe letter was %c\n", y, x);
     return 0;
     }



Okay, the first few lines are nothing new until about the seventh line. This is the 'scanf' statement, which is versatile and can be used for all number types, characters and even strings. The first part of the statement is ' "%d" ', which is the format specifier for the type of data that is going to be inputed ('%d' for integers, '%c' for characters, '%f' for floats and doubles, and the rest you will learn later). The next part, '&x', is the name of the variable that the data is going to be placed in. Note: If you forget the '&' in front of the variable you will not get the result that you want (later there is a place that you do leave it out at, but not now. The next scanf statement uses the same format, but it gets an integer. The final printf statement at the end of the program proves that the data that the user entered was indeed stored to the variables.

Lesson three


This lesson is going to show several more examples of different ways that you can control your program. The if-else statement is one of the most useful ways to make your program work, while the switch case is a little more task specific and the goto statement is just what it sounds like; it sends the user to the specified place.

     #include stdio.h

     int main(void)
     {
     int i;

     beginning:
     printf("Enter a number\n");
     scanf("%d", &i);
     if(i == 1)
      {
      printf("i = 0\n");
      }else if(i < 0)
      {
      printf("i < 0\n");
      }else
      {
      printf("i > 1");
      }
     printf("Enter a number between 1 and 3\n");
     switch (i){
      case 1:prinf("1\n");break;
      case 2:prinf("2\n");break;
      case 3:prinf("3\n");break;
      }
     printf("Enter 1 if you want to go again\n");
     scanf("%d", &i);
     if(i == 1)
      goto beginning;
     return 0;
     }

    


The beginning of the program obtains a number from the user and stores it in the variable i. Then an "if" statement tests i to see if it is equal(you can also use less than <, and greater than >, or not equal to !=) to 1(in an if statement always use == for equal to, = just asigns the number to the variable). The program then performs the code that is in the brackets that follow the if statement. After that, the next statement is "else if(i < 0)". Basically thats just what it sounds like, if any of the previos statements are not true, and "i < 0" is, then the program performs the code in those brackets. The last statement is just a plain old "else", which means if nothing else was true, then the program runs whats in those brackets.
The "switch case" statement is usually a little more specific than if statements are; you can only test the variable for equality with the numbers after each "case". You put the variable that you want to test in the parenthesis after "switch", and then for every possible value of the variable that you want to put different lines of code for, you put "case n:" where n is that value. After the end of one set of code for one case, please put "break" (it doesnt work if you don't).
The next few lines are fairly simple, it asks the user if he wants to do the program again, if he types one, the program will execute "goto beginning;". This is easy: First you have to put the name of the label that your going to, in this case "beginning". All you have to do is put the name in the place that you want the program to go to followed by

Page : << Previous 2  Next >>