|< Basic Data Type 2 | Main | Basic Data Type 4 >| Site Index | Download |












My Training Period: xx hours


ANSI C refers to ISO/IEC C. This is a continuation from previous Module. The source code for this Module is: C/C++ source codes and the practice worksheets: C/C++ basic data types and the C/C++ standard input, scanf()/scanf_s().


The C and C++ programming skills that should be acquired:

















C and C++ Program Examples and Experiments


Example #1


// data types program example

#include <iostream>

using namespace std;


int main()    // main() function


    int        a = 3000;              // positive integer data type

    float      b = 4.5345;         // float data type

    char       c = 'A';                // char data type

    long       d = 31456;         // long positive integer data type

    long       e = -31456;        // long -ve integer data type

    int       f =  -145;                // -ve integer data type

    short      g = 120;             // short +ve integer data type

    short      h = -120;            // short -ve integer data type

    double     i = 5.1234567890;     // double float data type

    float      j = -3.24;            // float data type

cout<<"Welcome Ladies and Gentlemen!!\n";

cout<<"Here are the list of the C/C++ data type\n";

cout<<"\n1.  This is positive integer number (int):\t\t"<<a;

cout<<"\n2.  This is positive float number (float):\t\t"<<b;

cout<<"\n3.  This is negative float number(float):\t\t"<<j;

cout<<"\n4.  This is character data (char):\t\t\t"<<c;

cout<<"\n5.  This is long positive integer number(long):\t\t"<<d;

cout<<"\n6.  This is long negative integer number(long):\t\t"<<e;

cout<<"\n7.  This is negative integer number(int):\t\t"<<f;

cout<<"\n8.  This is short positive integer number(short):\t"<<g;

cout<<"\n9.  This is short negative integer number(short):\t"<<h;

cout<<"\n10.  This is double positive float number(double):\t"<<i;

cout<<"\n11.\'This is lateral string\'";

cout<<"\n\t---do you understand?----\n ";

return 0;





C/C++ data type console output


Example #2



// another data type program example

#include <iostream>

using namespace std;


void main()   // main() function


   int              p = 2000;               // positive integer data type

   short int        q = -120;            // variation

   unsigned short int r = 121;    // variation

   float       s = 21.566578;        // float data type

   char             t = 'r';                   // char data type

   long             u = 5678;            // long positive integer data type

   unsigned long    v = 5678;     // variation

   long             w = -5678;          // -ve long integer data type

   int              x =  -171;              // -ve integer data type

   short       y = -71;                    // short -ve integer data type

   unsigned short   z = 99;        // variation

   double           a = 88.12345;     // double float data type

   float       b = -3.245823;        // float data type

    cout<<"\t--Data type again--\n";


    cout<<"\n1.   \"int\" sample: \t\t"<<p;

    cout<<"\n2.   \"short\" int sample: \t"<<q;

    cout<<"\n3.   \"unsigned short int\" sample: "<<r;

    cout<<"\n4.   \"float\" sample: \t\t"<<s;

    cout<<"\n5.   \"char\" sample: \t\t"<<t;

    cout<<"\n6.   \"long\" sample: \t\t"<<u;

    cout<<"\n7.   \"unsigned long\" sample: \t"<<v;

    cout<<"\n8.   negative \"long\" sample: \t"<<w;

    cout<<"\n9.   negative \"int\" sample: \t"<<x;

    cout<<"\n10.  negative \"short\" sample: \t"<<y;

    cout<<"\n11.  unsigned \"short\" sample: \t"<<z;

    cout<<"\n12.  \"double\" sample: \t\t"<<a;

    cout<<"\n13.  negative \"float\" sample: \t"<<b<<endl;






C/C++ data type console program output




// program to calculate the circumference and area of circle

#include <iostream>

using namespace std;

// define identifier PI with constant, replace all the occurrence

// of PI with 3.14159 in the program...

#define  PI   3.14159   

// define identifier TWO with a constant

#define  TWO  2.0    


int main( )


    float   area, circumference, radius;

    cout<<"\nEnter the radius of the circle in meter: ";


    // circle area =  PI*radius*radius

    area = PI * radius * radius;

    // circumference = 2*PI*radius       

    circumference = TWO * PI * radius;

    // circle circumference

    cout<<"\nCircumference = "<<circumference<<" meter";

    // circle area

    cout<<"\nCircle area =  "<<area<<" square meter"<<endl;   

    return 0;





