Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

C Character Set

Characters are symbols. Every language has its specific set of characters to form words, statements, etc. C also has its character set which contains alphabets, digits, special characters, and white spaces.

Alphabets
C accepts both small and capital letters of the English language alphabets.
Small Letters: a b c ................ x y z
Capital Letters: A B C ................ X Y Z

Digits
C accepts 10 digits used to form numerical values.
Digits: 1 2 3 4 5 6 7 8 9 0

Special Character
C support various printable and control special characters.
Printable Special Characters: ~ ! @ # $ % ^ & * ( ) _ - + = [ ] { } | / \ ? < > , . ; : ' " ` etc.
Control Special Characters: NULL EOF BEL ESC DEL etc.

White Space
C supports various white space characters. White space is any character that represents horizontal or vertical space.
White Space: Space, Horizontal tab, Vertical tab, New line, Carriage return, Form feed, etc.

C Tokens

Tokens are the smallest individual units or basic building blocks which construct a C program. Each instruction or statement of the C program is a collection of these tokens. C supports the following six types of tokens:
1. Keywords
2. Identifiers
3. Constants
4. Operators
5. Special Symbols

Keywords

Keywords are predefined reserved words having special meanings to the compiler. These meanings cannot be changed in any circumstances so that keywords can not be used as identifiers. C has 32 reserved keywords. As C is case sensitive language, all keywords must be written in lowercase letters. Following is the list of keywords used in C language with their uses:
Keyword
Uses
auto
Used to represent automatic storage class.
break
A control statement used to terminate the switch-case or loop statement.
case
Used to represent an option in switch-case control statement.
char
Used to represent character data type.
const
Used to define a constant.
continue
A control statement used to pass the control at the beginning of loop.
default
Used to represent the default option in switch-case statement.
do
Used to define the do block in do-while loop statement.
double
Used to represent a double data type.
else
Used to define else block in an if-else statement.
enum
Used to define the enumeration data type.
extern
Used to represent extern storage class.
float
Used to represent the floating-point data type.
for
Used to define the for loop statement.
goto
A control statement used to pass the control at the label.
if
Used to define if-else conditional control statement.
int
Used to represent integer data type.
long
Used to represent long size qualfier.
register
Used to represent register storage class.
return
Used to terminate the execution of a function and return a value to its calling function.
short
Used to represent short size qualifier.
signed
Used to represent signed sign qualifier.
sizeof
Used to represent the sizeof operator which gives the memory size of a variable.
static
Used to represent static storage class.
struct
Used to define the structure data type.
switch
Used to define the switch-case control statement.
typedef
Used to assign an alternate name to the existing data types.
union
Used to define the union data type.
unsigned
Used to represent unsigned sign qualifier.
void
Used to represent nothing as return value, function parameter etc.
volatile
Used to represent volatile qualifier.
while
Used to define the while loop statement.

Identifiers

Identifiers are names given to the program entities such as variables, functions and user-defined data types, etc. Each of these entities is stored at unique addresses in computer memory. Identifiers give us the advantage to access these entities by identifiers instead of memory address and compiler keep track of where they are physically stored in memory. Different programming languages use different rules to form identifiers. Rules to form identifiers in C are very simple.

Rules to form an identifier in C
1. Keywords could not be used as identifiers.
2. The first character of an identifier must be an alphabet or underscore.
3. An identifier must be unique.
4. Identifier written in different cases are different i.e. num, Num and NUM are different.
5. An identifier can be any combination of alphabets, digits, and underscores.
6. Two successive underscores are not allowed.
7. No special character or white space other than underscore is allowed to form an identifier.
8. There is no limit for the length of an identifier. However, only the first 31 characters are significant.

Typically, it is discouraged to start an identifier with an underscore because many of the identifiers in C system libraries start with an underscore. We should make sure that our names do not duplicate the system names. Good identifier names are meaningful, descriptive, easy to understand and short. We often use some abbreviations to make them short. For example, the name student could be abbreviated as stdnt. In this example, we remove all the vowels used in word student.

