You are on page 1of 39

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

Programming punctuation,individual

words,characters etc

called tokens.

Tokens are basic building blocks of C Programming

Token Example :
No

Token Type

Example 1

Example 2

Keyword

do

while

Constants

number

sum

Identifier

-76

89

String

HTF

PRIT

Special Symbol

Operators

++

Basic Building Blocks and Definition :

are

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

Special
Symbol

Symbols other than the Alphabets and Digits and white-spaces

Operators

A symbol that represent a specific mathematical or non mathematical


action

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.

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

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

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

<

Opening angle bracket

>

Closing angle bracket

Question mark

Comma

Period

Slash

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 Programmingwhich is also called as identifier.
[box]A Variable is a name given to the memory location where the actual data is stored.
[/box]

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 beconsidered
as garbage..

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.

Fundamental Attributes of C Variable :


1. Name of a Variable
2. Value Inside Variable
3. Address of Variable
4. Size of a Variable
5. Type of a Variable

1.Name of Variable

We can Give proper name to any Variable which is human readable.

Compiler wont understand this name , This is only for human understanding.

Variable name is only the name given to the Memory address.

Variable name maps into Address inside and then by considering mapped
address compiler processes data.

We have declared variable means We have created one empty container


which will hold data.

2.Value inside Variable

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.

Simple Variable can only hold one value at a time.

3.Address of Variable

Variable can hold data ,it means there should be a container.

So container must have Starting Address.

Address of variable = Starting Address of Memory where Memory is allocated to


Variable.

4.Type of Variable

While declaring a variable we have to specify type of variable.

Type of variable tells compiler that Allocate memory for data of Specified
type.

If we declare variable of type Integer then compiler will allocate memory


container of size 2 bytes and container will be able to store integer data only.

5.Size of Variable

We can use sizeof operator to calculate size of any data type.

Variable attributes

Variable Name

ivar

Variable Type

Integer

Variable Address

2000

Variable Size

2 Bytes

Variable Value

34

What is Declaration ?

To Declare is nothing but to represent a variable.

Only Variable name and its Data type is Represented in Declaration.

Facts about Declaration :


1. Memory is neither allocated to Variable nor space is Reserved after
declaration.
2. Declaration is just used for Identification of Data Type.
3. Re-Declaration of variable will cause compile Error.
int ivar;
int ivar = 10;

It will cause compile error. We cannot re-declare variable.


4. We

cannot

use

variable

inside

program

expression

or

in

program

statements without declaring.


5. Declaring a variable just give rough idea to compiler that there is something
which is of type <Data Type Specified> and will be used in the program.

Example Of Declaration :
int ivar;
float fvar;
char cvar;

Variable Can be categorized depending on Type of Data it


stores inside
1. Variable Storing Integer Data Type is called as Integer Variable.
2. Variable Storing Character Data Type is called as Character Variable.
3. Variable Storing Float Data Type is called as Float Variable.
4. Variable Storing Double Data Type is called as Double Variable.

Variables can be categorized depending on Number of


Variables it can store
1. Variable storing single value at a time of any data type is called as Normal or
Ordinary Variable.
2. Variable Storing multiple values of same data type is called as Array.

3. Variable Storing multiple values of different data type is called as Structure


[ User defined data type]

Some Examples of Declaration :


Declaration of Function :
Whenever we write function after the main function, compiler will through the error
since it does not have any idea about the function at the time of calling function. If we
provide prototype declaration of function then we compiler will not look for the
definition.
int sum(int,int);

main()
{
int res = sum(10,20);
}

int sum(int n1,int n2)


{
return(n1+n2);
}

In the above example , first line is called as function declaration.


int sum(int,int);

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;

Variable Declaration Vs Definition Differentiation Parameters


A. Space Reservation :
1. Whenever we declare a variable then space will not be reserved for the variable.
2. Whenever we declare a variable then compiler will not look for other details
such as definition of the variable.
3. Declaration is handy way to write code in which actual memory is not allocated.
struct book {
int pages;
float price;
char *bname;
};

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 ?

1. Declaration will identify the data type of the identifier.


2. Definition of the variable will assign some value to it.

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.

Re-Declaring the Variable in Same Scope :


main()
{
int num1;
int num1;
}

&
struct book {
int pages;
float price;
char *bname;
};

struct book {
int pages;
float price;
char *bname;
};

above statement will cause error.

Re-Defining the Variable :


int sum(int n1,int n2)

{
return(n1+n2);
}

int sum(int n1,int n2)


{
return(n1+n2);
}

above definition will cause compile error.

Difference between Declaration and Definition


No

Declaration

Definition

Space is Not Reserved

In Definition Space is Reserved

Identifies Data Type

Some Initial Value is Assigned

Re-Declaration is Error

Re-Definition is Error

No

Topic

What is Constants ?

Types of Constants

Integer Number Constant

Single Character Constant

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 :

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

Constant

Type of Value Stored

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

Primary Constant :

Integer

Float

Character

Secondary Constant :

Array

Pointer

Structure

Union

Enum

Rules of Constructing Integer and Float Constant


Sr.N
o

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

Format of Real Number Constant :


Syntax :

mantissa e exponent

Part

Type

Sign

Mantissa

Integer Part of Real Number

Has Optional +/- Sign

Exponent

Power of 10

Has Optional +/- Sign

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

Rules for constructing Integer constant


No

Rule

Integer

Decimal

Point Not Allowed

Sign

Positive or Negative

Default Sign

Positive

No of Digits

At least 1

Min Value

-32768

Max Value

+32767

Some important Tips :


1. The range of the integer constant depends on the compiler. The above
mentioned range is for 16-bit compiler.
2. Integer Value does not have decimal point.
3. Integer Value may be positive or negative. even 0 is also considered as Integer.
4. Integer Constant Value is assigned to integer variable.
5. Arithmetic

Operations

can

be

performed

addition,subtraction,multiplication and division.


6. 2 bytes in memory is allocated for Integer Value

on

Integer

Value

such

as

integer_graph1-blue-yellow

Examples of integer constants :


1. Whole Numbers
2. Real Numbers i.e 10,-10,0

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

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

Audible Alert (Bell)

Back Space

Form Feed

New Line

Carriage Return

Horizontal Tab

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 differentWord
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 hexadecimalsequence.
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_

[box]Note : on o character from Word Hello[/box]

4. Carriage Return : \r Character

It is Carriage Return Character

Takes Control to First Position in the Line

printf("Hello\r");

Cursor Position :

[box]Note : Cursor on H character from Word Hello[/box]

_allo

4. Audible Return : \a Character

It is audible Return Character

Beeps Sound

printf("Hello\a");

What will happen ?


Hello

You might also like