Professional Documents
Culture Documents
Unit 2 (Elements of C)
Unit 2 (Elements of C)
Sulav Nepal
Email: nep.sulav@live.com
Contact No.: 9849194892
Master’s in Computer Information System (MCIS) – Pokhara University
Bachelor of Science. Computer Science & Information Technology (B.Sc. CSIT) – Tribhuwan University
Microsoft Technology Associate (MTA): Windows Server Administration Fundamentals
Microsoft Certified Technology Specialist (MCTS): Windows Server 2008 R2, Server Virtualization
Microsoft Specialist (MS): Programming in HTML5 with JavaScript and CSS3
Microsoft Students Partner (MSP) 2012 for Nepal
P P
R R
E
P
Syllabus E
P
A UNIT 2: Elements of C A
R R
E E
D
o C Standards C Character Set D
B B
Y Y
o C Tokens Escape Sequence
S S
U U
L o Delimiters Variables L
A A
V V
o Data Types Structure of a C Program
N N
E E
P o Executing a C Program Constants/Literals P
A A
L L
o Expressions, Statements and Comments
2 2/15/2023 7:51 AM
P P
R R
E E
P P
A A
R R
UNIT 2
E E
D D
B B
Y Y
S ELEMENTS OF C S
U U
L L
A A
V V
N N
E E
P P
A A
L L
3 2/15/2023 7:51 AM
P P
R
E First C Program R
E
P P
A A
R R
E
D
//First program in C E
D
B
#include<stdio.h>
B
Y void main() Y
S { S
U U
L printf(“This is my first program in C.”); L
A A
V printf(“Welcome to programming world!”); V
N } N
E E
P P
A A
L L
4 2/15/2023 7:51 AM
P P
R
E C Standards (ANSI C & C99) R
E
P P
A
R
ANSI C, ISO C, and Standard C refer to the successive standards for A
R
E the C programming language published by the American National E
D D
Standards Institute (ANSI).
B B
Y Y
5 2/15/2023 7:51 AM
P P
R
E Character Set R
E
P P
A
R
Set of characters that are used to form words, numbers and expression A
R
E in C is called C character set. E
D D
B B
Y Characters in C are grouped into the following four categories: Y
N
E
Special Characters (, . ; : ? ‘ “ & ^ * - + < >) N
E
P P
A A
L White spaces (Blank space, Horizontal tab, etc.) L
6 2/15/2023 7:51 AM
P P
R
E Keywords R
E
P P
A
R
These are predefined words for a C programming language. A
R
E E
D D
All keywords have fixed meaning and these meanings cannot be
B B
Y changed. Y
7 2/15/2023 7:51 AM
P P
R
E Identifiers R
E
P P
A
R
Every word used in C program to identify the name of variables, A
R
E functions, arrays, pointers and symbolic constants are known as E
D D
identifiers.
B B
Y Y
B B
Y Similarly in C, 10 is an integer number whereas 10.5 is a float Y
S number. S
U U
L L
A
V
There are variety of data types available. A
V
N N
E ANSI C supports three classes of data types: E
P P
A Primary/Fundamental data types A
L L
User-Defined data types
2/15/2023 7:51 AM
10 Derived data types
P P
R
E Primary/Fundamental Data Types R
E
P P
A
R
Primary data types are categorized into five types: A
R
E E
D D
Integer type (int)
B B
Y Y
Floating point type (float)
S S
U U
L L
A Double-precision floating point type (double) A
V V
11 2/15/2023 7:51 AM
P P
R
E Integer Type R
E
P P
A Integers are whole numbers. A
R R
E E
D Requires 16 bits of storage. i.e. 2 bytes for 16-bit compiler. D
B B
Y Requires 32 bits of storage. i.e. 4 bytes for 32-bit compiler.
Y
S S
U Three classes of integer:
U
L Integer (int) L
A Short integer (short int) A
V Long integer (long int) V
N N
E Both signed and unsigned forms. E
P P
A A
L Defined as: L
int a;
12 2/15/2023 7:51 AM
int myValue=6;
P P
R
E Integer Type R
E
P P
A Signed Integer Unsigned Integer A
R R
E It represents both positive and negative integers It represents only positive integers E
D D
The data type qualifier is signed int or int. The data type qualifier is unsigned int or
B B
unsigned.
Y Y
Variables are defined as:
S signed int a; Variables are defined as:
S
U int b; unsigned int a; U
L unsigned b; L
A A
V By default all int are signed Unsigned int have to be declared explicitly V
13 2/15/2023 7:51 AM
P P
R
E Floating Point Type R
E
P P
A
R
Floating point types are fractional numbers A
R
E E
D D
In C, it is defined by float
B B
Y Y
N float a; N
E E
P float myValue=6.5; P
A A
L L
14 2/15/2023 7:51 AM
P P
R
E Double Precision Floating Point Type R
E
P P
A
R
Used to represent the floating point numbers A
R
E E
D D
Reserves 64 bits (i.e. 8 bytes)
B B
Y Y
S In C, it is defined by double S
U U
L L
A A
V Variables are defined as: V
N double a; N
E E
P double myValue=4244.546; P
A A
L L
15 2/15/2023 7:51 AM
P P
R
E Character Type R
E
P P
A
R
A single character can be defined as a character type data A
R
E E
D D
Stored in 8 bits (1 byte)
B B
Y Y
N N
E E
P The signed char has values from -128 to 127 P
A A
L L
16 2/15/2023 7:51 AM
P P
R
E Character Type R
E
P P
A
R
Each character is represented by an ASCII (American Standard Code A
R
E for Information Interchange) E
D D
B B
Y Example: “A” is represented by 65, “B” is represented by 66, “a” is Y
N N
E With conversion character c, it will display character E
P P
A A
L L
17 2/15/2023 7:51 AM
P P
R
E ASCII Table R
E
P P
A A
R R
E E
D D
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
18 2/15/2023 7:51 AM
P P
R
E NOTE R
E
P P
A
R
The difference of corresponding uppercase and lowercase character is A
R
E always 32 E
D D
ASCII value of “a” – ASCII value of “A” = 32
B
Y
ASCII value of “m” – ASCII value of “M” = 32 B
Y
S S
U
Using this logic, we can convert uppercase letter into its lowercase U
L L
A and vice – versa. A
V V
N N
E E
P P
A A
L L
19 2/15/2023 7:51 AM
P P
R
E Void Type R
E
P P
A
R
This void type has no value A
R
E E
D D
This is usually used to specify a type of function when it does not
B B
Y return any value to the calling function Y
S S
U U
L Example: L
A A
V void main() V
N void whatIsThis(); N
E E
P P
A A
L L
20 2/15/2023 7:51 AM
P P
R
E Derived Data Types R
E
P P
A C supports a feature called type definition which allows users to define an A
R R
E identifier that would represent an existing data type. E
D D
S It allows users to define new data types that are equivalent to an existing S
U U
L data types. L
A A
V V
General form:
N N
E typedef existing_data_type new_name_for_existing_data_type;
E
P P
A A
L Example: L
21
typedef int integer; 2/15/2023 7:51 AM
P P
R
E Constants R
E
P P
A
R
A constant is a quantity that doesn’t change during the execution A
R
E E
D D
These fixed values are also called literals
B B
Y Y
N N
E There are enumeration constants as well E
P P
A A
L L
22 2/15/2023 7:51 AM
P P
R
E Integer Literals R
E
P P
A
R
An integer literal can be a decimal, octal, or hexadecimal constant. A
R
E E
D D
A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for
B B
Y octal, and nothing for decimal. Y
S S
U U
L An integer literal can also have a suffix that is a combination of U L
A
V
and L, for unsigned and long, respectively. A
V
85 /* decimal */
N N
0213 /* octal */
E Following are other examples of E
P 0x4b /* hexadecimal */ P
A various types of integer literals: 30 /* int */ A
L L
30l /* long */
23 2/15/2023 7:51 AM
P P
R
E Character Constants R
E
P P
A
R
Character literals are enclosed in single quotes A
R
E
D
‘x’ can be stored in a simple variable of char type E
D
B B
Y A character literal can be a: Y
N N
E There are certain characters in C that represent special meaning E
P P
A when preceded by a backslash. For example, new line (\n) or tab (\t) A
L L
24 2/15/2023 7:51 AM
P P
R
E String Constants R
E
P P
A
R
Sequence of characters enclosed in double quotes A
R
E E
D D
May contain letters, numbers, special characters or blank spaces
B B
Y Y
S Example: S
U U
L “hello” L
A A
V “hi” V
N “2076” N
E E
P P
A A
L L
25 2/15/2023 7:51 AM
P P
R
E Variables R
E
P P
A
R
A symbolic name which is used to store data item. i.e. a numerical A
R
E quantity or a character constant. E
D D
B B
Y Unlike constant, the value of a variable can change during the Y
S execution of a program. S
U U
L L
A
V
The same variable can store different value at different portion of a A
V
program.
N N
E E
P P
A Variable name may consist of letters, digits, or underscore characters. A
L L
26 2/15/2023 7:51 AM
P P
R
E Variable Declaration R
E
P P
A
R
Any variable should be defined before using it in a program A
R
E E
D D
Variable declaration syntax:
B B
Y data-type variable_name1, variable_name2, … Y
S S
U U
L Valid declaration are: L
A A
V int n1; V
27 2/15/2023 7:51 AM
P P
R
E Rules for Variable Declaration R
E
P P
A The variable name should start with only letters. A
R R
E E
D D
The variable name shouldn’t be keyword.
B B
Y Y
S
White spaces are not allowed between characters of variable but
S
U underscores are approved. U
L L
A A
V
The variable name is case sensitive. V
B B
Y Placed in the source program before the main function. Y
N N
E E
P These statements are called preprocessor directives as they are P
A processed before compilation of any other source code in the program. A
L L
29 2/15/2023 7:51 AM
P P
R
E Escape Sequences R
E
P P
A
R
An escape sequence is a non-printing characters used in C. A
R
E E
D D
Character combination consisting of backslash (\) followed by a
B B
Y letter or by a combination of digits. Y
S S
U U
L Each sequences are typically used to specify actions such as carriage L
A
V
return, backspace, line feed, or move cursors to next line. A
V
N N
E E
P P
A A
L L
30 2/15/2023 7:51 AM
P P
R
E Escape Sequences R
E
P P
A Character Escape Sequence ASCIIValue A
R R
E Bell (alert) \a 007
E
D Backspace \b 008 D
31 2/15/2023 7:51 AM
P P
R
E Escape Sequence in C R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D
B
{
B
Y printf(“Hello! \n I am testing an escape sequence”); Y
S } S
U U
L L
A A
V OUTPUT: V
N Hello! N
E E
P I am testing an escape sequence P
A A
L L
32 2/15/2023 7:51 AM
P P
R
E Escape Sequence in C R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D
B
{
B
Y printf(“Hello \tWorld \n”); Y
N OUTPUT: N
E E
P Hello World P
A A
L He said,“Hello” L
33 2/15/2023 7:51 AM
P P
R
E Tokens in C R
E
P P
A The basic elements recognized by the C compiler are the “tokens”. A
R R
E E
D D
In C, tokens are of six types:
B Keywords (e.g. int, while, float, printf) B
Y Y
N Strings (e.g.“ram”,“hello”) N
E E
P P
A Special symbols (e.g. (), {})
A
L L
Operators (e.g. +, –, *, /)
34 2/15/2023 7:51 AM
P P
R
E Delimiters R
E
P P
A
R
A delimiter is a unique character or series of characters that indicates A
R
E the beginning or end of a specific statement, string or function body E
D D
set.
B B
Y Y
B B
Y For example, in the C language, 𝒙 + 𝟓 is a legal expression. Y
S S
U U
L Every expression consists of at least one operand and can have one or L
A
V
more operators. A
V
N N
E E
P P
A A
L L
36 2/15/2023 7:51 AM
P P
R
E Expressions R
E
P P
A
R
Operands are values and operators are symbols that represent A
R
E particular actions. E
D D
B B
Y Types of expressions: Y
37 2/15/2023 7:51 AM
P P
R
E Comments R
E
P P
A Used for program documentation. A
R R
E E
D D
Comments are not compiled.
B B
Y Y
S
The C syntax for writing comment is
S
U /* U
L L
A anything written in between slash and asterisk and asterisk A
V
and slash is a comment V
N */ N
E E
P P
A A
L Another way to write comment in C is L
38
//using double slash (this line only) 2/15/2023 7:51 AM
P P
R R
E E
P P
A A
R R
E E
D D
B B
Y Y
S
END OF UNIT TWO
S
U U
L L
A A
V V
N N
E E
P P
A A
L L
39 2/15/2023 7:51 AM