0% found this document useful (0 votes)
27 views30 pages

C Lecture 3

The document discusses different data types in C including integer, floating point, character, and void. It describes each data type, their syntax, range, size, and examples.

Uploaded by

Gagan
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)
27 views30 pages

C Lecture 3

The document discusses different data types in C including integer, floating point, character, and void. It describes each data type, their syntax, range, size, and examples.

Uploaded by

Gagan
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/ 30

C character set, Identifiers and

keywords, Datatypes

1
Character Set
• C language also has a set of characters which include alphabets,
digits, and special symbols.

• C language supports a total of 256 characters.

• Every C program contains statements. These statements are


constructed using words and these words are constructed using
characters from C character set.

• C language character set contains the following set of characters.


➢ Alphabets
➢ Digits
➢ 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:

Special Characters in C Programming


, < > . _

( ) ; $ :

% [ ] # ?

' & { } "

^ ! * / |

- \ ~ +

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

break else long switch

case enum register typedef


char extern return union
continue for signed void

do if static while

default goto sizeof volatile


const float short unsigned

8
Identifiers
• Identifiers are the building blocks of a program.

• Identifiers are unique names that are assigned to variables,


structs, functions, and other entities.

• They are used to uniquely identify the entity within the


program.

• Example: In the below example “age” is an identifier


assigned to the character type value.

int age = 20;


9
Identifiers
• Rules for naming identifiers:
S.No. Description
1 Identifiers may include letters (a-z, A-Z) and digits (0-9).

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.

6 Each identifier must be unique within its namespace.

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.

• For instance, we may want to utilize some numbers such as 5, 8, 600, or


maybe a decimal point number such as 43.59, 127.368, 271.49, or
maybe a text such as “cappuccino”. Then, the compiler used in C
language would handle all of these very differently. Thus, here, we use
different data types for defining what data types we want in the
program.
13
Data Types

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.

• Syntax: int var_name;

• Example: int age;

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.

• The int with 2 bytes of memory is capable of storing values ranging


from -32,768 to +32,767.

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.

• It is majorly of two different types:


• double

• 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.

• Range: 1.2E-38 to 3.4E+38

• 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.)

• Syntax: float var_name;

• Example: float average=12.06;

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.

• It can easily accommodate about 16 to 17 digits after or before a decimal point.

• Range: 1.7E-308 to 1.7E+308

• Size: 8 bytes

• Syntax: double var_name;

• Example: double c = 2312312.123123;


20
Data Types – Character
• Character data type allows its variable to store only a single character.

• The size of the character is 1 byte.

• It is the most basic data type in C.

• It stores a single character and requires a single byte of memory in almost


all compilers.

• Range: (-128 to 127) or (0 to 255)

• Size: 1 byte

• Syntax: char var_name;

• Example: char a = 'a';


21
Data Types – void
• The void data type in C is used to specify that no value is
present.

• It does not provide a result value to its caller.

• It has no values and no operations.

• It is used to represent nothing.

• Void is used in multiple ways as function return type, function


arguments as void, and pointers to void.

22
Data Types – void

Syntax:
• // function return type void

void exit(int check);

• // Function without any parameter can accept void.

int print(void);

• // memory allocation function which returns a pointer to void.

void *malloc (size_t size);

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.

• C language provides the sizeof() operator to check the 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.

• The symbol we use in every format specifier is %.

• 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

You might also like