×

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.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.