0% found this document useful (0 votes)
120 views

Lecture 5

The document discusses the key elements of the C programming language including character sets, identifiers, keywords, constants, variables, and data types. Identifiers are names given to program elements like variables and functions, and must begin with a letter. Keywords are reserved words in C that cannot be used as identifiers. There are different types of constants including integer, floating point, character, and string constants. Variables represent data within a program and can take on different values.

Uploaded by

Rocky Ishu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Lecture 5

The document discusses the key elements of the C programming language including character sets, identifiers, keywords, constants, variables, and data types. Identifiers are names given to program elements like variables and functions, and must begin with a letter. Keywords are reserved words in C that cannot be used as identifiers. There are different types of constants including integer, floating point, character, and string constants. Variables represent data within a program and can take on different values.

Uploaded by

Rocky Ishu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

DECLARATIONS

Character Set, Keywords, Identifiers, Constants, Variables

Designed by Parul Khurana, LIECA.

Character Set
C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9.

C uses certain Special Characters.


Designed by Parul Khurana, LIECA.

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.

Valid Invalid Identifiers


The following names are valid Identifiers:
x names y12 area sum_1 tax_rate _tempera GOTO

The following names are Invalid Identifiers:


1th x The first character must be a letter. Illegal Character ( ).

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).

Designed by Parul Khurana, LIECA.

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.

Designed by Parul Khurana, LIECA.

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

Designed by Parul Khurana, LIECA.

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.

Designed by Parul Khurana, LIECA.

Rules for Numeric Type Constants


Commas and blank spaces cannot be included within the constant. The constant can be preceded by minus (-) sign, if desired. The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant these bounds will vary from one C compiler to another.
Designed by Parul Khurana, LIECA.

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).

Designed by Parul Khurana, LIECA.

Decimal Integer Constant


A decimal integer constant can consist of any combination of digits taken from the set 0 through 9. if the constant contains two or more digits, the first digit must be something other than 0.

Designed by Parul Khurana, LIECA.

Valid Invalid Integer Constants


Several valid decimal integer constants are shown below:
0 1 743 5280 32767 9999

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

Illegal Character ( blank space ).


Illegal Character ( - ). the first digit cannot be zero
Designed by Parul Khurana, LIECA.

Octal Integer Constant


An octal integer constant can consist of any combination of digits taken from the set 0 through 7.

However the first digit must be 0, in order to identify the constant as an octal number.

Designed by Parul Khurana, LIECA.

Valid Invalid Integer Constants


Several valid octal integer constants are shown below:
0 01 0743 077777

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 ( . ).

Designed by Parul Khurana, LIECA.

Hexadecimal Integer Constant


A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from 0 to 9 and a through f ( either upper or lowercase). Note that the letters a through f ( or A through F) represent the (decimal) quantities 10 through 15, respectively.
Designed by Parul Khurana, LIECA.

Valid Invalid Integer Constants


Several valid hexadecimal integer constants are shown below:
0x 0X1 0X7FFF 0xabcd

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 ).

Designed by Parul Khurana, LIECA.

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.

Valid Invalid Floating-point Constants


Several valid floating-point constants are shown below:
0. 50000. 1. 0.000743 0.2 12.3 827.602 315.0066

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.

Valid Character Constants


Several valid character constants are shown below:
A x 3 Blank space

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.

Designed by Parul Khurana, LIECA.

Valid String Constants


Several valid string constants are shown below:
green Washington, D.C. 20005 270-32-3456

$19.95

THE CORRECT ANSWER IS:

2*(I+3)/J

Line 1\n Line 2\n Line 3

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.

Designed by Parul Khurana, LIECA.

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

newline ( line feed ) \n

apostrophe
question mark (? backslash

\
\? \\

039
063 092 000

Designed by Parul Khurana, LIECA. null \0

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

Give the alternative declaration of following symbolic constants:


# define CSE101 C const int CSE = 101 const char city =J; int const city =N;
Designed by Parul Khurana, LIECA.

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);

Designed by Parul Khurana, LIECA.

You might also like