C/C++ Basics


  1. Built-in data types
    1. Integers (size : 2 bytes)
      eg : int age = 5 ;
    2. Float numbers (size : 4 bytes)
      eg : float weight = 84.5 ;
    3. Characters (size : 1 byte)
      eg : char grade = 'b' ;
    4. Double float numbers (size : 8 bytes)
      eg : double distance = 33.333 ;

  2. Data constructions
    1. Arrays
      1. int list[3] ;
        char table[4][3] ;
        list[2] = 333
        table[1][3] = 'c' ;
        • List is a one dimension array ( 3 items )of integers.
        • table is a two dimensions array ( 4 rows and 3 columns )of characters.
        • the value of the third element ( notice that it starts from 0 ) is '333'.
        • the value of the fourth row in the third column is 'c'.
    2. Pointers
      1. int y , * x ;
        y = 3 ;
        x = & y ;
        * x ++
        • We first initialized two variables ( y is an integer and x is a pointer to an integer )
        • we assigned ( y = 3 )
        • We let x points to y ( put the address of y in x )
        • we increment the variable which the x is pointing to ( y = 4 now )
    3. Dynamic allocaton
      1. These variables are created when we need'em and then they must be deleted.
        notice that these variables are generated during the excution time not in the compiling time as the other variables.
      2. Creating a new variable :
        eg : int * xPtr = new int ;
        eg : char * NamePtr = new char[17] ;
      3. Deleting it :
        eg : delete xPtr;
        eg : delete [17] NamePtr ;
    4. Structures
      1. Defining a structure :
        sruct employee {
        int id;
        float salary;
        char phone[30];
        };
        • Here we've defined a new variable type called employee
      2. Declaring a structure :
        struct employee Hussein,Ali;
        • Here we've declared two variables called 'Hussein' and 'Ali' respectively of type employee
      3. Accessing an element of the structure :
        Hussein.id = 5 ;
        cout << " Ali's salary = " << Ali.salary ;
        • Here we've put the value '5' in the 'id' of the 'Hussein' , and then we displayed the 'salary' of 'Ali'
      4. Pointers to structures
        • Assume we've the structure written in the previous example.
        • Declaring a pointer to it :
          employee * pt = & Ali;
        • Accessing one of it's elements:
          pt -> salary = 22.3 ;
          or :
          (*pt).salary = 22.3 ;

  3. Operators
    1. Arithmatic
      ( + ) Addition.
      assume we have x = 5 .
      1. x = x + 3 ;
        x now equals 8
      2. cout << + + x ;
        The computer will display the number 6
      3. cout << x + + ;
        The computer will dispaly the number 5

      ( - ) Subtraction.
      assume we have x = 5 .
      1. x = x - 3 ;
        x now equals 2
      2. cout << - - x ;
        The computer will display the number 4
      3. cout << x - - ;
        The computer will dispaly the number 5

      ( * ) Multiplication.
      ( / ) Division.
      ( % ) Division's remainder.
    2. Logic
      ( > ) Greater than.
      ( < ) Less than.
      ( > = ) Greater than or equal.
      ( < = ) Less than or equal.
      ( = = ) Equal. (Two adjacent equal's for logic operations.)
      ( ! = ) Not Equal.
      ( & & ) And. (Two adjacent and's for logic operations.)
      ( | | ) Or. (Two adjacent or's for logic operations.)
      ( ! ) Not.

  4. Statements and loops
    1. If statemant
      1. if ( condition ) {
        ...... ;
        ...... ;
        }
      2. if ( condition ) {
        ...... ;
        ...... ;
        }
        else {
        ...... ;
        ...... ;
    2. While statement
      1. while ( condition ) {
        ...... ;
        ...... ;
        }
      2. do {
        ...... ;
        ...... ;
        }while( condition )
    3. For loop
      1. for ( i=5 ; i>=2 ; i-- ) {
        ...... ;
        ...... ;
        }
    4. Notes
      1. Break statement
        It causes the programme to exit from the loop.
      2. Continue statement
        It causes ther programme to skip other loop statements and go to the start of the loop .
      3. Return ( x )
        It causes the function to terminate and returns the value of x .

  5. Functions
    1. Simple Function :
      void fun1(void);
      void main(void){
      cout<<"\nMain programme";
      fun1( );
      getch( );
      }
      void fun1(void){
      cout<<"\nFunction";
      }
    2. Passing and returning values :
      int fun2(int x);
      void main(void){
      int y;
      y=fun2(33);
      cout<<y;
      getch( );
      }
      int fun2(int x){
      if(!(x%11)) {
      cout<<"\nDivisible by eleven";
      return (x/11);
      }
      else {
      cout<<"\nNot divisible by eleven";
      return 0;
      }
      }
    3. Passing variables by references :
      void fun3(int &x , int &y);
      void main(void){
      fun3(3,4);
      cout<<x<<y;
      getch( );
      }
      void fun3(int &x , int &y){
      x++;
      y++;
      }


C/C++ Message Board

Now if you've comments or questions about anything in C/C++ , just leave a message here, and your it will be discussed in details incha`Allah.


( Prev ) ( Table of contents ) ( Next )

Copyright (C) 2000 Tarek Amr Abdullah. All rights reserved. My Home Page