bscodinglab

Variables

A variable is a named storage location in computer memory that can hold a value, such as a number, a string, or a reference to an object. Variables can be assigned different values during the execution of a program.A variable definition specifies a data type and contains a list of one or more variables of that type

     Example :-  Int, char, float, array, strings etc.

The specific types of variables and their syntax may vary depending on the programming language used.

The general rules for naming variables in most programming languages are

  1. The name of a variable must begin with a letter (a-z, A-Z) or an underscore character (_).
  2. Subsequent characters in the name may be letters, digits (0-9), or underscore characters.
  3. Variable names are case sensitive, meaning “myVar” and “myvar” are considered different variables.
  4. Variable names should be descriptive and meaningful to help make the code more understandable.
  5. Avoid using reserved keywords as variable names.
  6. Use camelCase or snake_case convention for naming variables.
  7. Do not use special characters in variable names, such as !, @, #, $, %, ^, &, *, (, ), +, =, {, }, [, ], , |, ;, :, ‘, “, <, >, ?, /, ., or ,.
  8. Keep the variable name length reasonably short, typically less than 20 characters.
  9. Avoid starting variable names with a numeral or digits.
  10. Following these rules for naming variables can help make your code more readable and easier to maintain.

    Type Variables in C :-  Thare are five types of variable in ‘C’.

Variables Types
Local Variable
Globle Variable
Static Variable
Autometic Variable
External Variable

The specific types of variables and their syntax may vary depending on the programming language used.

Local Variable :-

A variable  that is declear in the blocks and function is call Local variables.the local variable is used only into the block whare that is declear  out side this variable is not use . hare I given a example of local variable.

   Example :-  void demo () 

                       {

                       Int  a = 20; // Local variable of Intiger.

                       float b = 5.30; // Local variable of Float.

                        }

Note :- Hare I create a local variable into the function demo it can be use only into the demo function.

Globle Variable :-

A variable that is declear out side of the block and any function is called globle variable.globle variable is use any ware in the program into the block and function and out side of block and function.

Example :-  int x = 30;   // Globle variable of Intiger.

                      void demo ()

                     {

                         Int  a = 20;    // Local variable of Intiger.

                         float b = 5.30;   // Local variable of Float.

                      }

Note :- Hare I create a Globle variable Inside the Program it can be use Anyware into the Whole Program.

Static Variable :-

A variable that is declared with the static keyword is called static variable. It uses the “static” keyword before its definition. The keyword helps the compiler to understand the nature of the variable. The significant property of a static variable is holding its last value even after the end of the function. When you call a function, the static variable will give you the updated value.

Note :- If you call this function many times, the local variable will print the same value for each function call,Example hare print all time same value of variable like 5 5 5But the static variable will print the incremented value in each function call,Example print every time 1+ value like 5 6 7.

Exampe :-     void functionA()

                      {

                           int a=15;//local variable 

                           static int b=25;//static variable 

                            printf(“%d,%d \n”,a,b);

                            printf(“%d,%d \n”,a,b);

                        } 

Note :- Hare I create a Globle variable Inside the Program it can be use Anyware into the Whole Program.

Autometic Variable :-

Variables declared with “auto” Keywords are known as automatic variables. these variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block. The variables declared inside the block of functions are automatic variables by default. The scope of automatic variables is inside the block in which it is declared. 

Example :-

      int main()
      {
         auto int number = 15;
         function automatic()
{ auto int number = 20; printf("Inside number: %d", number); } printf("\n"); printf("Outside number: %d", number); return 0; }
Output:- the Second number: 20
the Frist number: 15

External Variable :-

When We have to use a variable in multiple C source files. Then we can use  by an external variable. To declare an external variable, you need to use extern keyword. To use external variable with save file name as” file-name.h” .h Extention.

Example :-  hare we create a file with “variables.h” write in this file some variables declearation.

          int a,b,c;
          float x,y,z;

write some code with another file using external variables in this file.

#include <variables.h>
#include <stdio.h>
void  valueprint()
{
     a=10;   b=20;   c=30;
     x=3.15;  y=7.21;   z=9.5;
     printf(“a: %d b: %d c: %d”, a,b,c);
     printf(“\n x: %f y: %f z: %f”, x,y,z);
     getch();
}

Output :-  a: 10    b: 20    c: 30

                   x: 3.15    y: 7.21    z: 9.5

Note :- Hare I create a Globle variable Inside the Program it can be use Anyware into the Whole Program.