Software 'N' Hardware

 

 

Programming in C/C++
C/C++

This tutorial will teach you the basics of programming in C/C++

The very first thing I suggest you do before reading my tutorial is to click on the Software button on the left menu then click on Compilers. Download a C/C++ compiler listed there.

There are no prequisities for learning C. Its a very powerful language, most of the Linux/Unix operating systems are coded in C. However to start coding in C you do need to pay attention, do research and read books.

Lets get started hopefully you should have download and installed your compiler. I'm going to be using Dev C++. Below is a simple introductory tutorial on how to program, compile, save and run your first program.

lesson1 - HelloWorld.c

In this lesson i'll be teaching you how to write your first C program. Open up Dev C++, click on File, New then Project. Then you'll see another window open, type in project name HelloWorld and choose Console Application as the Project Type. On the bottom right hand side there be 2 options C Project or C++ Project choose the C Project. Once you done all of the above click OK. Next you'll see a text window open with some text, highlight it and delete the text. So now for the code.

line: Code:

1 #include

2 int main()

3 {

4 printf("Hello, World!\n");

5 system("Pause");

6 }

Type all of the above code into the editor without the line numbers for example instead of 1 #include you would simply type #incude . Do that for all of the line numbers and code. Simply click on the Execute menu which is located right near top of the window, you'll see a list of options click on Compile, it might ask you for a file name, type HelloWorld.c. Once you done that click on the Execute menu again and this time click on Run hopefully if you typed the code correctly and you followed my instructions properly you should see the following output: Hello World .

Explanation

line 1 -> The basic header file for standard input and output.

line 2 -> This is the main method of the C language.

line 3 -> Opening brace for the code.

line 4 -> A print function to print data to the standard out which is the monitor.

line 5 -> We use the system command only so that when you run this program from the Dev C++ we can view the output. Experiment with what happens if you remove this line of code.

line 6 -> Simply ends the code and the program.

html

lesson2 - Variables

In this lesson I going to teach you about variables in C. A variable is a peice of memory which is reserved by the program and which will be used, it simply holds data. Variables can hold different types of data, in C we have to specify which type we want the variable to hold for example:

  • int - can hold integers
  • char - can hold characters
  • float - can hold single precision floating point number
  • double - can hold double precision floating point number

Now we know what variables are and what types we can have, i'll demonstrate this by a simple program. It will display the value to standard out which is held in the variable. So open a new project in Dev C++ and new console application save it as variables. So here goes..

line: code

1 #include

2 int main()

3 {

4 int a = 10;

5 printf("Variable A holds the value: %d",a);

6 system("pause");

7 }

Explanation

If your not familiar with lines 1, 2 ,3,6 and 7 please read lesson1 again.

line 4 -> we're defining an integer called a which holds the number 10. We could have made this variable a hold any number we want it to apart from a decimal number, thats what the float and double types are used for.

line 5 -> a simple print statment however we use %d to print a integer value and after the comma we define the variable name used to get the integer value from, in our case a.

I am now going to show you how to use the char type. So again open a new project in Dev C++ and save it as charvariable.

line: code

1 #include

2 int main()

3 {

4 char a = 'a';

5 printf("Variable A holds the value: %c",a);

6 system("pause");

7 }

Explanation

line 4 -> is now defining a character type which holds the value a

line 5 -> is the same as the previous example except the d for integer is now change to c for char.

html

lesson3 - Conditional Statements

Conditional statements are used to make decisions in programming. For example, if the user types in the value 10, the computer will double it and if the user types in 5 the computer will tripple the value. This is the example im going to show you now. So again you should be getting use to this open a new project and save it as IFstatements.

line: code

1 #include

2 int main()

3 {

4 int a, b;

5 scanf("%d", &a);

6 if (a==10)

7 {

8 b = a * 2 ;

9 printf("%d",b);

10 }

11 else if (a==5)

12 {

13 b = a * 2;

14 printf("%d",b);

15 }

16 else

17 {

18 exit(1);

19}

20 system("pause");

21 }

Explanation

Lines 1,2,3,7,10,12,19,20 and 21 you should be able understand by now if you dont I suggest reading the first lesson again.

line 4 -> We reseve two memory locations one called a and the other called b both to hold integer values.

line 5 -> We use the scanf method to receive data from the standard in which is the keyboard. The program will loop until the user enters a value or theres kill signal detected.

line 6 -> The if statement clause has three part if, else if and else. If some conditon is true execute, else if its not execute that code else execute the remaining code and jump out of the if statment. In this statement we are saying if the variable has the value 10 then run the code which is on line 7.

