Monday, November 19, 2012

Programming Session 3


Hello everyone :) we finished last session on what is the meaning of variables and we knew that there are different kinds of variables according to their size.

Today we are going in deep with variables let's check our agenda :)

  1. Variables cont.
  2. Data Types
  3. create variables  

okay we knew that we need to select suitable place for storing our data in memory but how can we store them and how to know whether is it a suitable space or not?

to store data in  a variable we need to create a variable and before creating it we need to know what is the type of the data that will be stored in this variable in order to know how much space would be reserved for this variable.

so we need to know what is the available data types to select the most suitable data type for our data.

Data Types

these are the data types with their sizes in bytes.

After Creating a Variable we need to know what is the rules of Creating a Variable:
  1. We need to know which type
  2. Give it a name and
    1.  it should't be a reserved word such as "int , short , printf .. etc.
    2. its name shouldn't exceed 255 characters 
    3. it should start with a letter and _ is considered a letter
    4. it shouldn't contain any special character like "" , * ,\ ,/ ...etc.

well let's try to create a program that calculates the summation of two numbers.

first we need to create 2 variables for storing our numbers:
which type : int "integer"

int N1,N2;   //we have reserved two integers in memory.

now what does these variables contain ?
  1. rubbish   
  2. empty space
it is rubbish as you told your program that you want to reserve space but you didn't say put something inside this space or even clean it.

so we need to put values into these variables
int N1= 4 , N2= 8;

okay now we are ready to make the sum operation 

#include<stdio.h>
int main()
{


     int N1= 4 , N2= 8;

     printf("the result is = %d",N1+N2);     
     return 0;
}

what  is printf("the result is = %d",N1+N2); ?
as we can see what is enclosed between double quotes will be displayed as it is except what follows a percentage sign % as this tell the program there will be something here check its value outside the quotes which is the sum of N1 and N2.

okay but how can i print this statement "the result of 4 + 8 is 12"
it could be written in two formats either :

printf("the result of 4 + 8 is %d",N1+N2);
or 
printf("the result of %d + %d is %d",N1,N2,N1+N2);

did you get it ?
yes each time we need to print a number on the screen we put %d and we put its place after the quotations and for sure they are printed with the same sequence which mean N1 then N2 then N1+N2.

Practice 1:
now modify the previous program to add three numbers and print their sum and Multiplication.




No comments:

Post a Comment