C/C++ Variables, Storage Classes, Defining Variables

<<Previous

Next >>





In C program, Variable is a data name that is used to store a data value. Variable is nothing but a location in your computer's memory that hold the different type of data such as integers, floating points, characters and strings. It is also called an identifier. A Variable can have a unique name that differentiates it from the other variables. Each variable in C can be declared with a different data type. These types can convey the information to the compiler about the amount of memory that will have to be allocated to the variable while program execution. During the program execution, the value of the variable can be changed.

Many kinds of variables may exist. That means, integer variable can hold only the integer data, floating point variable can hold only the floating point data and so on. Following are the list of different variables:

intholds the integer data
charholds the character data
floatholds the floating point data
doubleholds extremely large or extremely small floating point data

In addition to that, C programming can also include other variable types such as void, enumeration, volatile, array, string, pointer, union and so on. These will be discussed in upcoming sections.

Rules for defining variables

  • The variables must begin with a character without space
  • It should not start with a digit.
  • Underscore is permitted between the variable.
  • variable length can vary from 1 to 31 characters
  • variable should not be a keyword
  • variable name may be a combination of lower case and upper case letters. C language is case sensitive so we need to take much care while defining the variable name. Ex: ‘ Sum ’ and ‘ sum ’ are not same variables.

Following are the valid variable names:

myname   Use_97    Weight    height    AVRg

Following variables are invalid:

int   const    90usd

Defining the variable in C/C++ programming

The variable must be defined before they are used in the program. The variable definition tells the compiler the data type of the variable being declared and helps in allocating the memory. To declare the variable in C program, we need to specify the data type and followed by list of variable names.

Syntax:

Data-typename  Variablename

Here Data-typename can be an any one of the valid data type such as int, char, float, double etc. The Variablename is an identifier name.

For example;

 int age;
 char m;
 float average;
 double result;
 

We can define more than one variables of the same data type on the same line that are separated by comma. For example, you have to define three variables of double data type on the same line.

double result, average, a ;

Initializing the Variables in C/C++ programming

We assign a initial value to the declared variable. For initialization, we use the assignment operator ‘ = ’ which puts the initial value to the variable.

Syntax:

variable-name = data;

Here the variable-name is the name of the variable where the value is stored. The data can be a number, character or result of the other expression.

For example:

  int a=10;
  char ch='C';
  int a,b,c= 20;
  

Where to declare the variable in C/C++ programming?

There are two places in which the variables are declared.

  • One place is outside of all function. This kind of declaration are called global variables declaration. These variable can be used by all functions.
    	 #include<stdio.h>
    
    	 int avg;
    	 				/* Global variables */
    	 float result;
    
    	 int main()
    	 {
     		/* rest of the program statements */
     	 }
     
  • Other place is inside the open and close curly brace,that means '{}'. These variables are called local variables which work only inside their {} brace.
   #include<stdio.h>

   int main()
   {

       int mark1, mark2;   /* Local variables */

	float avarage;

 	/* rest of the program statements*/
    }
   

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C/C++ Variables, Storage Classes, Defining Variables

<<Previous

Next >>





In C program, Variable is a data name that is used to store a data value. Variable is nothing but a location in your computer's memory that hold the different type of data such as integers, floating points, characters and strings. It is also called an identifier. A Variable can have a unique name that differentiates it from the other variables. Each variable in C can be declared with a different data type. These types can convey the information to the compiler about the amount of memory that will have to be allocated to the variable while program execution. During the program execution, the value of the variable can be changed.

Many kinds of variables may exist. That means, integer variable can hold only the integer data, floating point variable can hold only the floating point data and so on. Following are the list of different variables:

intholds the integer data
charholds the character data
floatholds the floating point data
doubleholds extremely large or extremely small floating point data

In addition to that, C programming can also include other variable types such as void, enumeration, volatile, array, string, pointer, union and so on. These will be discussed in upcoming sections.

Rules for defining variables

  • The variables must begin with a character without space
  • It should not start with a digit.
  • Underscore is permitted between the variable.
  • variable length can vary from 1 to 31 characters
  • variable should not be a keyword
  • variable name may be a combination of lower case and upper case letters. C language is case sensitive so we need to take much care while defining the variable name. Ex: ‘ Sum ’ and ‘ sum ’ are not same variables.

Following are the valid variable names:

myname   Use_97    Weight    height    AVRg

Following variables are invalid:

int   const    90usd

Defining the variable in C/C++ programming

The variable must be defined before they are used in the program. The variable definition tells the compiler the data type of the variable being declared and helps in allocating the memory. To declare the variable in C program, we need to specify the data type and followed by list of variable names.

Syntax:

Data-typename  Variablename

Here Data-typename can be an any one of the valid data type such as int, char, float, double etc. The Variablename is an identifier name.

For example;

 int age;
 char m;
 float average;
 double result;
 

We can define more than one variables of the same data type on the same line that are separated by comma. For example, you have to define three variables of double data type on the same line.

double result, average, a ;

Initializing the Variables in C/C++ programming

We assign a initial value to the declared variable. For initialization, we use the assignment operator ‘ = ’ which puts the initial value to the variable.

Syntax:

variable-name = data;

Here the variable-name is the name of the variable where the value is stored. The data can be a number, character or result of the other expression.

For example:

  int a=10;
  char ch='C';
  int a,b,c= 20;
  

Where to declare the variable in C/C++ programming?

There are two places in which the variables are declared.

  • One place is outside of all function. This kind of declaration are called global variables declaration. These variable can be used by all functions.
    	 #include<stdio.h>
    
    	 int avg;
    	 				/* Global variables */
    	 float result;
    
    	 int main()
    	 {
     		/* rest of the program statements */
     	 }
     
  • Other place is inside the open and close curly brace,that means '{}'. These variables are called local variables which work only inside their {} brace.
   #include<stdio.h>

   int main()
   {

       int mark1, mark2;   /* Local variables */

	float avarage;

 	/* rest of the program statements*/
    }
   

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy