0% found this document useful (0 votes)
7 views41 pages

Lesson 3

Uploaded by

venomisgodlike
Copyright
© © All Rights Reserved
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)
7 views41 pages

Lesson 3

Uploaded by

venomisgodlike
Copyright
© © All Rights Reserved
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/ 41

C and Data Structures (CDS)

LECTURE 3
C Language Fundamentals

Presented By:
Pratibha Ku. Khandual
Department of CSE,
VSSUT, Burla
Elements of C
• This lecture deals with the basic elements, which are
used to create a C program. These elements are:-
– Character set
– Keywords
– Identifiers
– Data Types
– Constant and Variables
– Statements

2
Character Set
• Character set in C includes the following:
– The uppercase English alphabets A to Z
– The lowercase English alphabets a to z
– The digits 0 to 9
– Certain special characters as building blocks to form
basic program elements i.e., Constants, variables,
operators, expressions and statements.

3
Character Set

4
Keywords
• Keywords are the words whose meaning
has already been explained to the C
compiler.
• The keywords cannot be used as variable
names because assigning a new meaning to
the keyword is not allowed by the C compiler.
• There are only 32 keywords available in C.

5
Keywords

6
Identifiers
• Identifiers are user-defined names given to
various items in the program, such as variables,
functions, arrays, structure, union, etc.
• An identifier can be composed only of uppercase
letters, lowercase letters, underscore and digits,
but should start only with an alphabet or an
underscore.
• Underscore character is usually used as a link
between two words in long identifiers.
7
Rules for Constructing Identifiers
1) The first character in an identifier must be an alphabet or
an underscore and can be followed by any number of
alphabets, or digits or underscores.
2) Identifiers must not begin with a digit.
3) Uppercase and lowercase letters are distinct. That is,
identifiers are case sensitive.
4) Commas or blank spaces are not allowed within an
identifier.
5) Keywords cannot be used as an identifier.
6) Identifiers should not be of length more than 31
characters.
7) Identifiers must be meaningful, short, quickly and easily
typed and read.
8
Example of Identifiers
• Valid identifiers: total sum average
_x y_ mark_1 x1
• Invalid identifiers:
1x - begins with a digit
char - reserved word
x+y - special character

9
Data Types
• In the C programming language, data types refer to a
domain of allowed values and the operations that
can be performed on those values.
• The data type of a variable determines the
following information:
– Identify the type of a variable when it is declared
– The amount of space the variable occupies in storage
– Specific operations that can be performed on the
variable.

10
Data Types
• There are 4 primary/basic data types in C, which are as
follows :–
– char is used to store any single character
– int is used to store any integer value
– float is used to store any single precision floating point
number
– double is used to store any double precision floating point
number
• We can use two qualifiers with these basic types to get
more data types. The two types of qualifiers are as
follows:
– Sign qualifier: signed and unsigned
– Size qualifier: short and long
11
Built-In/Basic Data Types in C
• Based on the combination of each of the primary data types
and the qualifiers (sign and size) we can have the following
basic data types:

12
Data Types in C
• C provides the following categories of data types:

13
Derived Data Types in C
• Derived data types are composed of fundamental
data types. Example of derived data types are arrays,
structures, pointers etc.
– A collection of integer values stored contiguously
makes an array
• Derived data types are what we can create by
combining various primitive data types. There are
basically three derived types in C:
– Array
– Function
– Pointer
14
User-defined Data Type (UDT)
• Sometimes, the basic set of data types defined in the C
language such as int, float etc. may be
insufficient for our application.
• In such circumstances, we can create your own data
types which are based on the standard ones.
• A User-defined Data Type (UDT) is a data type that
derived from an existing data type. UDTs can be used to
extend the built-in data types already available and
create your own customized data types.
• There are three mechanisms for doing this in C:
– using typedef
– using structure
– using union
15
Void Data Type in C
• Many programming languages need a data type to
define the lack of return value to indicate that
nothing is being returned.
• The void data type is used in the definition and
prototyping of functions to indicate the following:
– Nothing is being passed as parameter to the
function.
– No return value is being returned from the
function.

16
Size and Range of Data Types in C
• Size refers to the amount of memory each data type
requires for its storage in memory.
• We can use the sizeof() operator to check the size of
a variable.
• The size of data types in C is dependent on the
compiler.
• Range means the maximum and minimum value that
can be stored inside the variable of a given data type.

17
Steps to Print the Range of Data Types
• The following three steps are used to compute the range
(min to max) of permissible values in a particular data
type:
1) Convert the size of the data type in bytes into bits (N).
2) For signed data types, the following formula is used to
find the minimum and maximum range of values:
-(2N-1) to 2N-1 - 1
Note: N-1 bits are used to compute range because one bit is used to store
the sign of the signed data type.
3) The minimum and maximum range of an unsigned data
type is given by: 0 to (2N - 1)
where, N is the sizeof(datatype) × 8 i.e., total number of
bits used by the data type.
18
19
20
Constants
• C constants is the most fundamental and essential part of
the C programming language.
• They have fixed values that are used in a program i.e., its
value remains the same during the entire execution of
the program.
• Constants are also called literals and they can be of
various data types.
• The constants can be initialized once only.
• A constant can be defined using the following syntax:
const datatype constant_name;
const keyword defines a constant in C.

