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

Unit 2: Elements of C: Ramesh Rimal

The document discusses various elements of the C programming language including character sets, comments, tokens, keywords, identifiers, variables, data types, and more. It provides definitions and examples of each element and explains the basic building blocks used in C programs.

Uploaded by

samin1213
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Unit 2: Elements of C: Ramesh Rimal

The document discusses various elements of the C programming language including character sets, comments, tokens, keywords, identifiers, variables, data types, and more. It provides definitions and examples of each element and explains the basic building blocks used in C programs.

Uploaded by

samin1213
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

UNIT 2: ELEMENTS OF C

Prepared By
Ramesh Rimal

6/21/20 11:52:26 AM 1
CHARACTER SET
 A character denotes any alphabet, digit,
white space or special symbol used to
represent information.
 The character set in C language can be
grouped into the following categories.
Letters: uppercase A to Z
lowercase a to z
Digits: 0 to 9
White spaces: Blank space, Horizontal
tab, Carriage return, New
line & Form feed
Special characters: listed in the table

6/21/20 11:52:26 AM 2
Symbol Name of Symbol Symbol Name of Symbol

, Comma & Ampersand

. Period ^ Caret

; Semicolon * Asterisk

: Colon - Minus Sign

? Question Mark + Plus Sign

' Apostrophe < Opening Angle (Less than sign)

" Quotation Marks > Closing Angle (Greater than sign)

! Exclamation Mark ( Left Parenthesis

| Vertical Bar ) Right Parenthesis

/ Slash [ Left Bracket

\ Backslash ] Right Bracket

~ Tilde { Left Brace

- Underscore } Right Brace

$ Dollar Sign # Hash

% Percentage Sign White space

6/21/20 11:52:26 AM 3
COMMENTS/REMARKS
 Comments are non executable program codes
that may appear any where within a program.
 Comments are used to clarify purpose of the
entire program or the purpose of some
statements in the program.
 Comments cannot be nested.
 Comments can be written in two forms:
//Single Line Comments
/*Multi-Line .....................................

.....................................................
.........................Comments*/

6/21/20 11:52:26 AM 4
2.1 C TOKENS
 The most basic element of a C program recognized by
the compiler is a single character or group of
characters called C token. C programs are written
using these tokens and syntax of language.
 A token is an atomic unit of source program, i.e., the
compiler will not break down the token any further.
 There are six types of C tokens which are given below.
1.Keywords
2.Operators
3.Identifiers
4.Constants
5.Strings
6.Special symbols

6/21/20 11:52:26 AM 5
KEYWORDS
 Keywords are reserved words that have standard,
predefined meanings in C.
 Keywords are all lowercase, since uppercase and
lowercase characters are not equivalent.
 Keywords can’t be used as identifiers but It's
possible to utilize an uppercase keyword as an
identifier but it's not a good programming practice.
 There are 32 keywords used in ANSI C.
 Some C compilers may recognize other keywords as
well.
 A compiler named C99 has added 5 more keywords:
_Bool, _Complex, _Imaginary, inline and restrict

6/21/20 11:52:26 AM 6
32 KEYWORDS USED IN ANSI C

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
6/21/20 11:52:26 AM 7
ESCAPE SEQUENCE
 The non printing characters started with
backslash(\) are called escape sequence
constants.
 They are special characters used in output
functions.
 Escape Sequence always represent single
character even though they contain two or
more characters.
 The commonly used escape sequences and
their meanings are listed below.

6/21/20 11:52:26 AM 8
Escape Sequence Meaning ASCII Value

\a Audible alert (Bell) 007

\b Backspace 008

\t Horizontal tab 009

\v New line (Line feed) 010

\n Vertical tab 011

\f Form feed 012

\r Carriage return 013

\” Quotation mark (”) 034

\’ Apostrophe (’) 039

\? Question mark (?) 063

\\ Backslash (\) 092

\0 Null 000
6/21/20 11:52:26 AM 9
ESCAPE SEQUENCE CONTD.
 Escape sequence can also be expressed in terms
