Constants and Variables-C Programming


C Programming Language – Constants and Variables

In this tutorial you will learn about Character Set, C Character-Set Table, Special Characters, White Space, Keywords and Identifiers, Constants, Integer Constants, Decimal Integers, Octal Integers, Hexadecimal integer, Real Constants, Single Character Constants, String Constants, Backslash Character Constants [Escape Sequences] and Variables.

 Instructions in C language are formed using syntax and keywords. It is necessary to strictly follow C language Syntax rules. Any instructions that mis-matches with C language Syntax generates an error while compiling the program. All programs must confirm to rules pre-defined in C Language. Keywords as special words which are exclusively used by C language, each keyword has its own meaning and relevance hence, Keywords should not be used either as Variable or Constant names.

 

Character Set

The character set in C Language can be grouped into the following categories.

1. Letters
2. Digits
3. Special Characters
4. White Spaces

White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers.

 

C Character-Set Table

Letters

Digits

Upper Case A to Z

0 to 9

Lower Case a to z

.

 

Special Characters

 

,

.Comma

&

.Ampersand

.

 .Period

^

.Caret

;

 .Semicolon

*

.Asterisk

:

.Colon

.Minus Sign

?

.Question Mark

+

.Plus Sign

.Aphostrophe

<

.Opening Angle (Less than sign)

.Quotation Marks

>

.Closing Angle (Greater than sign)

!

.Exclaimation Mark

(

.Left Parenthesis

|

.Vertical Bar

)

.Right Parenthesis

/

.Slash

[

.Left Bracket

\

.Backslash

]

.Right Bracket

~

.Tilde

{

.Left Brace

.Underscore

}

.Right Bracket

$

.Dollar Sign

#

.Number Sign

%

.Percentage Sign . .


 


 


 


 


 


 


 


 


 


 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

.
.
White Space

1. Blank Space
2. Horizontal Tab
3. Carriage Return
4. New Line
5. Form Feed


 

 

 

 

 

 

Keywords and Identifiers

Every word in C language is a keyword or an identifier. Keywords in C language cannot be used as a variable name. They are specifically used by the compiler for its own purpose and they serve as building blocks of a c program.


The following are the Keyword set of C language.


.auto .else .register .union
.break .enum .return .unsigned
.case .extern .short .void
.char .float .signed .volatile
.const .for .size of .while
.continue .goto .static .
.default .if .struct .
.do .int .switch .
.double .long .typedef .


some compilers may have additional keywords listed in C manual.

Identifiers refers to the name of user-defined variables, array and functions. A variable should be essentially a sequence of letters and or digits and the variable name should begin with a character.

Both uppercase and lowercase letters are permitted. The underscore character is also permitted in identifiers

The identifiers must conform to the following rules.

1. First character must be an alphabet (or underscore)
2. Identifier names must consists of only letters, digits and underscore.
3. A identifier name should have less than 31 characters.
4. Any standard C language keyword cannot be used as a variable name.
5. A identifier should not contain a space.

 

Constants

A constant value is the one which does not change during the execution of a program. C supports several types of constants.

1. Integer Constants
2. Real Constants
3. Single Character Constants
4. String Constants

Integer Constants

An integer constant is a sequence of digits. There are 3 types of integers namely cimal integer, octal integers and hexadecimal integer.

Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or – sign. Spaces, commas and non digit characters are not permitted between digits. Example for valid decimal integer constants are

123
-31
0
562321
+ 78

Some examples for invalid integer constants are

15 750
20,000
Rs. 1000

Octal Integers constant consists of any combination of digits from 0 through 7 with a O at the beginning. Some examples of octal integers are

O26
O
O347
O676

Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are

OX2
OX8C
OXbcd
Ox

Real Constants

Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Example of real constants are

0.0026
-0.97
435.29
+487.0

Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign

Single Character Constants

A Single Character constant represent a single character which is enclosed in a pair of quotation symbols.xample for character constants are
‘5’
‘x’
‘;’
‘ ‘

All character constants have an equivalent integer value which are called ASCII Values.

 

String Constants

A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are


“VISHAL”
“1234”
“God Bless”
“!…..?”

 

Backslash Character Constants [Escape Sequences]

Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings.

 

Constant

Meaning

\a’

.Audible Alert (Bell)

‘\b’

.Backspace

 

‘\f’

.Formfeed

‘\n’

.New Line

‘\r’

.Carriage Return

‘\t’

.Horizontal tab

‘\v’

.Vertical Tab

‘\”

.Single Quote

‘\”‘

.Double Quote

‘\?’

.Question Mark

‘\\’

.Back Slash

.Null

Variables

A variable is a value that can change any time. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Example of variable names are


Sun
number
Salary
Emp_name
average1


Any variable declared in a program should confirm to the following

1. They must always begin with a letter, although some systems permit underscore as the first character.
2. The length of a variable must not be more than 8 characters.
3. White space is not allowed and
4. A variable should not be a Keyword
5. It should not contain any special characters.

Examples of Invalid Variable names are

123
(area)
6th
%abc

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s