Learn Variables in C with Code Example Explanation

In this tutorial, we will learn the C Variables with a code example explanation. Variables are a collection of stored data values such as characters and numbers.

In addition, a variable must start with a letter or underscore while the lowercase and uppercase are recognizable because C language is perceptive. 

Furthermore, in a C program there are various types of variables or it specifies various keywords.

Let’s have a look at the different types of variables.

TypeDescription
1. intis an integer with a whole numbers and without decimals. For example, like 5,10 or -20, -30
2. floatare floating point numbers with decimals. For example, like 99.99, 88.88 or -99.99, 88.88
3. charis consist of single character. For example, like lowercase “a, b, c, d” or uppercase “A, B, C, D”
4. voidIn a C program, a void can be used as a data type which means no data.

However, in C language it allows specifying the different other types of variables. Also we go along we will learn the following tutorials such as structure, arrays, union, enumeration, pointer and etc. In this tutorial, we will discuss the basic types of variables.

Declaration of Variables in C

For declaring a variable, we will need to define the type and assign its value. The variable declaration gives the guarantee to the compiler which exists given type variables and its name. For that a compiler can continue for compiling without involving the complete information about the variable.

Furthermore, a declaration of variable in C is effective when you are using different files. It will be applicable at the time of linking a program.

Also, we will be using the keyword “extern” to declare a variable at any time. Although we can declare variables multiple times in our C language yet they can be specified only once in a function, in code blocks and in a file.

Example of Variables Declaration in C

Let’s have a look at the example, if the variables are declared on the top yet it was specified and inside the main statement.

#include <stdio.h>

// Variable declaration:
extern int x, y;
extern int z;
extern float f;

int main () {

   /* variable definition: */
   int x, y;
   int z;
   float f;
 
   /* actual initialization */
   x = 100;
   y = 200;
  
   z = x + y;
   printf("value of z : %d \n", z);

   f = 100.0/5.0;
   printf("value of f : %f \n", f);
 
   return 0;
}

Output:

value of z : 300
value of f : 20.000000

Variable Definition in C

In a C language, the variable definition instructs the compiler how much storage to build for a variable. Furthermore, a variable definition defines a data type and consist of a list of either one or more variable type.

Moreover, a type must be original in C data types such as int, char, double, bool, float and any user-defined functions.

Apart from this, a variable_name must contains at least one or more identifier names that will be separated by commas.

Also you may read or Visit: Structure of C Program with Example

Here are examples of valid declarations:

int x, y, z;
char a, ch;
float fl, price;
double b;

In a line code of int x, y, z; it is specified and declares the variables used which are x, y, and z. To tell the compiler to make a variable named x, y and z which is int type.

On the other hand, the variables can be assigned a value for declaration. The initializer must contain an equal sign and it must be followed by constant functions.

Syntax Description:

type variable_name = value;

Here are the other examples of variables definition in C

ExamplesDescription
extern int a = 2, b = 9;It shows a declaration of a and b.
int a = 2, b = 9;It shows a definition and initializing a and b.
byte c = 18;It shows a definition and initializes c.
char y = ‘y’;It shows the variable y and it has the value ‘y’.

Whereas, a definition without an initializer and with a static duration is essentially initialized with a NULL. In other words, all the bytes have a value of 0 while the initial value of other variables is unspecified.

Also Read: C Environment Setup in Linux, Mac OS, and Windows

Format Specifiers

In a C language, format specifiers can be used together with a printf() statement which is to instruct the compiler if what type of data variable is stored.

Additionally, the format specifiers begin with a percentage symbol like this (“%”) and then it is followed by a character,

As an example, we will output the value of a variable int and we must used the format specifier “%d” or “%i”. Then it must be surrounded with a double quotes sign inside in a print() statement.

Let’s have a look at the example:

#include <stdio.h>

int main() {
  int number = 18;
  printf("%d", number);
  return 0;
}

Output:

18

Let’s have another example which is to print the other types, by using the %f for float(floating) and %c for char(character) :

#include <stdio.h>

int main() {
  // Create variables
  int number = 2022;              //This is an Integer (whole number)
  float floating_numbers = 18.61;     //This is a Floating point number
  char alphabet = 'S';         // This is Character
  
  // This is to a Print variables
  printf("%d\n", number);
  printf("%f\n", floating_numbers);
  printf("%c\n", alphabet);
  return 0;
}

Output:

2022
18.610001
S

The other example we will show, which is to merge both a text and a variable, then separate them with a comma inside the printf() statement:

#include <stdio.h>

int main() {
  int number = 18;
  printf("My beloved number is: %d", number);
  return 0;
}

Output:

My beloved number is: 18

This example is to print various types in a single printf() statement, by using the following:

#include <stdio.h>

int main() {
  int number = 15;
  char letter = 'S';
  printf("The number will show is %d and the letter will show is %c", number, letter);
  return 0;
}

Output:

The number will show is 15 and the letter will show is S

Change C Variable Values

In a C program, If we assign a new value to the existing variable, it will change the previous value of a variable:

As an Example

#include <stdio.h>

int main() {
  int number = 21; // The value of a number is 21
  number = 18; // The value of a current number is 18
  
  printf("%d", number);
  return 0;
}

Output:

18

Moreover, we can assign the value of one variable to another variable: Let’s have a look at the example below.

#include <stdio.h>

int main() {
  int number = 21;
  
  int another_number = 51;

  // Assign the value of another_number  (21) to number
  number = another_number;

  // number is now 51, instead of 21
  printf("%d", number);
  
  return 0;
}

Output:

51

Conclusion

In conclusion, you have an understanding and learning about the variables in C, thus how to assign a value for variables. Furthermore, we also learned the declaration of variables in C, the variable definition in C, format specifiers and change C variable values.

In the previous tutorial, we discussed the Learn C Basic Syntax Rules with Examples and for the next tutorial, we will discuss the “C Constant.

Leave a Comment