Variables in C:
int
, char
, float
, double
etc.
Variable names are just the symbolic representation of a memory location, so that it can be easily identified.
Syntax to declare variables:
/* Variable name can only have letters, digits, and underscore */
Data_type variable_name;
Example 1: Variable Declaration:
/* Both small x and capital X are treated as two different variables-
of int type. Since C is a case sensitive language. */
int x, X;
float y; // y is a variable of float type.
double z; // z is a variable of double type.
char ch; // ch is a variable of char type.
int
, char
, float
, double
etc. are data
types.
Initializing a variable:
Initializing a variable means specifying an initial value to assign to it (i.e. before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.
When a local variable is defined, it is not initialized by the system, to use local variable you must initialize it first. Whereas global variable is initialized automatically by the system when you define it. Let’s see the below default value which is automatically generated by the system.
Example 2: Variable Declaration with Initialization:
We can initialize the value of variables at the time of declaration.
/* We can initialize the value of variables at the time of declaration */
int x = 8, X = 10; // x and X int type with value 8 and 10
float y = 10.5; // y is a float type with vlaue 10.5
double z = 12.4; // z is a double type with value 12.4
char ch = 'p'; // ch is a char type with value 'p'.
/* We can change the value of a variable. */
int x = 1;
//some code
int x = 10; // Value of variable x is now been changed to 10.
Rules for naming a variable
- A variable name can only have 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.
- No whitespace is allowed in the variable name.
-
A variable name cannot be any reserved words or keywords. Like
int
,char
,float
,double
etc. - Let's see the below example of valid or Invalid variable names:
Example 4: Valid Variable name:
int number, sum, average, _num1, sum_2;
Example 5: Invalid Variable name:
int char; // It cannot be valid variable name because it is reserved words.
float 2nd_number // Not valid, since an variable name cannot be start with number
char first name // Not valid, since it cannot have white spaces
More in variables:
1) Local Variable: Variables that declared inside a function or block are called local variables. They can be used only inside the fucntion or block of code.
#include <stdio.h>
#include <conio.h>
void main()
{
/* Local Variables, sicnce it is declared inside the body */
int a;
char b;
float c;
clrscr();
printf("%d is a local variable of int type", a);
printf("%c is a local variable of char type", b);
printf("%f is a local variable of float type", c);
getch();
}
2) Global Variable: Variables that declared outside of any function is called global variables. Global variables hold their values lifetime of your program and they can be used or accessed from anywhere in the program. OR inside any of the functions for the program.
#include <stdio.h>
#include <conio.h>
/* Global Variables, sinice it is declared outside of the body */
int a;
char b;
float c;
void main()
{
clrscr();
printf("%d is a global variable of int type", a);
printf("%c is a global variable of char type", b);
printf("%f is a globla variable of float type", c);
getch();
}
3) Static Variable: When a local variable is declared with static keyword, then it is known as static variable. For Example: static int a; Here, a is known as static variable.
4) Constant Variable: In C programming language, such variables or value which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constant like integer, float, character constant etc.
int x, X;
float y; // y is a variable of float type.
double z; // z is a variable of double type.
char ch; // ch is a variable of char type.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.