of one, two or three octal digits which
represent single character bit patterns.
 The general form of such an escape sequence is
\ooo, where each o represents an octal digit (0
to 7).
 Some versions of C also allow an escape
sequence to be expressed in terms of one or
more hexadecimal digits, preceded by the
letter x.
 The general form of a hexadecimal escape
sequence is \xhh, where each h represents a
hexadecimal digit (0 to 9 and a to f).
6/21/20 11:52:26 AM 10
IDENTIFIERS
 Identifiers are the names given to various
program elements such as variable, structure,
function, array etc.
 Identifiers are not defined in programming
language. They are user defined names and
consist of a sequence of letters, digits and
underscore.
 Underscore is usually used as a link between
two words in long identifiers.
 An identifier can be arbitrarily long. Some C
compilers recognize only first 8 characters but
most C compilers recognize more
(typically31characters).
6/21/20 11:52:26 AM 11
SOME RULES TO DEFINE IDENTIFIERS
 It must consist of only letters, digits and underscore.
 First character must be an alphabet or underscore.
 Any standard C language keyword cannot be used as
identifier.
 It should not contain a space.
 It allows both upper case and lower case characters but
common usage favours the use of lowercase letters for
most types of identifiers.
 Valid Identifiers
A1 B34 FIRST _NAME x_1
 Invalid Identifiers
1A 34AB int void FIRST-NAMEX.1

6/21/20 11:52:26 AM 12
VARIABLE
 A variable is an identifier that is used to
represent a single data item i. e. a numerical
quantity or a character constant with in a
designated portion of the program.
 In other words, a variable is an entity which
may vary during program execution.
 In computer programming, a variable is used
to reserve a location in the computer
memory.
 Variable names are names given to locations
in the computer memory where different
types of can be stored.
6/21/20 11:52:26 AM 13
VARIABLE CONTD.
 The data item must be assigned to the variable at
some point in the program that can then be accessed
later in the program simply by referring to the
variable name.
 In any programming language, the types of variables
that it can support depends on the types of constants
that it can handle.
 Declaration of variables:
The declaration of variables must be done before
they are used in the program. It does two things:
1.It tells the compiler what the variable name is.
2.It specifies what type of data the variable will hold.
Syntax of variable declaration:
data_type v1,v2,v3,……..,vn;
6/21/20 11:52:26 AM 14
DATA TYPES
 Data types refer to formats of data what we use in
programs.
 Data types are used to define a variable before they are
used in a program.
 Data types determine the following:
 Range of the data
 Type of the data stored
 Number of bytes it occupies in memory
 C supports variety of data types, each of which may be
represented differently within the computer’s memory.
 ANSI C supports three classes of data types:
1. Primary(or fundamental)Data Type
2. Secondary(or derived)Data Type
3. User-defined Data Type
6/21/20 11:52:26 AM 15
PRIMARY DATA TYPES

 Fundamental data types that are supported


by all C compilers are called Primary Data
Types.
 Other data types can be derived from
primary data types.
 List of primary data types
 Integer
 Floating Point Value
 Character
 Double
 Void
6/21/20 11:52:26 AM 16
INTEGER
 Integers are whole numbers whose range depend
on machine i.e. for a 16 bit compiler like Turbo
C or Turbo C++ the range is -32768 to 32767. For
a 32-bit compiler like Visual Studio or GCC the
range is -2147483648 to 2147483647.
 ‘int’ is used as keyword for integer data type
i.e. ‘int’ keyword is used to define variables
that hold only whole numbers.
 It’s format specifier is %d.
 C offers a variation of integer data type to
provide integers with different ranges wherever
possible: ‘short int’, ‘int’ and ‘long int’
 short int <= int <= long int

6/21/20 11:52:26 AM 17
INTEGER CONTINUED

Compiler Short int Int Long int

16-bit 2 2 4
(Turbo C/ C++)

32-bit 2 4 4
(Visual Studio, GCC)

6/21/20 11:52:26 AM 18
FLOATING POINT VALUE
 Floating point value represents a real