line 8 -> If the conditon is true then b holds the calculation of a x 2 which is 20.

line 9 -> simply prints the data out of the variable b.

line 11 -> the else if clause, if the if clause is false the program goes to the else if clause and executes that code, if it is true then it jumps out to the block of code following it.

line 13 -> If the else if condition is true then it simply times the value 5 by 3, so the application should print 15.

line 14 -> Simply prints the data from variable b to the standard out.

line 16 -> This is the final part of the if statment, else if all fails execute the code in this section.

line 18 -> We send the exit signal which closes the program

Ok play around with the code, try different variable types, see what happens because when programming you need to keep practising and the best way is by writing small programs.

html

lesson4 - Loops 

A loop is iterative function built into C, for example if you want to display the numbers 1 to 10 on a newline, with your current skillset you would have to type 10 different printf statements. However I am going to show you how you can write this program in 5 lines of code and do the same thing with it.

There are 3 different types of loops in C it varies from language to language. The first is a FOR loop, then a While loop and the last a DO While loop.

Below im going to demostrate the FOR loop. A FOR loop is used when you know how many times you want to execute a certain method, function or statement.

FOR.c

line: code:

1 #include <stdio.h>

2 int main()

3{

4 int i;

5 for (i =1; i<=10; i++)

6 {

7 printf("%d\n",i);

8 }

9 system("pause")

10 }

Explanation

line 4 -> Declare a variable of type integer called i.

line 5 -> The for loop declaration, takes 3 argument, start of loop, end of loop, and increment or decrement.

line 7 -> Prints the numbers on seperate line.

This program works by running the loop a set amount of time for example our program loops until i is equal to 10, once this is true it quits printing to the screen and exits the program.

WHILE loop

WHILE loops are used for infinite operations, when you dont know how many times you want it to loop. For example you may use a WHILE loop for user input, until the user doesnt enter a value the program doesn't continue, this can be used in a password application.

Now for the code. Copy and paste it into your compiler without the line numbers ofcourse.

line: code:

1 #include <stdio.h>

2 int main()

3{

4 int i, a=5, b=6;

5 WHILE(a<=b)

6 {

7 printf("%d\n",i);

8 }

9 system("pause")

10 }

Caution this program will run infinitley so make you have taskmgr open to kill the program once you've seen the results.

Explanation

line 5 -> Declare the while loop method takes one argument the testing condition, if its true execute the loop if not jumps to next part of the program in our case exit.

line 7-> Print huge list of number you see infinitly.

DO-WHILE loop

A DO-WHILE loop is the pretty much the same as a WHILE loop however instead of testing the condition at the top it tests it at the bottom of the code. The following program will do the same as a WHILE loop but using a DO-WHILE loop syntax.

line: code:

1 #include <stdio.h>

2 int main()

3{

4 int i, a=5, b=6;

5 do

6 {

7 i++;

8 printf("%d\n",i);

9 }

10 while(a<=b);

11 system("pause")

12 }

Explanation

line 5 -> The keyword do starts the do while loop.

line 8 -> Prints the values to standard out, the monitor.

line 10 -> The condition is tested if true then the do part is executed.

html

lesson5 - Arrays

In this lesson im going to teach you about arrays, what they are and how we can implement them in our own programs.

Arrays can be usefull when storing information. If you wanted to declare 10 variables with your current skillset you would think you have to declare 10 integers seperately. No thats wrong you can just use an array of size 10 which holds integers. Its much more efficient, faster and its less code every programmers dream. Declaring variables are simple for example if you wanted a variable of size 10 with the name a:

  • int a[10];
  • char a[10];
  • float a[10];
  • double a[10];

line: code:

1 #include <stdio.h>

2 int main()

3 {

4 int a[10], i;

5 for(i=1; i<=10; i++)

6 {

7 a[i]=i;

8 }

9 for(i=1; i<=10; i++)

10 {

11 printf("%d",a[i]);

12 }

13 System("pause");

14 }

Explanation

This program uses two for loops, one for entering data into the array and another one to display data which is held in the array.

line 4 -> Declaring the array of size 10 and type int.

line 5 -> For loop to store the value of i into the array.

line 9 -> For loop to read out the values you stored in the array with the previous for loop.

html

TOP

Softhardware.co.uk
Know it All..



Menu

 
 

  

     

 

      Registry Cleaner  

 

    Computers  

    SiteBuilder Elite

  computers