Vous êtes sur la page 1sur 19

Variables Home

No Topic
3.1 Basic Building Blocks in C
3.2 Variable in C Programming :

What is Variable ?

Variable Type : Local Variable

Variable Type : Global Variable

Fundamental Attributes of Variable

Rules for Declaring Variable

L-Value of Variable

R-Value of Variable
3.3 Constant in C
3.4 Type of Constants : Character | Integer | String
3.5 Backslash Character : List | Properties
3.6 C Programming Character Set
3.7 Keywords in C
3.8 Special Characters in C : Backslash Characters
3.9 Backslash Characters with Examples
3.10 White Space Characters in C
3.11 Expression in C


C Tokens Chart
In C Programming punctuation,individual words,characters etc are called tokens.
Tokens are basic building blocks of C Programming

Example :
No Token Type Example 1 Example 2
1 Keyword do while
2 Constants number sum
3 Identifier -76 89
4 String HTF PRIT
5 Special Symbol * @
6 Operators ++ /
Basic Building Blocks and Definition :
Token Meaning
Keyword
A variable is a meaningful name of data storage location in computer memory. When
using a variable you refer to memory address of computer
Constant Constants are expressions with a fixed value
Identifier The term identifier is usually used for variable names
String Sequence of characters
Token Meaning
Special
Symbol
Symbols other than the Alphabets and Digits and white-spaces
Operators A symbol that represent a specific mathematical or non mathematical action

What is Variable in C Programming
Variable in C Programming is also called as container to store the data. Variable name may
have different data types to identify the type of value stored. Suppose we declare variable of
type integer then it can store only integer values.Variable is considered as one of the building
block of C Programming which is also called as identifier.
A Variable is a name given to the memory location where the actual data is stored.
Consider real time example , suppose we need to store water then we can store water in glass
if quantity of water is small and Bucket is the quantity of water is large. And big can to store
water having quantity larger than bucket similarly Variable (i.e Value container) may have
different size for storing different verities of values.
Must Read : Fundamental Attributes of Variable
What is Variable in C Programming?
1. Initially 5 is Stored in memory location and name x is given to it
2. After We are assigning the new value (3) to the same memory location
3. This would Overwrite the earlier value 5 since memory location can hold only one value at a
time
4. Since the location xcan hold Different values at different time so it is refered as Variable
5. In short Variable is name given to Specific memory location or Group.


Consider following Scenario -
Step 1 : Memory Before Variable Declaration

Initially before declaring variable ,We have Only memory.
Memory is block of bytes.
Each byte is filled with random or garbage data.
Step 2 : Declaring Variable in C Programming

1. Now we have declared a variable. (in this case we have declared character variable)
2. Compiler Checks data type . Depending on data type it will allocate appropriate bytes of
memory. (in this case Compile will allocate 1 byte because we have declared Character data
type)
3. Its only declaration so , garbage value inside that address remains the same.
Step 3 : Initialize Variable in C Programming

1. We have Initialize character variable now.
2. After initializing value inside that memory block gets overwritten.
Conclusion :
1. Variable Name always holds a single Value.
2. Variable Name is user defined name given to Memory Address.
3. During Second Run , Address of Variable may change.
4. During second Run , Value set inside variable during current will be considered as garbage..
What is Local Variable ?
1. Local Variable is Variable having Local Scope.
2. Local Variable is accessible only from function or block in which it is declared .
3. Local variable is given Higher Priority than the Global Variable.
Above Fig. Tells us that Above Program has 2 blocks i.e Inner Block and Outer Block

Global Variable :
1. Global Variable is Variable that is Globally available.
2. Scope of Global variable is throughout the program [ i.e in all functions including
main() ]
3. Global variable is also visible inside function , provided that it should not be re-
declared with same name inside function because High Priority is given to
Local Variable than Global
4. Global variable can be accessed from any function.

#include<stdio.h>
int var=10;
void message();
void main()
{
int var=20;
{
int var = 30;
printf("%d ",var);
}
printf("%d ",var);
message();
}

void message()
{
printf("%d ",var);
}
Output :
30 20 10
Inside message() var is not declared so global version of var is used , so 10 will be
printed.
{ int var = 30; printf("%d ",var); }
Here variable is re-declared inside block , so Local version is used and 30 will be
printed.
Rules For Constructing Variable Name
1. Characters Allowed :
o Underscore(_)
o Capital Letters ( A Z )
o Small Letters ( a z )
o Digits ( 0 9 )
2. Blanks & Commas are not allowed
3. No Special Symbols other than underscore(_) are allowed
4. First Character should be alphabet or Underscore
5. Variable name Should not be Reserved Word
Explanation with Example
Tip 1 : Use allowed Characters
Valid Names
num
Num
Num1
_NUM
NUM_temp2
Tip 2 : blanks are not allowed
Invalid Names
number 1
num 1
addition of program
Tip 3 : No special symbols other that underscore
Valid Identifier
num_1
number_of_values
status_flag
Tip 4 : First Character must be underscore or Alphabet
Valid Identifier
_num1
Num
Num_
_
__
Invalid Identifier
1num
1_num
365_days
Tip 5 : Reserve words are not allowed
C is case sensitive.
Variable name should not be Reserve word.
However if we capitalize any Letter from Reserve word then it will become legal variable
name.
Valid Identifier
iNt
Char
Continue
CONTINUE
Invalid Identifier
int
char
continue
Tip 6 : Name of Identifier cannot be global identifier
Basic Note :
Global predefined macro starts with underscore. (_) .
We cannot use Global predefined macro as our function name.
Example : Some of the predefined macros in C
__TIME__
__DATE__
__FILE__
__LINE__
Valid Identifier
__NAME__
__SUM__
Invalid Identifier
__TIME__
__DATE__
__FILE__
Tip 7 : Name of identifier cannot be register Pseudo variables
Invalid Example :
#include<stdio.h>
int main(){
long int _AH = 15;
printf("%ld",_AH);
return 0;
}
Tip 8 : Name of identifier cannot be exactly same as of name of another identifier within
the scope of the function
Valid Example :
#include<stdio.h>
int main(){
int ivar = 15;
{
int ivar = 20;
printf("%d",ivar);
}
return 0;
}
Invalid Example : We cannot declare same variable twice within same scope
#include<stdio.h>
int main(){
int ivar = 15;
int ivar = 20;
printf("%d",ivar);
return 0;
}
Tip 9 : Constants
1. We know that M_PI constant is declared inside math.h header file.
2. Suppose in our program we have declared variable M_PI and we have not included math.h
header file then it is legal variable.
Legal Example :
#include<stdio.h>
int main(){
int M_PI=25;
printf("%d",M_PI);
return 0;
}
Output :
25
Illegal Example :
#include<stdio.h>
#include<math.h>

int main(){
int M_PI=25;
printf("%d",M_PI);
return 0;
}
Output :
Compile error

Remember following Tricks
1. Do not Create unnecessarily long variable name
2. Do not use underscore as first character to avoid confusion between System Variable & user
defined variables because many system variables starts with undescore
3. Variable names are case-Sensitive . i.e sum,Sum,SUM these all three are different variable
names.
4. Reserve words with one/more Capital letters allowed eg. Int,Float,chAr are allowed but try
to skip them.
What is Constant ?
Constant in C means the content whose value does not change at the time of execution of a
program.
Definition :
Constant means Whose value cannot be changed

Explanation :
Initially 5 is Stored in memory location and name x is given to it
After We are assigning the new value (3) to the same memory location
This would Overwrite the earlier value 5 since memory location can hold only one value at a
time
The value of 3,5 do not change ,so they are constants
In Short the Values of Constant Does not Change.
Different Types of C Constants :
Constant Type of Value Stored
Integer Constant Constant which stores integer value
Floating Constant Constant which stores float value
Character Constant Constant which stores character value
String Constant Constant which stores string value
How to Declare Constant in C :
We can declare constant using const variable. Suppose we need to declare constant of type
integer then we can have following two ways to declare it -
const int a = 1;
int const a = 1;
above declaration is bit confusing but no need to worry, We can start reading these variables
from right to left. i.e
Declaration Explanation
const int a = 1;
read as a is an integer which is constant
int const a = 1;
read as a is a constant integer


C Character Constant
Previous Page Next Page
Single Character Constant :
1. Character Constant Can hold Single character at a time.
2. Contains Single Character Closed within a pair of Single Quote Marks
3. Single Character is smallest Character Data Type in C.
4. Integer Representation : Character Constant have Integer Value known as ASCII value
5. It is Possible to Perform Arithmetic Operations on Character Constants
Examples of character Type :
1. a
2. 1
3. #
4. <
5. X
How to Declare Character Variable ?
Way 1: Declaring Single Vaiable
char variable_name;
Way 2: Declaring Multiple Vaiables
char var1,var2,var3;
Way 3: Declaring & Initializing
char var1 = 'A',var2,var3;
Format Specifier for Character Variable :
%c is used as format specifier for character inside C.
However we can also use %d as format specifier because Each Character have its
equivalent interger value known as ASCII Value.
Using Format Specifier to Print Character Variable :
Sample 1:
printf("%d",'a'); //Output : 97
Sample 2:
printf("%c",'97'); //Output : a
These two Represent the same Result
Complete Example of Character Variable :
Example 1 : Creating Variable and Displaying Character Data
#include<stdio.h>

int main()
{
char cvar = 'A';

printf("Character is : %c",cvar);

return(0);
}
Output :
Character : A
Example 2 : Accepting Character Data
#include<stdio.h>

int main()
{
char cvar;

printf("Enter chracter :");
scanf("%c",cvar);

printf("Character is : %c",cvar);

return(0);
}


String Constant in C Programming Language :
1. String is Sequence of Characters.
2. String Constant is written in Pair of Double Quotes.
3. String is declared as Array of Characters.
4. In C , String data type is not available.
5. Single Character String Does not have Equivalent Integer Value i.e ASCII Value
Different Constant Values Contained inside String :
String with Single Character
"a"
String With Multiple Characters
"Ali"
String With Digits
"123"
String With Blanks
"How are You"
Note :
1] 'A' is not Equal to "A"
2] 'A' ==> Requires 1 byte Memory
3] "A" ==> Requires 2 byte Memory
Summary :
Example Meaning
a String with Single Character
Ali String With Multiple Characters
123? String With Digits
How are You String With Blanks


Special Backslash Character Constants in C :
Constant Meaning
a Audible Alert (Bell)
b Back Space
f Form Feed
n New Line
r Carriage Return
t Horizontal Tab
v Vertical Tab
Single Quote
Double Quote
? Question Mark
\ Backslash
\0 Null
How many Spaces Makes One Tab Space :
1. Generally 8 Spaces makes one Tab in Borland CC++ 3.0 Compiler
2. It differs from Compiler to Compiler And also different for different Word Processors

Backslash Characters : Properties
You need to learn What is Backslash Character in C Programming.
1. Although it consists of two characters, it represents single character.
2. Each escape sequence has unique ASCII value.
3. Each and Every combination starts with back slash()
4. They are non-printable characters.
5. It can also be expressed in terms of octal digits or hexadecimal sequence.
6. Escape sequence in character constants and string literals are replaced by their equivalent
and then adjacent string literals are concatenated
7. Escape Sequences are preprocessed by Preprocessor.
1. Tab : \t Character
It is Horizontal Tab
Takes Control 8 spaces ahead in Borland CC++ 3.0 Compiler
#include<stdio.h>

int main()
{
printf("Hello\t");
return(0);
}
Cursor Position After Execution of Printf :
Hello _
2. New Line Character : \n Character
It is New Line Character
Takes Control to new Line
#include<stdio.h>

int main()
{
printf("Hello\n");
return(0);
}
Cursor Position After Printf :
Hello
_
3. Backslash : \b Character
It is Backslash Character
Takes Control one position back
#include<stdio.h>

int main()
{
printf("Hello\b");
return(0);
}
Cursor Position :
Hell_
Note : on o character from Word Hello
4. Carriage Return : \r Character
It is Carriage Return Character
Takes Control to First Position in the Line
printf("Hello\r");
Cursor Position :
Note : Cursor on H character from Word Hello
_allo
4. Audible Return : \a Character
It is audible Return Character
Beeps Sound
printf("Hello\a");
What will happen ?
Hello
After hello System Sound Beep will be started.

C Character Set :
Whenever we write any C program then it consists of different statements. Each C Program is
set of statements and each statement is set of different c programming lexims. In C
Programming each and every character is considered as single lexim. i.e [ Basic Lexical
Element ]
Character Set Consists Of -
Types Character Set
Lowercase Letters a-z
Uppercase Letters A to Z
Digits 0-9
Special Characters !@#$%^&*
White Spaces Tab Or New line Or Space
Valid C Characters : Special Characters are listed below -
Symbol Meaning
~ Tilde
! Exclamation mark
# Number sign
$ Dollar sign
% Percent sign
^ Caret
& Ampersand
* Asterisk
( Left parenthesis
) Right parenthesis
_ Underscore
+ Plus sign
| Vertical bar
\ Backslash
` Apostrophe
- Minus sign
= Equal to sign
{ Left brace
} Right brace
[ Left bracket
] Right bracket
: Colon
Quotation mark
; Semicolon
< Opening angle bracket
> Closing angle bracket
? Question mark
, Comma
. Period
/ Slash


Keywords in C Programming Language :
1. Keywords are those words whose meaning is already defined by Compiler
2. Cannot be used as Variable Name
3. There are 32 Keywords in C
4. C Keywords are also called as Reserved words .
32 Keywords in C Programming Language
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Simple Tip :
We cannot Use Keywords for For Declaring Variable Name,For Function Name and for declaring
Constant Variable

Vous aimerez peut-être aussi