Variable in c program
A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.
How to Define Variable in C ?
Syntax : datatype variable_name;
Example : int a;
This statement create variable a of type int.
The Programming language C has two main variable types
Local Variables
Global Variables
Global variables are declared outside any function, and they can be accessed (used) on any function in the program.
Local variables are declared inside a function, and can be used only inside that function.
#include<stdio.h>
int x; /* global variable */
void main()
{
int a; /* local variable */
a=10; /* Assign value 10 to variable a. */
printf("Value of variable a = %d",a);
}
Output :
Value of variable a = 10
Different Datatypes Available in C
unsigned char 0 to 255 (1 Byte)
signed char -128 to 127 (1 Byte)
int -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 (2 Bytes or 4 Bytes)
unsigned int 0 to 65,535 or 0 to 4,294,967,295 (2 Bytes or 4 Bytes)
short int -32,768 to 32,767 (2 Bytes)
float allow decimal number (4 Bytes)
long extend size of other Datatypes (8 Bytes)
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the Variable in bytes.
Rules for naming a variable
A variable name can have only letters (both uppercase and lowercase letters), digits and underscore.
The first letter of a variable should be either a letter or an underscore.
There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.
Note: You should always try to give meaningful names to variables. For example: firstName is a better variable name than fn.
Post a Comment
If you have any doubts, Please let me know
Thanks!