IDENTIFIERS:
- Identifiers are user defined.
- The name of the variables, functions, labels and various other user- defined objects are called identifiers.
RULES TO DECLARE AN IDENTIFIER NAME:
- An identifier can be a combination of alphabets, digits and underscore provided that first character is not a digit.
- A space cannot be included in identifier name
- As we know C is a case sensitive identifier name like var and VAR are different and also var, VAR, vAr, vaR are also different.
- A identifier name cannot be a keyword or a function
char double; /*double is a keyword*/
int const; /*const is a keyword*/
int return; /*return is a keyword*/
- Some invalid identifier names are
9abc /*cannot start with digit*/
flower vase /*space is included which is not valid*/
1uv*u /* special symbols are not allowed*/
India-Hyderabad /*hyphen not allowed*/
VARIABLES:
- Mathematically variable is a value which can be altered.
- Here in C language, a variable name is a name given to a memory location to which a constant value can be assigned and can be altered.
Syntax:
<data_type> <variable list>;
<data_type> <variable1 >,<variable2>,<variable3>;
- Angled brackets are used for demonstration purpose only and should not be used in your c program.
- In C language you can declare many variables of same data type at a time by separating them with comma operator.
Note: Never forget to end the statement with semicolon else the compiler will generate an error.
- Thus the below statement
a=25;
indicates that a value 25 is placed in memory and that memory location is named as ‘a’.
Ex:
a=25;
after few or next statement
a=25+6;
- Here first we are storing 25 in a and again after few statements, we are storing a=25+6 i.e. 31 is stored in a which is completely possible in C programming language.
RULES TO DECLARE A VARIABLE IN C:
As variable is one of the identifier, rules to declare a variable are same as of identifier mentioned in earlier section.
- A variable name can be a combination of alphabets, digits and underscore provided that first character is not a digit.
- A space cannot be included in variable name
- As we know C is a case sensitive variable name like var and VAR are different and also var, VAR, vAr, vaR are also different.
- In C programming language a variable name cannot be a keyword or a function
char double; /*double is a keyword*/
int const; /*const is a keyword*/
int return; /*return is a keyword*/
- Some invalid variable names are
9abc; /*cannot start with digit*/
flower vase; /*space is included which is not valid*/
1uv*u; /* special symbols are not allowed*/
India-Hyderabad /*hyphen not allowed*/
VARIABLE DECLARATION AND DEFINITION:
- In below mentioned example we are saying to compiler that a variable with name a exists and its type is int. Thus at same time we are defining and declaring the variable a.
- Allocating memory to a variable (ex: ‘a’) but not assigning any value to that particular location such type of statements are the examples.
Ex: int a; valid
We can also declare many variables at a time like
int a,var1,sum,b;
Program:
#include<stdio.h>
extern int x; /* declaring x but not defining it */
int main()
{
int a; /* declaring and defining at same time.*/
}
VARIABLE INITIALIZATION:
- Allocating memory and at the same time storing a value to that location is called initialization.
Example:
int a=5; /* We can also declare and initialization at same time
int a=4,sum=45,c=30;/*Many variables can be initialized at once*/
int 5=a; /* Not valid; constant cannot be assigned a value*/
Program:
/*A program to declare a variable and print the value in it */
#include<stdio.h>
int main()
{
int a; /*variable declaration*/
printf("\nThe value in a is %d",a);
return 0;
}
Output:
The value in a is <garbage_value>
(output is a garbage value, i.e. you might get some random value)
- In above program a memory of 4 or 2 bytes(complier dependent) is allocated and named it as ‘a’ and look at the program we are not storing any value in a.So the output of program is a garbage value.
What is GARBAGE VALUE?
- A garbage value is a value which is stored in memory by previously executed program which could be anything. So thus the result is unidentified.Program:
/*A program to declare a variable and store a value ‘6’ in it and print the value*/
#include<stdio.h>
int main()
{
int a; /*variable declaration*/
a=6; /*storing constant 6 in a*/
printf("/n The value in a is %d", a);
return 0;
}
Output:
The value in a is 6
Program:
/*A program to declare a variable a and store a value ‘6’at same time*/
#include<stdio.h>
int main()
{
int a=6; /* Variable initialization*/
printf("\n The value of a is %d",a);
return 0;
}
Output:
The value of a is 6
CONST KEYWORD:
- We can even make the variable value unchangeable throughout the program by using keyword const. Const stands for constant.
- Const can be used locally and globally.
Syntax:
const <datatype> <variable>=<value>;
- Angled brackets are used for demonstration purpose only and should not be used in your c program.
- Here initializing a variable at the time of declaration is very important.
Example:
const int a=25;//value of a is 25 and it can’t be changed
a=100;/*trying to change value of a from 25 to 100 but it is not possible; it gives an error during compilation*/
Program: (This program yields error)
#include<stdio.h>
int main()
{
const int a=25; /* valid*/
a=36; /* error */
return 0;
}
Output:
error: assignment of read-only variable ‘a’
/*Yields error because we are assigning 36 to a constant variable a*/
No comments:
Post a Comment