number with 6 digits precision and is stored
in 32 bits. (on all 16 bit and 32 bit machines)
and also known as single precision number.
 It is declared in C by using keyword ‘float’.
 It’s format specifier is %f and it occupies 4
byte memory.
 It can range from -3.4×1038 to 3.4×1038

6/21/20 11:52:26 AM 19
DOUBLE
 Another data type called Double can be used if
the accuracy provided by a ‘float’ number is
not sufficient.
 It is declared in C by using ‘double’ keyword
and it’s format specifier is %lf.
 It occupies 8 bytes in memory giving a precision
of 14 digits. So, it is also known as double
precision number.
 It has a range from -1.7×10308 to 1.7×10308 .
 To extend the precision further, we may use
‘long double’ which occupies 10 bytes in
memory and it’s format specifier is %Lf and
range is from -1.7×104932 to 1.7×104932 .
6/21/20 11:52:26 AM 20
CHARACTER
 A character refers to any alphabet or digits or
symbol enclosed within single quotes.
 It’s format specifier is %c and keyword is ‘char’
 It’s range is from -128 to 127.
 It occupies only one byte.
 The qualifier signed or unsigned can be
explicitly applied to ‘char’.
 A ‘signed char’ is same as an ordinary ‘char’
and has a range from -128 to 127; whereas, an
‘unsigned char’ has a range from 0 to 255

