Home » »

Explain how a variable differs from a constant.

September 23, 2019

Explain how a variable differs from a constant.

The main difference between constant and variable in C programming is that a constant is similar to a variable, but it cannot be modified by the program once it is defined while a variable is a memory location that holds data.
C is a structured programming language developed by Dennis Ritchie. It has various programming structures such as loops, functions, and pointers. Defining constants and variables are initial steps to write a program. A constant refers to a fixed value, and it cannot be changed after defining.  On the other hand, a variable is a name to identify a specific memory location. A programmer can assign a value to a variable and use that variable throughout the program. Each variable has a specific data type. A variable declared to store an integer cannot be used to store a floating point value.  

What is Constant in C Programming

A constant is a fixed value that cannot be changed after defining.  They are also called literals. The constants can be of various data types. There can be integer constants, floating constants, character constants and enumeration constants. In C, there are two ways to define a constant. They are by using the #define preprocessor and by using the const keyword.

What is Variable in C Programming

A variable is a container to hold data. It is a name to identify the storage area. Every variable has a unique name to identify it. A variable name can have uppercase and lowercase letters, digits and underscores. It is a good practice to use meaningful names for variables. C is a case-sensitive language. Therefore, the variable name width is different from WIDTH.
A variable can store a particular data type. The ‘int’ variables can store an integer (5,20 etc.). The ‘char’ can store a single character such as ‘A’, ‘a’ etc. Moreover, ‘float’ is used to store a single precession floating point value while ‘double’ is used to store double precision floating point values. Refer below examples.



Share this article :
Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

FB Page

 
@ELITE_Mv
Copyright © 166/A-1/2017/19. ELITE Institute - All Rights Reserved