C

In C, Variables – The Most Detailed

A variable is a memory location that stores data. For example, an int variable num has a value of 10 (int num = 10), and the variable name “num” represents the memory location where this value 10 is stored. A variable’s value, as the name implies, can be changed an unlimited number of times.

Variable declaration in syntax

data_type variable name; 

For example:

//a variable num of int type
int num; 

//two variable ch1 and ch2 of char type
char ch1, ch2; 

//three variable x, y and z of float type where y has been
// initialized with a value and other variables x & z are 
// un-initialized.
Float x, y=10.5, z;

Variables in C, for example

#include <stdio.h>

int main()
{
  int num1 = 20, num2 = 50;
  char ch = 'A';
  float x = 10.5, y = 13.5;
  printf("Variable 'ch' value: %c\n", ch);
  printf("Variable num1 and num2 values: %d\t%d\n", num1, num2);
  printf("Variable x and y values: %f\t%f\n", x, y);
  return 0;
}

Variable definition rules

1. Variable names may contain alphabets, digits, and underscores.
2. Variable names can only begin with alphabets and underscores and cannot begin with digits. For example, _num and num 1 are both valid variable names, but 9num and 99 num are not.
3. Whitespaces are not permitted in variable names. For example, the names num 1, num 2, my var are invalid because they contain whitespace.
4. A keyword or reserved word cannot be used in a variable name. For example, “if,” “else,” and “int” are all invalid names because they are keywords.

Variable types in C

1. a variable found locally
A variable declared within a function or a block is referred to as a local variable because its scope is limited. A variable declared within a function can only be accessed within that function; its scope is restricted to that function and cannot be accessed outside of it.
A variable declared within a block, similarly, cannot be accessed outside of that block.
It is important to note that a local variable must be initialized before it can be used.
Example of a Local Variable:

#include <stdio.h>
void myfunction() {
  // This is a local variable
  int num = 99;
  printf("Local variable num value: %d", num);
}
int main()
{
  myfunction();
}

Output:

Local variable num value: 99

2. International variable

If a variable can be accessed by all of the program’s functions and blocks, it is referred to as a global variable. The scope of the global variable is limited to the program rather than the specific function or block. This means that a global variable can be accessed within a program but not outside of it.
It is stated at the beginning of the program.
The Global Variable Example:

#include <stdio.h>
//This is a global variable
int num = 99;
void myfunction()
{
  printf("%d\n" , num);
}
void myfunction2()
{
  printf("%d\n" , num);
}
int main() {
  myfunction();
  myfunction2();
  return 0;
}

Output:

99
99

3. a fixed variable

The static keyword is used to declare a static variable. It keeps its value even after multiple function calls. Static variables are only initialized once. The compiler keeps their values until the program terminates. Static variables can be declared both within and outside of a function. Static variables have a default value of zero.
Example of a Static Variable:
A local variable and a static variable are declared inside a function in the following example. The function is called repeatedly in the main() method, and because there is a print statement in the function, the values num and num2 are printed on each function call. As shown in the output, the local variable value is the same for each function call, whereas the static variable retains its value from the previous function call.

#include <stdio.h>
void myfunction(){
  //This is a local variable
  int num = 20;

  //This is a static variable
  static int num2 = 20;
  num = num + 100;
  num2 = num2 + 100;
  printf("\n Value of num: %d, value of num2: %d",num,num2);
}
int main() {
  //Calling myfunction() repeatedly to print num and num2
  //values for each repeating function calls.
  myfunction();
  myfunction();
  myfunction();
  myfunction();
  return 0;
}

4. Automated variable

Automatic variables, like local variables, have a scope that is limited to a single function or block. By default, local variables within a function or block are automatic variables. The auto keyword can be used to explicitly mark a variable as automatic.
Example of an Automatic Variable:

void function()
{
  //This is also an automatic variable by default
  int num = 100;

  //This is how you can explicitly declare variable as automatic
  auto int num2 = 150;
}

5. External variable

External variables are more general than global variables. A global variable’s scope is limited to the current c file, which means it cannot be accessed by another c file. External variables, however, can be accessed by other C files because they are shared by multiple C files. Using the extern keyword, we can declare an external variable.
Variable External Example:
Using the extern keyword, you can declare a variable as external. Because the scope of extern is greater than the scope of global variable, the extern variable, like global variables, can be accessed by all functions and blocks in the same C file. External variables, unlike global variables, can be accessed outside of the C file. A C file’s extern variable can be accessed by another C file.

extern int num = 99;

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button