Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Basic Variable Types 
2.1.
1. char
2.2.
2. int
2.3.
3. float
2.4.
4. double
2.5.
5. void
3.
Variable Definition in C
3.1.
Example
4.
Variable Declaration in C
4.1.
Example
5.
Lvalues and Rvalues in C
5.1.
Lvalue
5.2.
Rvalue
6.
Frequently Asked Questions
6.1.
What is a variable?
6.2.
How to represent variables in C?
6.3.
How to declare variables?
6.4.
How to access Variables in C?
7.
Conclusion
Last Updated: Jun 25, 2024
Easy

Variables in C Language

Author Gaurav Gandhi
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A variable is a name given to a storage location that our programs can control. Each variable in C has a particular kind, which determines the size and layout of the variable's memory, the range of values saved inside that memory, and the set of operations applied to the variable.

Variables in C Language

Basic Variable Types 

1. char

C uses char kind to store characters and letters. The char type is integer type because C stores integer numbers rather than characters. In C programming, char values are stored in 1 byte in memory, ranging from -128 to 127 or 0 to 255.

2. int

Int, short for "integer," is a basic variable type built into the compiler and used to define numeric variables retaining whole numbers. ... C, C++, C# and many different programming languages understand int as a data type.

3. float

Float is a shortened period for "floating point." using the definition. It is a fundamental data type built into the compiler. It is indeed used to define numeric values with floating decimal points. C, C++, C#, and many different programming languages understand float as a data type.

4. double

A double is a data type in c language that stores high-precision floating-point data or numbers in computer memory. It is called double data type because it can hold the double size of data compared to the float data kind. A double has 8 bytes, the same as 64 bits in length.

5. void

In computer programming, while void is used as a function return type, it indicates that the function does now not return a value. While void appears in a pointer statement, it specifies that the pointer is universal. when used in a function's parameter listing, void indicates that the function takes no parameters.
 

Also see, Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Variable Definition in C

A variable definition tells the compiler how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or extra variables of that type as follow: 

type variable_list;

 

Valid declarations are shown here −

int x, y, z;
char c;
float f;
double d;

The line int x, y, z; declares and defines the variables x, y, and z, which instructs the compiler to create variables named x, y, and z of type int.

Variables can initialize in their statement. using a constant expression as follows −

type variable_name = value;

Example

extern int d = 4, f = 6; // declaration of d and f. 
int d = 4, f = 6; // definition and initializing d and f. 
byte z = 20; // definition and initializes z. 
char a = 'a'; // the variable a has the value 'a'.

Also see, Floyd's Triangle in C

Variable Declaration in C

A variable declaration presents a guarantee to the compiler that there exists a variable with the given type and name so the compiler can continue for a similar compilation without requiring the complete detail about the variable. A variable definition has its that means at the time of collection only. The compiler desires actual variable definition at the time of linking this system.

A variable declaration is helpful when you use multiple files, and also you define your variable in one of the files so that it will be available at the time of linking the program. You will be using the keyword extern to declare a variable at any place. Although you can declare a variable multiple times to your C program, it could be described only once in a particular file, a function, or a block of code.

Also read - Bit stuffing program in c

Example

#include <bits/stdc++.h>
using namespace std;
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () 
{
   /* variable definition: */
   int a, b;
   int c;
   float f;
   /* actual initialization */
   a = 10;
   b = 20;  
   c = a + b;
   printf("value of c : %d \n", c);
   f = 70.0/3.0;
   printf("value of f : %f \n", f);
   return 0;
}

Output

value of c: 30
value of f: 23.333334

Lvalues and Rvalues in C

Two types of expressions in C −

Lvalue

"l-value" refers to a memory location that is identifies an object. l-value can also seem as both left-hand or right-hand sides of an assignment operator(=). l-value frequently represents an identifier—a facet of an assignment.

Rvalue

Rvalue" refers to the data value saved at a few addresses in memory. A rvalue is an expression that couldn't have a value assigned to it. This means that rvalue can seem on the right but not on the left-hand side of an assignment operator(=).

You can also read about the memory hierarchy, Also read about Tribonacci Series

Also read, odd or even program in c

Frequently Asked Questions

What is a variable?

In c programming, a variable is a value that can change, depending on conditions or on information passed to the program.

How to represent variables in C?

  • Variable call ought to start with a letter or underscore.
  • Variables are case sensitive
  • They may be constructed with digits letters.
  • No special symbols are allowed other than underscore.
  • sum, height, _value are a few examples for variable name

How to declare variables?

  • Variables must be declared in the C program before applying.
  • Memory space isn't allocated for a variable at the same time as a declaration. It takes place only on the variable definition.
  • Variable initialization approach assigning a value to the variable.

How to access Variables in C?

Variables that might be declared inside a function or block are known as local variables. They can be used most effectively by means of statements that can be inside that function or block of code. Local variables aren't recognized to functions outside their personal.

Conclusion

We learned about C Variables, how to create a variable, assign value to a variable, what occurs when a variable is made, and the declaration of a variable. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass