Professional Documents
Culture Documents
Topic
3.1
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
3.5
3.6
3.7
Keywords in C
3.8
3.9
3.10
3.11
Expression in C
C Tokens Chart
In
Programming punctuation,individual
words,characters etc
called tokens.
Token Example :
No
Token Type
Example 1
Example 2
Keyword
do
while
Constants
number
sum
Identifier
-76
89
String
HTF
PRIT
Special Symbol
Operators
++
are
Token
Meaning
Keyword
Constant
Identifier
String
Sequence of characters
Special
Symbol
Operators
Conclusion
We have learnt different type of tokens in C. In the upcoming chapters we will learn C
tokens, keywords and identifiers in more details.
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
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
Lowercase Letters
a-z
Uppercase Letters
A to Z
Digits
0-9
Special Characters
!@#$%^&*
White Spaces
Meaning
Tilde
Exclamation
Number
sign
Dollar
sign
Percent
sign
mark
Caret
&
Ampersand
Asterisk
Left
parenthesis
Right
parenthesis
Underscore
Plus
Vertical bar
\
Apostrophe
Minus sign
Equal to sign
Backslash
Left brace
Right brace
Left bracket
Right bracket
Colon
Quotation mark
sign
Semicolon
<
>
Question mark
Comma
Period
Slash
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
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.
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 beconsidered
as garbage..
number 1
num 1
addition of program
Invalid Identifier
1num
1_num
365_days
C is case sensitive.
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
__TIME__
__DATE__
__FILE__
__LINE__
Valid Identifier
__NAME__
__SUM__
Invalid Identifier
__TIME__
__DATE__
__FILE__
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
1.Name of Variable
Compiler wont understand this name , This is only for human understanding.
Variable name maps into Address inside and then by considering mapped
address compiler processes data.
Depending on the type of Variable we can store any value of appropriate data
type inside Variable.
Suppose we have Integer Variable then Value inside variable will be of type
Integer.
3.Address of Variable
4.Type of Variable
Type of variable tells compiler that Allocate memory for data of Specified
type.
5.Size of Variable
Variable attributes
Variable Name
ivar
Variable Type
Integer
Variable Address
2000
Variable Size
2 Bytes
Variable Value
34
What is Declaration ?
cannot
use
variable
inside
program
expression
or
in
program
Example Of Declaration :
int ivar;
float fvar;
char cvar;
main()
{
int res = sum(10,20);
}
Declaring Variable
Whenever we write declaration statement then memory will not be allocated for the
variable. Variable declaration will randomly specify the memory location.
int ivar;
float fvar;
In the above declaration memory is not allocated. Whenever we define a variable then
memory will be allocated for the variable.
struct book b1;
B. What it does ?
C. Re-Declaration Vs Re-Definition
In Both the cases we will get compile error. Re-declaration and Re-definition is illegal in
C programming language.
&
struct book {
int pages;
float price;
char *bname;
};
struct book {
int pages;
float price;
char *bname;
};
{
return(n1+n2);
}
Declaration
Definition
Re-Declaration is Error
Re-Definition is Error
No
Topic
What is Constants ?
Types of Constants
String Constant
Escape Sequence
Backslash Characters
What is Constant ?
Constant in C means the content whose value does not change at the time of execution
of a program.
Definition :
[box]Constant means Whose value cannot be changed[/box]
Explanation :
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
Integer Constant
Constant
Floating Constant
Character Constant
String Constant
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;
int const a = 1;
Primary Constant :
Integer
Float
Character
Secondary Constant :
Array
Pointer
Structure
Union
Enum
Rule
Integer
Float
Decimal Point
Not Allowed
Allowed
Sign
Positive or Negative
Positive or Negative
Default Sign
Positive
Positive
No of Digits
At least 1
At least 1
Min Value
-32768
-3.4e38
Max Value
+32767
3.4e38
mantissa e exponent
Part
Type
Sign
Mantissa
Exponent
Power of 10
Note :
1. Exponent Part Should be Always Integer.
2. Default Sign is Positive for both Mantissa as well as Exponent
3. Special Characters not allowed
4. Spaces are not allowed
Illustrative Examples :
Real Number
Representation
4567.45
4.56745e3
0.00045
4.5e-4
Consider Example 2:
1. Mantissa Should be in between 1 to 9
2. Number : 0.00045 so shift decimal Point to Right by 4 Digit to obtain Mantissa as
4.5
3. Decimal Point is Shifted 4 times to Right
4. According to above Fig . Real Number : 4.5 e -4
Rule
Integer
Decimal
Sign
Positive or Negative
Default Sign
Positive
No of Digits
At least 1
Min Value
-32768
Max Value
+32767
Operations
can
be
performed
on
Integer
Value
such
as
integer_graph1-blue-yellow
However we can also use %d as format specifier because Each Character have
its equivalent interger value known as ASCII Value.
printf("%d",'a'); //Output : 97
Sample 2:
printf("%c",'97'); //Output : a
int main()
{
char cvar = 'A';
printf("Character is : %c",cvar);
return(0);
}
Output :
Character : A
int main()
{
char cvar;
printf("Character is : %c",cvar);
return(0);
}
Note :
1] 'A' is not Equal to "A"
2] 'A' ==> Requires 1 byte Memory
3] "A" ==> Requires 2 byte Memory
Summary :
Example
Meaning
Ali
123?
Meaning
Back Space
Form Feed
New Line
Carriage Return
Horizontal Tab
Vertical Tab
Single Quote
Double Quote
Question Mark
Backslash
\0
Null
1. Tab : \t Character
It is Horizontal Tab
#include<stdio.h>
int main()
{
printf("Hello\t");
return(0);
}
#include<stdio.h>
int main()
{
printf("Hello\n");
return(0);
}
Hello
_
3. Backslash : \b Character
It is Backslash Character
#include<stdio.h>
int main()
{
printf("Hello\b");
return(0);
}
Cursor Position :
Hell_
printf("Hello\r");
Cursor Position :
_allo
Beeps Sound
printf("Hello\a");