Data Type and Variable in C/C++


In the previous section, you've seen details about the first program in C/C++ programming language. Now I shall discuss an important topic in C/C++ programming named data type.

Data type basics
To work with computer and computer programming, a programmer must handle various information or data. This data may be either numeric or string. Before programming with C/C++ for any application, you must decide which kind of data you want to handle. C/C++ gives you four basic fundamental data types to handle this information.



These fundamental data types are...

                         1. Boolean type
                         2. character type
                         3. integer type
                         4. floating type

Boolean Type
This fundamental type contains only two values either true or false. It takes 1 byte memory from computer's system memory to locate this flag. To use this type, you should declare it using this bool keyword. 

Character type
This fundamental type contains only a single character. To use character from a to z, you must declare this data type. In C/C++ language this type is represented by the char keyword. It will take 1 byte memory from the system's memory.

Integer type 
To use numeric value without floating, this fundamental data type is used. It takes 2 byte memory. This type is declared by int keyword. You can use only -32768 to 32767 range's number using this type. If you use number which goes over this range, you will find error or garbage value. Say, you want to add two numbers like 555 and 789. In this case, you can use this type. But you want to two numbers say 5555 and 6873. For this situation, you cannot use type. Sometimes you need to handle number greater than this range. Then what will you do? Don't worry, C/C++ provides you more flexibility. You can use long modifier before int keyword. This  long int occupies 4 byte memory. So, you can handle numeric value between -2147483648 to 2147483647. Another modifier named unsigned  helps you to increase your range. Say, you don't need to use negative number. In this case, if you declare unsigned int, you can handle numeric value between 0 to 65535. You don't need to use signed modifier although C/C++ supports it because all data types are signed by default.

Floating type
To use numeric value which is non integer like 55.55, this floating data type is used. In C/C++ this type is declared by float keyword. This keyword takes 4 byte memory from the system's reserved memory.The numeric range of this keyword is +/- 3.4e +/- 38 (~7 digits). Using unsigned keyword, you can increase this range. Another floating data type named double which doubled this range is used in C/C++. This data type can take numeric value between +/- 1.7e +/- 308 (~15 digits). You can use long modifier before this data type keyword.     


Variables
By declaring data type, you tell the compiler what kind of data you want to use and how much memory space you require to put this data. Using variable concept of C/C++, you can define a name for the taken memory space which will help you to remember this memory space as your flexible way. If you define a name for a memory location, you can call this memory by this given name and use this name through your program.    

How to declare variables
To declare a variable, you have to define and declare what kind of data you want to use and  provide a name for that taken memory space like this...

                                 Data_type_name  Variable_name;
                    Example:
                                     bool flag;
                                     char key;
                                     int a;
                                     float number;

At the above example bool, char, int and float are data type name and flag, key, a, number are variable name. Here bool, char, int and float are reserved keyword. So, you must declare these as mentioned above but you can provide any meaningful name of the variable.

Rules to declare a variable
You can define any meaningful name for the variables but the limitations are....
  • Variable name must start by any character or underscore( _ )  or dollar sign ($). Such as myNumber, _flag, $name etc are valid variable name.
  • A variable name cannot start with number or these special key (%,&,!,#,*). Such as 7name, #flag, &number etc are invalid variable name.
  • A variable name cannot be reserved keyword of C/C++.
 C/C++ reserved keyword
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while.


Lab work on Data Type in C/C++ programming

From the above section, you have gathered knowledge about Data Type and Variables. Now it is time to do some practical work. In the below program, you'll see the practical example. So, try and get result.

#include<stdio.h>
#include<iostream>
using namespace std;
void main()
{
  // Data type and variable region
  bool flag = true;
  char c ='A';
  int a = 10,b = 6;
  float x = 40,y = 2.5;
  int add_result;
  float div_result;

  //C++ programming style
  cout<<"Boolean value is :"<< flag <<endl;
  cout<<"Character data type value is :"<< c <<endl;

  // C Programming style
  add_result=a+b;
  printf(" %d + %d = %d\n",a,b,add_result);
  div_result=x/y;
  printf("%.2f/%.2f = %.2f\n",x,y,div_result);
}


The output is shown here.........


Data Type and Variables output

Here all the functions and commands  I used are familiar with you because I discuss them in previous section heading elaboration the first program in C/C++ programming.  The only new items are format specifier (%d or %f) which is used to display the content of the variable at the located space. Some important format specifier and its description is given below. Look deeply, if you use cout command, you don't need to use format specifier. You can use both of them when you need.

Format specifier  
  1. %d specifier: To display integer value %d is used. For long int, %ld should be used.
  2. %c specifier: To display floating number %f is used. It will display 6 digit after point. So, if you want, you have to indicate how many numbers you want to show after point like %.2f which show 2 digit after point.
  3. %c specifier:  To display character, you have to use this indication.
In C/C++ language Boolean value is represent either 0 or 1 where  0 indicates false and 1 indicates true. In this code I used three comments to understand the program clearly. The line which starts with double slash (//) indicates a comments and is ignored by the compiler.


In this section you have learned two important topics Data Type and Variables used in C/C++ programming. These two items often have to be used to any C/C++ program. So care should be taken here. In the later section section, I'll discuss more important and fundamental topics used in C/C++ Programming Language.

4 comments:

  1. Reverse Phone Lookup to Protect Your Teenage Children
    - Many have teenagers have cell phones these days and they serve as a great aid
    to parents to enable them to stay in touch on their children when they are away from home for any reason.
    A quick research showed that major reverse cell phone lookup services have a few
    pricing level. They must reflect the rate for an extensive
    finding varying from 99 cents to $69.

    ReplyDelete
  2. He published magazine articles in his own publication titled
    Skin Inc. For a faster and reliable result, skin care professionals may recommend these therapies that are
    less-invasive to the body. You must understand that the body is designed to
    move.

    Here is my blog post ... Cellulitis treatment

    ReplyDelete
  3. It's because the noises are only heard by the patient. Hearing damaged by noise being the biggest cause of tinnitus in the 8% of the population who suffer from tinnitus. A claimant have to be capable to prove that the injuries has resulted due to the negligence of the other human being.

    My webpage ... cure for tinnitus

    ReplyDelete
  4. bigger nails often seem to have the tendency of nail biting.
    To stop biting nails, your physician might use B vitamin inositol which enhances serotonin activity in the brain resulting in lessening of Nail biting
    tendencies. particular finger you won't ever bite and can perform the biting on remaining fingers.

    ReplyDelete