Variables
Variables are the names given to the memory locations used to store data values of different types. These values can be changed during the execution of the program. Each C variable has a specific type that defines the size and layout of the memory.

Variable Declaration
Following are the valid syntax to declare a C variable:
data_type variable_name;
or
data_type variable_name = value;
or
data_type variable_name_1, variable_name_2, ... variable_name_n;

Constants

Constants or Literals are the data values that cannot be altered during the execution of the program. These are the fundamental and essential parts of C programming. Following is the classification of typically used constants:
Classification of C Constants

Integer Constants: An integer constant is a whole number without any fractional or exponential part. It may be positive or negative identified by prefixed plus or minus sign. However, plus sign is optional for positive integers. Integer constants are further classified into three parts:
  • Decimal Integer (Base 10 numbers): These numbers are decimal numbers uses 10 digits from 0 to 9.
    Example: +13, -395, 2893 etc.
  • Octal Integer (Base 8 numbers): These numbers are octal numbers uses 8 digits from 0 to 7. These numbers represented by leading 0.
    Example: 025, -0143, +044 etc.
  • Hexadecimal Integer - These numbers are hexadecimal numbers uses 10 digits from 0 to 9 and 6 English alphabets from A to F or a to f. These numbers represented by leading 0x or 0X.
    Example: 0x1A, 0XAF7, 0xcb etc.

Floating Point or Real Constants: A floating-point number has a fractional or exponential part. It can also be positive or negative identified by prefixed plus or minus sign. Plus sign is optional for positive numbers. The exponential part is represented by using the English alphabet e or E.
Example: 3.1416, +9.8, -32.86, 1.625E5, -3.21e7 etc.

Character Constants: Characters are symbols. A character constant is simply a single character enclosed with a pair of single quotes. Each character has a specific integer value known as ASCII (American Standard Code for Information Interchange) value. It should be noted that digits '0' to '9' as character and numbers 0 to 9 are different.
Example: 'P', 'x', '3', '#' etc.

String Constants: String is the sequence of characters enclosed with a pair of double-quotes. A string can have any number of alphabets, digits, special symbols, and white spaces. We will learn the string in detail later.
Example: "Hello Students!", "5 x 2 = 10", "S", "" etc.
It should be noted that "S" and 'S' are different. "S" is a string and 'S' is a character. A string that has no character is called null string "".

Defining a C Constant: In C, there are the following two methods to define a constant:

  • Using 'const' keyword: In this method, we declare a variable with the initial value as constant and prefix the 'const' keyword in the declaration. This converts a variable into constant. It contains all the properties of a variable but the program will give a compile-time error if we try to change the value of the variable.
    const data_type variable_name = constant_value;
    Example: const float g = 9.8;
  • Using '#define' macro: In this method, we use #define preprocessor directive to define a constant.
    #define identifier constant_value
    Example: #define PI 3.1416

Operators

Operators are some special symbols that are used to perform specific mathematical or logical operations. We will discuss operators in detail later.
Example: +, -, /, *, =, <, >, >>, << etc.

Special Symbols

Special symbols have predefined meanings for the C compiler. They are used in C program to perform some special tasks.
Example: { } [ ] ( ) , ; etc.

Escape Sequences

Escape sequences are special characters, which have special meanings in C. These characters are impossible to type or add in a string through keyboard like newline, horizontal tab, vertical tab, backspace, etc. For this purpose, a set of backslash characters are included in C. Backslash character '/' escape the normal interpretation of these characters by the compiler.
Example:
printf("Digital\nJeevan");

Output:
Digital
Jeevan
In the above example, '\n' is interpreted as a new line by the compiler. Following is the list of the escape sequences.
Escape Sequence
Character Represented
\a
Alert (Beep/Bell)
\b
Backspace
\f
Form Feed Page Break
\n
New Line
\r
Carriage Return
\t
Horizontal Tab
\v
Vertical Tab
\”
Double Quotation Mark
\’
Apostrophe or Single Quotation Mark
\?
Question Mark
\\
Backlash
\/
Forward Slash
\0
Null Character

No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib