0% found this document useful (0 votes)
16 views7 pages

C Assingment

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)
16 views7 pages

C Assingment

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

SUBJECT : PROBLEM SOLVEING IN “C”

ASSINGMENT-2

(CHARACTERS AND TOKENS IN C)

SUBMITED TO: SUBMITED BY:


Mrs. Bhanu Priya karan singh
30342
BCA SEM-1

CHARACTERS IN “C”
Character Set includes a set of valid characters we can use in our program in different
environments. C language has broadly two character sets.
• Source Character Set (SCS): SCS is used to parse the source code into internal
representation before preprocessing phase. This set includes Basic Character Set
and White-space Characters.
• Execution Character Set (ECS): ECS is used to store character string constants.
Other than Basic Character Set, this set contains Control Characters and Escape
Sequences.

Character Set in C
In C, the character set used to represent characters in source code is based on the ASCII
(American Standard Code for Information Interchange) character set. The ASCII character
set includes the following elements:

1. Alphabetic Characters (A-Z, a-z): These represent both uppercase and lowercase
English letters. For example, 'A' to 'Z' and 'a' to 'z'.
2. Digits (0-9): These represent numeric characters. For example, '0' to '9'.
3. Special Characters: These include various special characters used in
programming and text processing. Some common special characters include:
• Arithmetic Operators: +, -, *, /, %
• Relational Operators: <, >, ==, !=, <=, >=
• Logical Operators: &&, ||, !
• Punctuation: ;, :, ,, ., ?, !
• Brackets and Parentheses: (, ), [, ], {, }
• Quotes: ', "
• Backslash: \
• Ampersand: &
• Dollar Sign: $
• Hash or Pound Sign: #
4. Whitespace Characters: These include space (' '), newline ('\n'), and form feed ('\f').
These characters are used for formatting and layout in code and text.
5. Control Characters: These include non-printable characters like the escape
character ('\e') and others used for control and formatting, such as '\b' (backspace),
'\t' (tab), and '\n' (newline).
6. Escape Sequences: C allows you to use escape sequences to represent
characters that are difficult to type or invisible. For example, '\n' represents a
newline character, and '\t' represents a tab character.
7. Extended Characters: C also includes characters beyond the ASCII set, especially
for international character encoding. These include characters with accents,
diacritics, and symbols from various languages.

Use of Character Set in C


The character set in C, which is primarily based on the ASCII character set, is fundamental
to working with characters and text in C programming. Here are several key uses of the
character set in C:

1. Declaring and Storing Character Variables: In C, you can declare character


variables to store individual characters. For example:
char myChar = 'A';
2. String Handling: C represents strings as arrays of characters. The character set is
used extensively in creating and manipulating strings. For example:
char myString[] = "Hello, World!";
3. Input and Output: The character set is used when reading input and displaying
output using functions like printf and scanf. For example:
printf("Enter a character: ");
char inputChar;
scanf("%c", &inputChar);
4. Comparisons: Characters can be compared using relational operators, and string
comparisons are essential for tasks like sorting and searching within strings.
char char1 = 'A';
char char2 = 'B';
if (char1 < char2) {
// Do something
}
5. Conversions: You can convert between characters and other data types, such as
integers and floating-point numbers. This is helpful for character manipulation and
arithmetic.
char myChar = '7';
int num = myChar - '0'; // Converts '7' to 7
6. Character Constants: Characters are used as constants, like newline characters
('\n') or escape sequences, in control statements and text processing.
char newline = '\n';
7. Loop Control: Characters play a role in loop control, especially in character-by-
character processing. For instance, reading characters in a loop to process a file
character by character.
char ch;
while ((ch = getchar()) != EOF) {
// Process each character
}

8. Character Classification: The character set is used for character classification


functions provided by the <ctype.h> library, such as isalpha, isdigit, and islower, to
check character properties.
if (isalpha(ch)) {
// It's an alphabetic character
}

C programming language supports four types of characters:


• Alphabets: Alphabets are characters representing letters of the alphabet, both
uppercase and lowercase. In C, characters are typically represented using single
quotes (') and are enclosed within single quotes.
Here are examples of alphabetic characters:
Uppercase Alphabets: 'A', 'B', 'C', ..., 'Z'
Lowercase Alphabets: 'a', 'b', 'c', ..., 'z'

• Digits: Digits in C are the numeric characters '0' through '9'. They are used to
represent numerical values and are fundamental for arithmetic operations,
numerical input, and output. Digits are represented as characters, but they can be
converted to integer values when needed.
• Special characters: In C programming language, generally, the special symbols
have some special meaning and they cannot be used for other purposes.
• Whitespace : Whitespace characters are used for formatting and layout in code
and text. They include space (' '), tab ('\t'), newline ('\n'), carriage return ('\r'), and
form feed ('\f').
TOKENS IN “C”

Tokens in C language are the minor elements or the building blocks used to construct or
develop together a C program. These tokens in C are meaningful to the compiler. The
online C compiler breaks a program into the possible minor units known as tokens and
proceeds further to the various stages of the compilation. Every meaningful character,
word, or symbol in this C program is a C token. Compiler groups together these characters
of the program into tokens.

The compilation process:


C Program ---> Group characters into C tokens ---> Translate tokens into target code.

Uses of Tokens in C
The following are the uses of tokens:

• Tokens in C are building blocks which means a program can’t be created


without tokens.
• Tokens are classified into various subcategories, which are very important like
identifiers are used to give identity to a variable, keywords can be used to
predefine things, operators can be used to perform operations and so on.
• Tokens like strings and special symbols play a major role in dealing with
problems as strings are required to define something, and special symbols are
used as a part of the syntax and many more.

Types of Tokens in C language


Tokens in C language can be classified as:

• Keywords
• Identifiers
• Constants
• Special Characters
• Strings
• Operators
Keywords: Keywords in C language are the collection of pre-defined or reserved words.
These are case-sensitive and written in lower cases. Their meaning and functionality are
already known to the compiler. There are 32 keywords in “c”.

IDENTIFIERS : Identifiers in C are short and informative names that uniquely identify
variables or function names.

These are user-defined words used for naming of functions, variables, structures, unions,
arrays etc. These can be composed of lowercase letters, uppercase letters, underscore or
digits, but the first character should be either an underscore or an alphabet.

CONSTANT: Constants are the variables whose values are fixed and can not be modified
during the execution of a program once they are defined. They are also known as literals.

SPECIAL CHARACTERS IN C: Special characters as the name suggests, are symbols in C


language that have special meaning and can not be used for any other purpose. Let us now
see their significance and purpose of use in C language.

Strings in C: Strings in C are represented as an array of characters having null character


'\0' at the end. This null character '\0' denotes the end of the string. Also, these strings are
always enclosed within double quotes. The size of the string is basically the number of
characters it contains and also, 1 byte memory space is always reserved for null character
value.

Operators: Operators in C are special symbols used to perform specific functions, and
data items on which they are applied upon are known as operands. C language provides a
wide range of operators that can be classified into 6 types based on their functionality:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators

You might also like