×

Variable Declaration in C

What is a Variable?

A Variable is nothing more than a name for a memory place where data/information can be stored.

Any alphabet (from a to z or A to Z), the digit (0 to 9), and underscore (_) combination can be used for the variable name.

Variable names that begin with digits are not valid.

Every variable has a type, or data type (such as int, char, float, or double), which instructs the compiler on how much memory to allot to the variables in accordance with their type and how to handle bits contained in that specific memory location when carrying out certain operations.

 Example:

char Car ='Toyota';

 Car is a char type variable in this case. The value "Toyota" is given to the variable car.

Variable Declaration:

The variable must be declared before being used in various functional components. The compiler receives two instructions from a variable declaration:

  • The variable's name
  • What kind of information will the variable store

The general syntax for declaring a variable

There are two ways to declare variables in statements that use variables:

1. Declaration of a variable without assigning any initial data to it:

Syntax :- data_type variable_name;
 
//example
 int a;

In the above example, the data type is “int”, and the variable name is “a” without any initialization of a value.

2. Declaration of a variable with assigning any initial data to it:

Syntax :- data_type variable_name=value;
 
//example
 int a=6;

In the above example, the data type is “int”, and the variable name is “a” with an assigned value of “8”.

Ways of Variable Declaration

In C programming, variables can be declared in two different ways.

1.    Primary Type Declaration

2.    User Defined Type Declaration

1- Primary Type Declaration:

In this type of declaration, we use the Primitive data type, which is also known as the built-in data type.

The primary data types that are most frequently used are int, float, char, boolean, double, long, etc.

  • Single Primary Type Declaration

We declare a single variable.

    float percentage = 86.83;
  • Multiple Primary type declarations in the same line

We can declare multiple variables in the same line with the help of a comma(,) which separates the multiple variables.

int a=5,b=6,c=7;
  • Multiple primary type declarations in the different line

We can declare multiple variables in the different lines. To separate the numerous variables for this, we must use a semicolon (;).

 int k=78;
float l = 87.8;
char m = 'c';

2-User Defined Type Declaration:

Data types that have been created by a user are known as user-defined data types. Since they are not built-in, class functions must be used to access them.

Struct, Union, enum, typedef, etc., are a few of the most often used data types.

1.typedef :

In order to declare the data type, we must use the keyword typedef. A feature called "type definition" in C programming enables a programmer to define an identifier representing an existing data type. Later in the program, variables can be declared using the user-defined identifier. The general syntax for declaring a variable using a user-defined type declaration is:

    typedef int student_age;
    typedef char student_name;
    typedef float student_percentage;

student_age, student_name, and student_percentage are three new data types we have established here. Now we are having these data types, we can declare variables, as seen below.

    student_age 16,17,18;
    student_name Om,Shiv,Shankar;
    student_percentage 88.73,88.40,90.2;

2. Union:

Unions give you the ability to make variables that share a shared memory area. In that they seem to be an aggregate data type. Unions and structures have some similarities. Union is used when we just need to reach one member at a time. A union variable's memory usage is equal to the amount of storage space required by the largest data member of the union.

   union bag
  {
    char bookname[5] ;
    int pen;
  };
 

 3.Structure:

The structure is also a user-defined data type in the C programming language that enables us to aggregate data of various sorts. A complex data type that is more meaningful can be built with the aid of structure.

To define a structure, use the struct keyword. A new data type called struct, which is made up of both primary and derived data types, is defined.

   struct player
  {
   int player_no;
   Char player_name;
  }  

Rules For Variable Declaration

We must declare a variable in C language with an appropriate data type and variable name. The following are some guidelines to remember when declaring a variable in C:

  • Any alphabet, number, and underscore combination are allowed in a variable name.
  • The variable name, however, shouldn't begin with a number.
  • It will be better if we define variable names with some relevant names, as this will make the variable's function more obvious.
  • The C language is case sensitive, and it treats lowercase and uppercase extremely differently. Typically, we maintain the variable name in lowercase.
  • A semi-colon must follow each declaration statement (;)
  • It is improper to declare two variables with the same name in the same scope.

Related Topics

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.