Lecture 5
Lecture 5
Character Set
C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9.
Character Set
The special characters are listed below:+ * / = % & #
! <
:
? >
;
^ (
.
)
,
[
_
~ ]
\ {
| }
WHITE SPACE
Most versions of the language also allow certain other characters, such as @ and $, to be included within strings and comments.
White space includes new lines, tabs, space..
Designed by Parul Khurana, LIECA.
Identifiers
Identifiers are the names that are given to various program elements, such as variables, functions and arrays.
Identifiers consist of letters and digits, in any order, except that the first character must be a letter. Both UPPER and lowercase letters are permitted.
Designed by Parul Khurana, LIECA.
Identifiers
Note: Upper case and lowercase letters are not interchangeable ( i.e., an uppercase letter is not equivalent to the corresponding lowercase letter.) The underscore ( _ ) character can also be included, and is considered to be a letter. An underscore is often used in the middle of an identifier. An identifier may also begin with an underscore.
Designed by Parul Khurana, LIECA.
rder-no
rror flag goto
Illegal Character ( - ).
Illegal Character (blank space). Reserved word (keyword).
Designed by Parul Khurana, LIECA.
Important - Identifiers
Note: As identifier can be arbitrarily long. Some implementations of C recognize only the first eight characters, though most implementations recognize more ( typically 31 characters).
Example - Identifiers
The identifier file_manager and file_management are both grammatically valid. Some compilers may be unable to distinguish between them, however, because the first eight characters are same for each identifier. Therefore, only one of these identifiers should be used in a single C program.
Designed by Parul Khurana, LIECA.
Keywords
There are certain reserved words, called keywords, that have standard, predefined meanings in C.
These keywords can be used only for their intended purpose, they cannot be used as programmer defined identifiers.
Standard Keywords
The standard keywords are:auto extern sizeof
break case
char const continue default do double else enum
float for
goto if int long register return short signed
static struct
switch typedef union unsigned void volatile while
Standard Keywords
Some compilers may also include some or all of the following keywords:
ada fortran Entry far asm huge near pascal
Note that the keywords are all lowercase. Since uppercase and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier.
Designed by Parul Khurana, LIECA.
Constants
There are four basic types of constants in C. They are:
Integer constants. Floating-point constants. Character constants. String constants. Symbolic constants. Non symbolic constants are also known as literals.
Designed by Parul Khurana, LIECA.
Constants
Moreover, there are several different kinds of integer and floating-point constants, as discussed below:
Integer and Floating-point constants represent numbers. They are often referred to collectively as numeric type constants.
Integer Constants
An integer constant is an integer-valued constant. Thus, it consists of a sequence of digits.
Integer constants can be written in three different number systems: decimal(base 10), octal(base 8) and hexadecimal(base 16).
The following decimal integer constants are written incorrectly for the reasons stated.
12,245 36.0 Illegal Character ( , ). Illegal Character ( . ).
10 20 30
123-45-6789 0900
However the first digit must be 0, in order to identify the constant as an octal number.
The following octal integer constants are written incorrectly for the reasons stated.
743 05280 Does not begin with 0. Illegal digit ( 8 ).
0777.777
Illegal character ( . ).
The following hexadecimal integer constants are written incorrectly for the reasons stated.
0X 12.34 0BE38 Illegal character ( . ). Does not begin with 0x or 0X.
0x.4bff
0XDEFG
Illegal character ( . ).
Illegal character ( G ).
Floating-Point Constants
A floating-point constant is a base-10 number either a decimal point or an exponent (or both). The interpretation of a floating-point constant with an exponent is essentially the same as scientific notation, except that the base 10 is replaced by the letter E or e. Thus, the number 1.210-3 would be written as 1.2E-3 or 1.2e-3. This is equivalent to 0.12e-2,or 12e4, etc.
Designed by Parul Khurana, LIECA.
2E-8
0.006e-3
1.6667E+8
.12121212e12
The following are not valid floating-point constants for the reasons stated.
1 1,000.0 2E+10.2 3E 10 Either a decimal point or an exponent must be present. Illegal character ( , ). The exponent must be an Integer quantity. Illegal character ( blank space) in the exponent.
Designed by Parul Khurana, LIECA.
Character Constants
A character constant is a single character, enclosed in apostrophes (i.e., single quotation marks). Character constants have integer values that are determined by the computers particular character set. Most computers, and virtually all personal computers, make use of ASCII (i.e., American Standard Code for Information Interchange) character set.
Designed by Parul Khurana, LIECA.
Several character constants and their corresponding values, as defined by the ASCII character set, are shown below:
Constant A x 3 Value 65 120 51
Designed by Parul Khurana, LIECA.
Constant ?
Value 63 32
String Constants
A string constant consists of any number of consecutive characters ( including none), enclosed in (double) quotation marks.
$19.95
2*(I+3)/J
Note that the string constant Line 1 \n Line 2\n Line 3extends over three lines because of newline characters that are embedded LIECA. within the string. Designed by Parul Khurana,
Symbolic Constants
A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a numeric constant, a character constant or a string constant. Thus a symbolic constant allows a name to appear in place of a numeric constant, a character constant or a string. When a program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence.
Designed by Parul Khurana, LIECA.
Symbolic Constants
Symbolic constants are usually defined at the beginning of a program. A symbolic constant is defined by writing # define name text Or const data-type name = value;
where name represents a symbolic name, typically written in uppercase letters, and text represents the sequence of characters that is associated with the symbolic name. Designed by Parul Khurana, LIECA.
Variables
A variable is an identifier that is used to represent some specified type of information within a designated portion of the program.
In its simplest form, a variable is an identifier that is used to represent a single data item i.e., a numerical quantity or character constant.
Designed by Parul Khurana, LIECA.
Variables
A given variable can be assigned different data items at various places within the program. Thus, the information represented by the variable can change during the execution of the program.
However, the data type associated with the variable cannot change.
Designed by Parul Khurana, LIECA.
Escape Sequences
Certain non printing characters, as well as the backlash ( \ ) and the apostrophe ( ), can be expressed in the terms of escape sequences or backslash character constants.
Escape Sequences
An escape sequence always begin with a backward slash and is followed by one or more special characters. For example, a line feed (LF), which is referred to as a newline in C can be represented as \n. Such escape sequences always represent single characters, even though they are written in terms of two or more characters.
Designed by Parul Khurana, LIECA.
Escape Sequences
The following are the commonly used escape Escape Sequence ASCII Value sequences: Character
bell (alert) backspace horizontal tab vertical tab form feed carriage return quotation mark () \a \b \t \v \f \r \ 007 008 009 011 010 012 013 034
apostrophe
question mark (? backslash
\
\? \\
039
063 092 000
Practice Questions
List 10 valid and 10 invalid identifiers other than those mentioned in your textbook and this presentation. Separate the valid decimal, octal and hexadecimal literals from the following: 00.5, 0.0.5, 0.8E+0.8, 786, 13B, 0xLPU, 0x87e3fa
Practice Questions
Comment on the following code:
#define int 100 and how does this declaration behave for the following: int x;
int;
printf( %d, x);