Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

C Programming (CSC110)

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

S  Historically, the names referred specifically to the original and best-


S
U supported version of the standard (known as C89 or C90). U
L L
A A
V V
 In March 2000,ANSI adopted the ISO/IEC 9899:1999 standard.
N N
E E
P P
A  This standard is commonly referred to as C99. A
L L

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

S  Letters and alphabets (A…Z, a…z) S


U U
L L
A  Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) A
V V

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

S auto double int struct S


U break else long switch U
L L
A case enum register typedef A
V char return union const V

N float short unsigned continue N


E void for default goto E
P P
A sizeof volatile do if A
L static while extern signed L

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

S  Names given by user can consist of a sequence of letters and digits,


S
U with a letter as the first character. U
L L
A A
V V
 Example: myVariable, myName, heyYou, callThisNumber45,
N N
E add_this_number, etc. E
P P
A A
L
 There are certain rules to be followed while naming identifiers. L

8 (KEEP THIS IN MIND) 2/15/2023 7:51 AM


P P
R
E Rules for Naming Identifiers R
E
P P
A  It must be a combination of letters and digits and must begin with a A
R R
E letter. E
D D

B  Underscore is permitted between two digits and must begin with a B


Y Y
letter.
S S
U U
L  Only first 31 characters are significant. L
A A
V V

N  Keywords cannot be used. N


E E
P P
A
L
 It is case sensitive. i.e. uppercase and lowercase letters are not A
L
interchangeable.
9 2/15/2023 7:51 AM
P P
R
E Data Types R
E
P P
A
R
 10 is a whole number where as 10.5 is a fractional/rational A
R
E number. E
D D

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

N  Character type (char) N


E E
P P
A A
L  Void type (void)
L

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

N It ranges from −215 to +215 It ranges from 0 to +216 N


E E
i.e. −32768 to 32767 i.e. 0 to 65535
P P
A A
L Its conversion character is d Its conversion character is u L

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

S  Reserves 32 bits (i.e. 4 bytes) S


U U
L L
A A
V  Variables are defined as: V

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

S  The qualifier signed or unsigned may be used with char S


U U
L L
A A
V  The unsigned char has values between 0 and 255 V

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

S represented by 97,“z” is represented by 122. S


U U
L L
A
V
 With conversion character d, it will display ASCII value A
V

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

B  typedef statement is used to give new name to an existing data type. B


Y Y

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

S  Constants can be of any of the basic data types like an integer S


U U
L constant, a floating constant, a character constant, or a L
A
V
string literal A
V

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

S  Plain character (e.g.‘x’) S


U U
L  An escape sequence (e.g.‘\t’) L
A
V
 Or a universal character (e.g.‘\u02C0’) A
V

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

N int centi, temp; N


E E
P float radius; P
A A
L char gender; L

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

N TEMP and temp are different variables. N


E E
P P
A A
L  No two variables of the same name are allowed to be declared in the L
same scope.
28 2/15/2023 7:51 AM
P P
R
E Preprocessor Directives R
E
P P
A
R
 Collection of special statements that are executed at the beginning of A
R
E a compilation process. E
D D

B B
Y  Placed in the source program before the main function. Y

S #include<stdio.h> //used for file inclusion S


U U
L #define PI 3.1416 //defining symbolic constant PI L
A A
V #define TRUE 1 //used for defining TRUE as 1 V

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

B Horizontal tab \t 009


B
Y Vertical tab \v 011 Y

S Newline (linefeed) \n 010


S
U Form feed \f 012 U
L L
A Carriage return \r 013 A
V Quotation mark (“) \“ 034 V

N Apostrophe (’) \’ 039 N


E Question mark (?) \? 063 E
P P
A Backslash (\) \\ 092 A
L Null \0 000 L

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

S printf(“He said, \“Hello\””); S


U U
L } L
A A
V V

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

S  Identifiers (e.g. sum, total, num_of_hours) S


U U
L L
A  Constants (e.g 10, 20, -15.4) A
V V

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

S  Delimiter examples include:


S
U  Round brackets or parentheses – () U
L L
A A
V V
 Curly brackets – {}
N N
E E
P  Escape sequence or comments – /* P
A A
L L
 Double quotes for defining string literals – “ ” 2/15/2023 7:51 AM
35
P P
R
E Expressions R
E
P P
A
R
 In programming, an expression is any legal combination of symbols A
R
E that represents a value. E
D D

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

S Type Explanation Example S


U U
L Infix Expression in which operator is in between operands a+b
L
A Prefix Expression in which operator is written before operands +ab A
V V
Postfix Expression in which operator is written after operands ab+
N N
E E
P P
A A
L L

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

You might also like