21
Types of Constants in C
• Constants are categorized into two basic types, and
each of these types has its subtypes.
• C constants can be divided into the following major
categories:
– Numeric Constants
• Integer Constants
• Real Constants
– Character Constants
• Single Character Constants
• String Constants
• Backslash Character Constants
22
Integer Constants
• An integer is a numeric literal without any fractional or
exponential part.
• There are three types of integer literals in C
programming:
• Decimal Integer constant (base 10):
• Digits: 0 to 9
• E.g.: 49, 58, -62, etc.
• Octal Integer constant (base 8):
• Digits: 0 to 7
• Add “0” before the value.
• E.g.: 045, 056, 067, etc.
• Hexadecimal Integer constant (base 16):
• Digits: 0 to 9 and A to F
• Add “0x” before the value
• E.g.: 0x42, 0x56, 0x67, etc.

23
Integer Constants
Rules for constructing Integer Constants in C
– An integer constant must have at least one digit.
– It must not have a decimal point.
– It can either be positive or negative.
– No commas or blanks are allowed within an integer
constant.
– If no sign precedes an integer constant, it is assumed
to be positive.
– The allowable range for integer constants is -32768 to
32767 (-215 to 215)
(Note: This range is for 16-bit compiler. For a 32-bit
compiler the range would be even greater.)
24
Real Constants/Floating-point Literals
• A floating-point literal is a numeric literal that has
either a fractional form or an exponent form.
• Rules for constructing Real constants (Fractional
Form)
– A real constant must have at least one digit
– It must have a decimal point
– It could be either positive or negative
– If no sign precedes an real constant, it is assumed to
be positive.
– No commas or blanks are allowed within a real
constant.
• For example: +867.9, -26.9876, 654.0.
25
Real Constants/Floating-point Literals
• Rules for constructing Real Constants (Exponential
Form)
– The mantissa part and the exponential part should be
separated by the letter ‘e’
– The mantissa may have a positive or negative sign
(default sign is positive)
– The exponent must have at least one digit
– The exponent must be a positive or negative integer
(default sign is positive)
– The range of real constants in exponential form is -
3.4e38 and +3.4e38
• For example: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4
26
Character Constants
• A “single character constant" is formed by enclosing a single
character within single quotation marks (' ').
• A character constants can contain any uppercase alphabet,
any lowercase alphabet, a space, a digit and any special
character. For Example: ‘m‘, ‘=‘, ‘A’, etc.
• Character constant declaration: A character constant is
declared by using const keyword.
Syntax: const char constant_name = 'value';
• Format Specifier for Character Variable:
– “%c” is used as format specifier for character inside C.
– We can also use “%d” as format specifier because each
character have its equivalent integer value known as ASCII
value.

27
Character Constants
• Rules for Constructing Single Character constants
– A single character constant or character constant is a single
alphabet, a single digit or a single special symbol enclosed
within single quotes.
– Both the single quotes should point to the left. For
example, ‫ۥ‬A ‫ ۥ‬is a valid character constant whereas ‛A’ is
not.
– The maximum length of a single character constant can be
one character. For example,
Invalid: ’123’ as length should be 1

28
String Character Constant
• A character string, a string constant consists of a
sequence of characters enclosed in double quotes.
• Rules for Constructing String Constants
– A string constant may consist of any combination of
digits, letters, escape sequences and spaces enclosed
in double quotes.
– Every string constant ends up with a NULL character
(‘\0’) which is automatically assigned by the compiler
before the closing double quotation mark.
• For Example: “a”, “Ali”, “123”, “How are you”, etc. are
string constants.
29
Escape Sequence or Backslash
Character Constant
• Escape sequence usually consists of a backslash
followed by a character. These characters
combinations are called as escape sequence.
• Although it consists of two characters, it
represents single character.
• Each escape sequence has unique ASCII value.
• They are non-printable characters.

30
31
Variables
• C variable is a named location in a memory where a
program can manipulate the data.
• This location is used to hold the value of the variable.
• The value of the C variable may get changed in the
program.
• C variable might be belonging to any of the data type
like int, float, char, etc.

32
Rules For Naming C Variable
• Variable name must begin with letter or underscore.
• Variables are case sensitive.
• They can be constructed with digits, letters.
• No special symbols are allowed other than
underscore.
• For Example: sum, height, _value are some variable
names.

33
Declaring and Initializing C Variable
• Variables should be declared in the C program before
they are in use. This is done is done for informing the
compiler that there exist a variable with specific data
type which is used in the program.
• Variable declaration does the following things:
– It tells the compiler what the variable name is.
– It specifies what type of data the variable will hold.
• There are two things which need to be defined while
declaring a variable:
– Data type - which type of value is going to be stored in the
variable.
– Identifier - Valid variable name (identifier is the name of
the allocated memory blocks for the variable).

34
Declaring and Initializing C Variable
• When a variable is declared, it contains undefined value
commonly known as garbage value.
• If we want we can assign some initial value to the variables
during the declaration itself. This is called initialization of the
variable.

35
Declaring and Initializing C Variable

36
Statements in C
• Statements are the primary building blocks of a program. A
program is a series of statements with some necessary
punctuation.
• A statement is a complete instruction to the computer.
• In C, statements are indicated by a semicolon at the end.
• Most of the statements in a C program are expression
statements. An expression statement is simply an expression
followed by a semicolon.
• The lines i = 0;
i = i + 1;
printf("Hello, world!\n");
are all expression statements.
37
Compound Statements in C
• Compound statements (also called a block) are those that appears
as the body of another statement.
• Unlike expression statements, they do not end with a semicolon.
• Compound statements is usually put on multiple lines, with one
statement per line and are executed together.
• Curly brackets { } are placed before and after compound
statements. For Example:
{
line_num = 0;
page_num++;
}
• Each inner statement ends with a semicolon, but the compound
statement itself does not terminate with a semicolon.

38
Compound Statements in C
• Compound statements can also be executed within a
loop. For Example:

39
ANY DOUBTS?

1/29/2025 40
THANK YOU!

1/29/2025 41

You might also like