Subscribe:

Ads 468x60px

Labels

Thursday, March 22, 2012

C Programming Tutorial Part 2 Variables and Operators

In this tutorial I will explain what are the variables and operators. " Variables " are changing parameters as the word says which we use to store data ( numbers, letters etc ). There are different types of variables. The most used are:  
              integers which can store numbers  in the range [ 2^-15 - 2^16-1]
              strings which can store letters only, to use strings we need to include the string.h library
              char which can store only one character or numbers from [ -128 - 127 ]

For each variable we can use the switch long before them , for example longint= " long integer " which allows us to store a larger number, or the switch singed/unsiged. For example unsignedint means that we always take only the positive value. Finally the simplest operators are " + ", " - " which allow us to make calculations and later on we will learn other operators as +=, ++ etc.
Below is a programm that I wrote and I have explained every command in the comments after the " // ".


1:  #include<stdio.h>   
2:  #include <windows.h>  
3:   int main ()  
4:  {  
5:     int a,b,s; //we declare 3 integer variables  
6:       printf("Enter Nr1:"); // we ask for the first number  
7:       scanf("%d",&a); // scanf command stores the value of the number we enter on the integer a  
8:       printf("EnterNr2:");   
9:       scanf("%d",&b); //store the value of number 2 on the integer b  
10:       s=a+b; // calculate the sum and store it on the variable b  
11:       printf("Sum=%d",s); // we print the sum, the %d switch tells the compiler to print a whole number  
12:       system("pause"); // we just freeze the program so it doesn't end automaticlly, we need to press any key for it to end  
13:       return 0;  
14:  }  

Download the .c file of the program above : Download

Previous Tutorial <<                    >> Next Tutorial will be posted on 30 March

C Programming Tutorial Introduction

Before I start my tutorial I will make an introduction to C programming language. First I want to give you some history about C.
The development of C occured at Bell Labs between 1969 and 1973. The first language was called B by Thompson, then at 1971 Ritchie modified the language and gave it a new name : Extended B or ( NB ) which means new B. Finally on 1973 it was given the name C and later on 1989 it was created the first standard ( c89 ) and on 1999 the second standard ( c99 ). These two standards aren't  very different from each other, but anyway we will learn c99 because it is being used more nowadays.

The steps we follow to write a program :
1. Write it in a text editor and it is saved in a .c file and is known as " source code "
2. Compile the source code and we get an object code .obj
3. Linking step where we make it a .exe file ( executable file ) and finally we execute the program!

Please note that the first and second steps are made automatically by the compiler and you won't see them. A compiler is a software which works as text editor, compiler and linker together. There are many free compilers avaible and we suggest that you use one of these : dev c++ or  codeblocks. On my next tutorials I will use dev c++ and you can download it for free here Download

>>Next Tutorial