Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, March 28, 2020

Variables in C

C Variables / Protec

Variables in C:

In C language, Variable is a name of the memory location where program can manipulate the data. This location is used to hold the value of variable. The value of the variable can be change, and can be used many times. It can be any type like integer exampleint, charfloat, double etc.

Variable names are just the symbolic representation of a memory location, so that it can be easily identified.

Syntax to declare variables:

 /* Variable name can only have letters, digits, and underscore */
 Data_type variable_name;

Example 1: Variable Declaration:

 /* Both small x and capital X are treated as two different variables-
  of int type. Since C is a case sensitive language. */
  
 int x, X; 
 float y;    // y is a variable of float type. 
 double z;   // z is a variable of double type. 
 char ch;    // ch is a variable of char type. 
Here, x, X, y, z and ch are name of variables. And e int, charfloat, double etc. are data types.

Initializing a variable:

Initializing a variable means specifying an initial value to assign to it (i.e. before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.

When a local variable is defined, it is not initialized by the system, to use local variable you must initialize it first. Whereas global variable is initialized automatically by the system when you define it. Let’s see the below default value which is automatically generated by the system.

Initializing a Variable | Protec

Example 2: Variable Declaration with Initialization: 

We can initialize the value of variables at the time of declaration.

 /* We can initialize the value of variables at the time of declaration */
  int x = 8, X = 10; // x and X int type with value 8 and 10
  float y = 10.5;    // y is a float type with vlaue 10.5 
  double z = 12.4;   // z is a double type with value 12.4 
  char ch = 'p';     // ch is a char type with value 'p'.  
Example 3: Value of a variable can be change.

 /* We can change the value of a variable. */
 int x = 1; 
 //some code 
 int  x = 10;  // Value of variable x is now been changed to 10. 

Rules for naming a variable

  • A variable name can only have letters, (both uppercase and lowercase letters), digits, and underscore.
  • The first letter of a variable should be either a letter or an underscore.
  • There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.
  • No whitespace is allowed in the variable name.
  • A variable name cannot be any reserved words or keywords. Like int, charfloat, double etc. 
  • Let's see the below example of valid or Invalid variable names:

Example 4: Valid Variable name:

 int number, sum, average, _num1, sum_2; 

Example 5: Invalid Variable name:

 int char; // It cannot be valid variable name because it is reserved words.
 float 2nd_number // Not valid, since an variable name cannot be start with number 
 char first name  // Not valid, since it cannot have white spaces 

More in variables:

There are many types of variables in C programming language.

1) Local Variable: Variables that declared inside a function or block are called local variables. They can be used only inside the fucntion or block of code.

For Example:
#include <stdio.h>
#include <conio.h>

void main()
{
    /* Local Variables, sicnce it is declared inside the body */ 
    int a;
    char b;
    float c;
    clrscr();
    printf("%d is a local variable of int type", a);
    printf("%c is a local variable of char type", b);
    printf("%f is a local variable of float type", c);
    getch();
}

2) Global Variable: Variables that declared outside of any function is called global variables. Global variables hold their values lifetime of your program and they can be used or accessed from anywhere in the program. OR inside any of the functions for the program.

For Example:
#include <stdio.h> 
#include <conio.h>

/* Global Variables, sinice it is declared outside of the body */ 

  int a;
  char b;
  float c;
  void main()
  {
    clrscr();
    printf("%d is a global variable of int type", a);
    printf("%c is a global variable of char type", b);
    printf("%f is a globla variable of float type", c);
    getch();
  }

3) Static Variable: When a local variable is declared with static keyword, then it is known as static variable. For Example: static int a; Here, a is known as static variable.

Visi to know more about static variable. 

4) Constant Variable: In C programming language, such variables or value which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constant like integer, float, character constant etc.

Variable can be declared as constant by using "const" keyword before the datatype of the variable. The constant variable can be initialized only once. And default value of constant variable is zero.

For Example: const int rollno = 12;

Visit to know more about constant variable.  









int x, X; 
 float y;    // y is a variable of float type. 
 double z;   // z is a variable of double type. 
 char ch;    // ch is a variable of char type. 

Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us