C/C++ data type program output


Example #4



// using cout from iostream header file
#include <iostream>

using namespace std;

int main()
  cout<<"Hello there.\n";
  cout<<"Here is 7: "<<7<<"\n";
  // other than escape sequence \n used for new line, endl...
  cout<<"\nThe manipulator endl writes a new line to the screen.\n"<<endl;
  cout<<"Here is a very big number:\t" << 10000 << endl;
  cout<<"Here is the sum of 10 and 5:\t" << (10+5) << endl;
  cout<<"Here's a fraction number:\t" << (float) 7/12 << endl;
  // simple type casting, from int to float
  cout<<"And a very very big number:\t" << (double) 7000 * 7000<< endl;
  // another type casting, from int to double
  cout<<"\nDon't forget to replace existing words with yours...\n";
  cout<<"I want to be a programmer!\n";
  return 0;




C C++ data type program output


Example #5


// comments in C/C++, using /*  */ or //

#include <iostream>

using namespace std;


int main()


  /* this is a comment

  and it extends until the closing

  star-slash comment mark */

  cout<<"Hello World! How are you?\n";

  // this comment ends at the end of the line

  // so, new comment line need new double forward slash

  cout<<"That is the comment in C/C++ program!\n";

  cout<<"They are ignored by compiler!\n";

  // double slash comments can be alone on a line

  /* so can slash-star comments */


  return 0;






















C/C++ data type comment program output


Example #6



// using predefined sizeof() function, to display the data type size, 1 byte = 8 bits
#include <iostream>

   using namespace std;

int main()
  cout<<"The size of an int is:\t\t"<<sizeof(int)<<" bytes.\n";
  cout<<"The size of a short int is:\t"<<sizeof(short)<<" bytes.\n";
  cout<<"The size of a long int is:\t"<<sizeof(long)<<" bytes.\n";
  cout<<"The size of a char is:\t\t"<<sizeof(char)<<" bytes.\n";
  cout<<"The size of a float is:\t\t"<<sizeof(float)<<" bytes.\n";
  cout<<"The size of a double is:\t"<<sizeof(double)<<" bytes.\n";
  cout<<"The size of a bool is:\t\t"<<sizeof(bool)<<" bytes.\n";
  return 0;




C/C++ data type size program output


Example #7


// demonstrates the use of variables
#include <iostream>

   using namespace std;

int main()
     	unsigned short int   Width = 7, Length;
     	Length = 10;
     	// create an unsigned short and initialize with result
     	// of multiplying Width by Length
     	unsigned short int Area  = Width * Length;
     	cout<<"Length: "<<Length<<endl;
     	cout<<"Area: \t"<<Area<<endl;
     	return 0;




C/C++ data type variable program output


Example #8


// calculating the total amount of money earned in n days

#include <iostream>

using namespace std;


int main( )


  int  n;

  int  total, rate= 20;

  cout<<"Enter number of days worked:  ";


  total = n * rate;


  cout<<"\n|   For rate RM20 per day   |";



  cout<<"\nFor "<<n<<" days of work, you have earned $ ";


  return 0;





C/C++ simple mathematics calculation


Example #9



// printing characters base on their respective integer numbers
#include <iostream>

   using namespace std;

int main()
         cout<<"For integer number from 32 till 127,\n";
         cout<<"their representation for\n";
         cout<<"characters is shown below\n\n";
         cout<<"integer     character\n";
         for (int i = 32; i<128; i++)
         // display up to 127...
         cout<<i<<"    "<<(char) i<<"\n";
         // simple typecasting, from int to char
         return 0;




C/C++ data type int and char value


C & C++ programming tutorials











Further C and C++ reading and digging:


  1. The ASCII, EBCDIC and UNICODE character sets reference Table can be found here: Character sets Table.
  1. Check the best selling C/C++ books at Amazon.com.






|< Basic Data Type 2 | Main | Basic Data Type 4 >| Site Index | Download |

Program Constructs & Basic Data Type:  Part 1 | Part 2 | Part 3 | Part 4 |