6/21/20 11:52:26 AM 21
CHARACTER CONTD..
/* A program that illustrates the range of signed
and unsigned character.*/
#include<stdio.h> What output do you
#include<conio.h> expect from this
program??
void main()
{ Possibly, 291 and
the character
char ch=291; corresponding to
clrscr(); that it.

printf(“%d%c",ch,ch); Well, not really.


getch(); Surprised???

}
6/21/20 11:52:26 AM 22
CHARACTER CONTD..
 The reason is that ‘ch’ has been declared as
a ‘char’, and a ‘char’ cannot take a value
bigger than +127.
 When value of ‘ch’ exceeds +127, an
appropriate value from the other side of the
range is picked up and stored in ‘ch’.
 This value in our case happens to be 35,
hence 35 and its corresponding character #,
gets printed out.

6/21/20 11:52:26 AM 23
VOID
 The void type specifies that no value is available.
 It is used in three kinds of situations:
1. Function returns as void
There are various functions in C which do not return value or
you can say they return void. A function with no return value
has the return type as void. For eg: void sum(int, int);
2.Function arguments as void
There are various functions in C which do not accept any
parameter. A function with no parameter can accept as a void.
For eg: int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but
not its type. For example a memory allocation function void
*malloc( size_t size ); returns a pointer to void which can be casted
to any data type.

6/21/20 11:52:26 AM 24
DATA TYPE QUALIFIERS

 The size and range of these data types may


vary among processor types and compilers.
 Data type qualifiers modify the behavior of
variable type to which they are applied.
 Data type qualifiers can be classified into
two types.
1. Size qualifiers
2. Sign qualifiers

6/21/20 11:52:26 AM 25
SIZE QUILIFIER
 It alters the size of the basic data type.
 There are two size qualifiers that can be
applied to integer: ‘short’ and ‘long’
 In any ANSI C compiler, the size of ‘short int’,
‘int’ and ‘long int’ follow the following
restrictions
 The minimum size of a ‘short int’ 2
bytes
The size of an ‘int’ must be greater than
or equal to that of
‘short int’
The size of a ‘long int’ must be greater
than or equal to that
6/21/20 11:52:26 AM 26
SIGN QUALIFIER
 It specifies whether a variable can hold both
positive and negative numbers or only
positive numbers.
 There are two sign qualifiers: ‘signed’ and
‘unsigned’
 These qualifiers can be applied to the data
type ‘int’ and ‘char’ only.

6/21/20 11:52:26 AM 27
CONSTANTS/LITERALS
 An entity whose value is not changed during program
execution is called constant. Constants are also called
literals
 There are four basic types of constants in C.

 Integer Constant
 Floating point constant
 Character constant
 Enumeration constant

Rules for Numeric Constants:


 Commas and blank spaces cannot be included within
the constant.
 The constant can be preceded by a minus(-) sign if
required.
 The value of constant cannot exceed specified range

6/21/20 11:52:26 AM 28
INTEGER CONSTANTS
 An integer constant is an integer valued number
which consists of sequence of digits.
 Integer constants can be written in decimal,
octal and hexadecimal number system.
 A size or sign qualifier can be appended at the
end of the constant. The suffix ‘u’ is used for
unsigned integer constants, ‘l’ for long integer
constants and ‘s’ for short integer constant.
 Unsigned integer constants ( 123u,123U etc.)
 Long integer constants (123456l, 123456L etc)
 The suffixes can be combined and specified in
any order. (1234567UL, 12345lu etc.)
6/21/20 11:52:26 AM 29
DECIMAL INTEGER CONSTANT
 A decimal integer constant can consists of any
combination of digits taken from the set 0
through 9.
 If the constant consists two or more digits, the
first digit must be something other than 0.
 Examples of valid decimal integer constant
0 32 674 7597 32767 9988
 Examples of invalid decimal integer constant
Decimal Integer Constants Reasons
12,345 Illegal character (,)
123.45 Illegal character (.)
12 34 56 Illegal character (blank space)
12-34-56 Illegal character (-)
0123 The first digit cannot be zero
6/21/20 11:52:27 AM 30
OCTAL INTEGER CONSTANT
 An octal integer constant consists of any
combination of digits from 0-7.
 The first digit of an octal integer constant
must be 0.
 Examples of valid octal integer constant
0 032 0674 0757 032767
 Examples of invalid octal integer constant
Octal Integer Constant Reasons

123 Does not begin with 0

01238 Illegal digit (8)

0123.45 Illegal character (.)

6/21/20 11:52:27 AM 31
HEXADECIMAL INTEGER CONSTANT
 An hexadecimal integer constant consists of any
combination of digits from 0-9 and a-f.
 The first digit of an hexadecimal integer constant
must be 0x or 0X.
 Examples of valid hexadecimal integer constant
0x 0X3FE2 0x674 0X759 0x32ab
 Examples of invalid hexadecimal integer constant

Hexadecimal Integer Constant Reasons

123 Does not begin with 0x or 0X

0X12G8 Illegal character(G)

0x123.45 Illegal character (.)

6/21/20 11:52:27 AM 32
FLOATING POINT CONSTANTS
 A floating point constant is a base 10 number
that contains either a decimal point or an
exponent (or both).
 Examples of valid floating point constant:
1. 0.23 546.789 45E-7 3E5 32.456E+9
 Examples of invalid floating point constant
Floating point constant Reason
6 Either a decimal point or
exponent must be present
12,345.6 Illegal character(,)
123E12.3 The exponent must be an
integer quantity
123E 12 Illegal character(blank
space) in the exponent
6/21/20 11:52:27 AM 33
CHARACTER CONSTANT
 A character constant is a single character enclosed in single
quotation marks.
 Character constants have integer values that are
determined by the computer’s particular set.
 Most computers, and virtually all personal computers,
make use of the ASCII character set, in which each
individual character is numerically encoded with its own
unique-7 bit combination.
 The first 32 characters in the ASCII-table are unprintable
control codes and are used to control peripherals such as
printers.
 Codes 32-127 are common for all the different variations of
the ASCII table, they are called printable characters,
represent letters, digits, punctuation marks, and a few
miscellaneous symbols. Character 127 represents the
command DEL.
6/21/20 11:52:27 AM 34
STRING CONSTANT

 A string constant is a collection of any number


of characters of any types enclosed in double
quotation marks.
 Examples of string constant are as follow:
“rajesh” “123” “$456.57” “ ”
 The compiler automatically places a null
character(\0) at the end of every string
constant, as the last character within the string
and this character is not visible when the string
is displayed.

6/21/20 11:52:27 AM 35
 There are two simple ways inDEFINING
C to define constants:
CONSTANTS
a. Using #define preprocessor.
b. Using const keyword.
The #define Preprocessor
 Syntax:
#define identifier value
 Exmple:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n’
void main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
When the above code is compiled and executed, it produces the following
result:
value of area : 50
6/21/20 11:52:27 AM 36
The ‘const’ keyword
 Syntax:
const type variable = value;
 Example
#include <stdio.h>
void main()
{
const int LENGTH=10;
const int WIDTH =5;
const char NEWLINE='\n’;
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
When the above code is compiled and executed, it produces the
following result:
value of area : 50

6/21/20 11:52:27 AM 37
EXPRESSIONS
 An expression is a combination of variables,
constants and operators written according to
the syntax of C language. In C, every expression
evaluates to a value i. e. every expression
results in some value of a certain type that can
be assigned to a variable.
 Some examples of C expressions are shown in
the table given below.
Algebraic Expression C Expression
(a + b) ÷ c (a +b) / c
(m + n) (x + y) (m + n) * (x + y)
(ab ÷ c) a*b/c
3x2 +2x + 1 3*x*x+2*x+1
(x / y) + c x/y+c
6/21/20 11:52:27 AM 38
EXPRESSION CONTD.
Evaluation of Expressions :
 Expressions are evaluated using an assignment
statement of the form
Variable = expression;
 Variable is any valid C variable name. When the
statement is encountered, the expression is evaluated
first and then replaces the previous value of the
variable on the left hand side.
 All variables used in the expression must be assigned
values before evaluation is attempted.
Example of evaluation statements are
 x = a * b – c
 y = b / c * a
 z = a – b / c + d;

6/21/20 11:52:27 AM 39
EXPRESSION CONTD.
The following program illustrates the effect of presence of
parenthesis in expressions.
main ()
{ Output:
float a, b, c x, y, z; x = 10.00 y = 7.00 z = 4.00
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %.2f\t”,x);
printf (“y = %.2f\t”,y);
printf (“z = %.2f”,z);
}
6/21/20 11:52:27 AM 40
STATEMENTS
 Statement causes the computer to carry out some
action. There are three types of statement.
 Simple statement or Expression statement
 Compound statement
 Control Statement
 Simple statement:

A simple statement consists of an expression


followed by a semicolon. The execution of a simple
statement causes the expression to be evaluated.
Examples
b=5;
c=d+e;
printf("Enter a number");
6/21/20 11:52:27 AM 41
STATEMENT CONTD.
 Compound statement:
A compound statement consists of several
individual statements enclosed in a pair of
braces { }. It provides capability for
embedding statements within other statements.
Example
{
r=5;
area=3.14*r*r;
}
It shows a compound statement to calculate area
of a circle.
6/21/20 11:52:27 AM 42
STATEMENT CONTD.
 Control statement:
Control statements are used to create special
program features, such as logical tests, loops and
branches.
Many control statements require that other statements
be embedded within them as illustrated in the
following example.
Example:
while (n!=0)
{
r=n%10;
printf(“%d”,r);
n=n/10;
}
6/21/20 11:52:27 AM 43
TYPE CASTING
 The process of converting one type of data into another type is
called type casting. It is also known as type conversion.
 Types of Type Casting
Implicit type casting

Explicit type casting


 The process of automatic conversion of any intermediate values
to the proper type so that the expression can be evaluated
without loosing any significance is called implicit type casting.
 Example
float x;
int x1=5;
int x2=2;
x=x1/x2;
printf("Output is%f", x);
 Output is 2.000000

6/21/20 11:52:27 AM 44
TYPE CASTING CONTD.
 The process of converting one type of data into
another type manually by writing program code is
called explicit type casting.
 The general syntax is
(type_name) expression
 Example
float x;
int x1=5;
int x2=2;
x=(float)x1/x2;
printf(“x=%f",x);
 Output:
x=2.500000
6/21/20 11:52:27 AM 45

You might also like