C Lecture 3
C Lecture 3
keywords, Datatypes
1
Character Set
• C language also has a set of characters which include alphabets,
digits, and special symbols.
2
Character Set
• Alphabets
• In C programming, we can use both uppercase and
lowercase letters of the English language
• Uppercase Letters: A to Z
• Lowercase Letters: a to z
• Digits
• We can use decimal digits from 0 to 9.
3
Character Set
• Special Symbols
• C Programming allows programmers to use the following
special characters:
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
4
Character Set
• White Spaces
• In C Programming, white spaces contain:
• Blank Spaces
• Tab
• Carriage Return
• New Line
5
Character Set
6
Keywords
• Keywords are predefined, reserved words used in programming
that have special meanings to the compiler.
• Keywords are part of the syntax, and they cannot be used as an
identifier.
• There are a total of 32 keywords in C.
• Example:
int age;
• Here, int is a keyword that indicates age which is a variable of type int
(integer).
7
Keywords
C Keywords
auto double int struct
do if static while
8
Identifiers
• Identifiers are the building blocks of a program.
2 Special characters are not allowed in identifiers except for the underscore ‘_’.
3 Spaces are prohibited in identifier names.
4 An identifier must start with a letter or an underscore.
Keywords like printf, scanf, int, char, struct, etc., cannot be used as identifiers
5
as they are reserved for specific tasks and will cause a compilation error.
In C language, due to case sensitivity, ‘age’ and ‘AGE’ are considered different
7
identifiers.
10
Identifiers
• Valid name:
• max_value – underscores can be used to separate words.
• _age – underscore can be used at the start of an identifier.
• Invalid names:
• float – this is a keyword in C and cannot be used as a variable
name.
• 2nd_place – variable names cannot begin with a number.
• total-sum – hyphens are not allowed in variable names; they
should be underscores.
11
Difference between keyword and identifier
S.NO. KEYWORD IDENTIFIER
Keywords are reserved and cannot be Identifiers are user-defined names for program
1
redefined. elements.
Are used to perform specific functions
2 Refer to variable names, functions, arrays, etc.
in a language.
Can be created using letters, digits, and
3 Cannot be used as identifiers.
underscores.
Are not case-sensitive (typically in
4 Are case-sensitive.
most languages).
The number of keywords is fixed for a
5 The number of identifiers is not fixed.
language.
Examples include if, else, while, Examples include userScore, readFile,
6
return. maxValue.
7 Defined by the language's syntax. Defined by the programmer.
Cannot be created or changed by the
8 Can be named almost anything by the user.
user.
12
Data Types
• Data types used in C language refer to an extensive system that we use
to declare various types of functions or variables in a program.
• A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
14
Data Types - Integer
• Integer – The variables that are of integer type are capable of storing
negative, zero, as well as positive values without the decimals. Example:
-25, 30, 0, etc.,
• The C language represents the integer data type by the keyword int.
• int takes up about 2 bytes or 4 bytes of the storage space in a 32-bit type
and 64-bit type operating system respectively.
15
Data Types - Integer
• Integer – The variables that are of integer type are capable of storing
negative, zero, as well as positive values without the decimals.
Example: -25, 30, 0, etc.,
• The C language represents the integer data type by the keyword int.
16
Data Types - Integer
• The int with 4 bytes of memory is capable of storing values ranging
from -2,147,483,648 to +2,147,483,647.
Trying to
store the
values more
than the
limit
17
Data Types – Floating Point
• We use the floating-point data types for storing the decimal values
into a variable in a C program.
• float
Syntax:
float var_name;
double var_name;
18
Data Types – float
• It is used to store decimal numbers (numbers with floating point values)
with single precision.
• Size: 4 bytes (However, it varies a lot on the basis of the processor present
in the CPU in use in the form of int type.)
19
Data Types – double
• A Double data type in C is used to store decimal numbers (numbers with
floating point values) with double precision.
• The double data type is basically a precision sort of data type that is capable of
holding 64 bits of decimal numbers or floating points.
• Since double has more precision as compared to that float then it is much more
obvious that it occupies twice the memory occupied by the floating-point type.
• Size: 8 bytes
• Size: 1 byte
22
Data Types – void
Syntax:
• // function return type void
int print(void);
23
Size of Data Types in C
• The size of the data types in C is dependent on the size of the
architecture, so we cannot define the universal size of the data
types.
• Syntax:
• sizeof(data type);
• Example:
• sizeof(int);
• sizeof(float);
24
sizeof() - Example
25
Format Specifiers
• Format specifiers in C are used to take inputs and print the
output of a type.
• Format specifiers tell the compiler about the type of data that
must be given or input and the type of data that must be printed
on the screen.
26
Format Specifiers
27
Data Types
28
Format Specifiers
29
Format Specifiers - Example
30