Professional Documents
Culture Documents
Character set of C
A Character Set in a programming language defines the valid characters and symbols which can
be used to create a Program. These are grouped to form the expressions, statements and other
tokens a Language. The C supports a group of characters as listed below.
Digits 0-9
Special + -* / ^ \ () [] {}
= != <> $ , ; : % ! & ?
Characters _ # <= >= @
Formatting Space, backspace, horizontal tab, vertical tab, form feed,
Characters and carriage return
Tokens
A Token is a basic building block in a programming language. It is a group of characters that
logically belong together and can be processed by C compiler. C uses these types of tokens:
Identifiers, Keywords, Literals, Punctuators, and Operators.
Identifiers
It is a name which is used to represent variables, constants, arrays, functions, structure etc.
Following are the rules while defining identifiers:
Keywords
There are some reserved words in C which have predefined meaning to compiler called
keywords. These words may not be used as identifiers. Here is the list of all keywords predefined
by ANSI C.
1
Auto double int struct
Break else long switch
Case enum register typedef
Const extern return union
Char float short unsigned
continue for signed volatile
default goto sizeof void
Do if static while
Literals
A literal denotes a fixed value. In C, there are four types of literals:
1. Integer constant
A value written without fraction part is known as integer constant. Example: 25, -674, 0 etc.
2. Floating constant
A value written with fraction part is floating value. Value of this type can be written with or
without exponent form. Example: 2.34, -9.2154, 1.21E10
3. Character constant
A character constant in C can have one or at most two characters enclosed in single quotation
marks. For example 'A', '9', etc. C allows nongraphic characters which cannot be typed directly
from keyboard, e.g., backspace, tab, carriage return etc. These characters can be represented by
using an escape sequence.
Escape Sequences
Some commonly used escape sequences are as follows.
4. String Literals
A sequence of character enclosed within double quotes is called a string literal. String literal is
by default (automatically) added with a special character \0' which denotes the end of the
2
string. Therefore the size of the string is increased by one character. For example "COMPUTER"
will re represented as "COMPUTER\0" in the memory and its size is 9 characters.
Punctuators
These are also known as separators, are the tokens that serve different purposes based on the
context in which they are used. Some punctuators are used as operators; some are used to
demarcate a portion of the program and so on.
Data Types
While doing programming in any programming language, you need to use various variables to
store various information. Variables are nothing but a name given to reserved memory locations
to store values. This means that when you create a variable you reserve some space in memory.
These variables may be used to store information of various types like character, integer, floating
point, etc.
A data type specifies the possible values that an identifier can have and valid operations which
can be perform on those values. In order to store values in a variable first we need to associate it
with a specific data type. Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.
3
In C language data types are broadly classified as:
1. float
2. double
1. float
Float data type allows a variable to store decimal values.
Storage size of float data type is 4 bytes. This also varies depend upon the processor in the
CPU.
We can use up-to 6 digits after decimal using float data type. For example, 10.456789 can
be stored in a variable using float data type.
The range of float data type is 1.2E-38 to 3.4E+38
float keyword is used to refer float data type.
2. double
4
Double data type is also same as float data type which allows up-to 15 digits after decimal.
Storage size of double data type is 8 bytes.
The range for double data type is from 2.3E-308 to 1.7E+308.
double keyword is used to refer double data type.
4. void Type
Void means nothing or not known.
The void type specifies that no value is available.
The void keyword is used for void data type.
Array type
Pointer type
Function type
Structure
Union
Enumeration
Type Modifiers
A type modifier modifies the base type to yield a new type. It modifies the range and the
arithmetic properties of the base type. The type modifiers and the corresponding keywords
available in C are:
Signed (signed)
Unsigned (unsigned)
Short (short)
Long (long)
The following table shows the variable type, how much memory it takes to store the value in
memory, and what is maximum and minimum value which can be stored in such type of
variables.
Variable Definition in C
A variable definition tells the compiler where and how much storage is needed for the variable. A
variable definition specifies a data type, and contains a list of one or more variables of that type
as follows:
type variable_list;
Here, type must be a valid C data type and variable_list may consist of one or more identifier
names separated by commas. Some valid declarations are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables can be initialized (assigned an initial value) at definition time. The initializer consists
of an equal sign (known as assignment operator in C language) followed by a constant
expression as follows:
int a = 20;
Constants
A variable which does not change its value during execution of a program is known as a constant
variable. Any attempt to change the value of a constant will result in an error message. We can
declare a constant as shown below:
6
The above declaration means that PI is a constant of float types having a value 3.1415.
Examples of valid constant declarations are:
const int RATE = 50;
const float PI = 3.1415;
const char CH = 'A';
Type Conversion
When variables and constants of different types are combined in an expression then they are
converted to same data type. The process of converting one predefined type into another is
called type conversion. Type conversions can be implicit which is performed by the compiler
automatically, or it can be specified explicitly.
Implicit conversion
When the type conversion is performed automatically by the compiler without programmers
intervention, such type of conversion is known as implicit type conversion or type promotion.
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given below:
1. If either of the operand is of type long double, then others will be converted to long
double and result will be long double.
2. Else, if either of the operand is double, then others are converted to double.
3. Else, if either of the operand is float, then others are converted to float.
4. Else, if either of the operand is unsigned long int, then others will be converted to
unsigned long int.
5. Else, if one of the operand is long int, and the other is unsigned int, then
o if a long int can represent all values of an unsigned int, the unsigned int is
converted to long int.
o otherwise, both operands are converted to unsigned long int.
6. Else, if either operand is long int then other will be converted to long int.
7. Else, if either operand is unsigned int then others will be converted to unsigned int.
It should be noted that the final result of expression is converted to type of variable on left side
of assignment operator before assigning value to it.
Also, conversion of float to int causes truncation of fractional part, conversion of double to float
causes rounding of digits and the conversion of long int to int causes dropping of excess higher
order bits.
7
For example
double a;
int b = 5;
float c = 8.5;
a = b * c;
b gets converted to float before multiplying to c. After multiplication the result gets converted to
double because a is of type double.
Explicit conversion
Use the following syntax for explicit type casting
( new_type ) expression
Note: Type conversion is not permanent. It means you cannot change the data type
of a variable.
Input/Output (I/O)
ANSI standard has defined many library functions for input and output in C language. Functions
printf() and scanf() are the most commonly used to display out and take input respectively.
Format of printf()
printf(format_string, argument1, argument2,);
where format string specifies how output should be formatted. It is a string of characters that
contains characters and some elements (for example escape sequences and format specifiers)
that are to be replaced. Format specifiers contain conversion specifiers which are to be replaced
by arguments.
%i or %d Int
%c Char
%f Float
%s String
8
Format of scanf()
scanf (Format_specifier, & variable_ name1, & variable_ name2,)
The scanf () function scans a series of input fields (all characters up to the next white-space
character) one character at a time. If you dont use the & (Address Operator), you might get
undesired result. So you should be careful put it before variable name. A list of format specifier
is given below.
Comments
Comments are used to document code. It increases the readability of a program. They are not
the part of a program hence ignored by compiler. C supports two types of comments:
one line */
Expressions
In programming, an expression is any legal combination of operators (An operator is a symbol
that represents particular action) and operands (An operand specifies an entity on which an
operation is to be performed) that represents a value. For example, in the C language x+5 is an
expression.
9
Every expression consists of at least one operand and can have one or more operators. In
the expression
x + 5
x and 5 are operands, and + is an operator.
Expression Evaluation
Precedence of Operators
Operator precedence determines the order in which operators are evaluated. It determines the
grouping of terms in an expression. This affects how an expression is evaluated. Certain
operators have higher precedence than others; for example, the multiplication operator has
higher precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
The knowledge of operator precedence alone is not sufficient to evaluate an expression in case of
two or more operators are involved are of the same precedence. To determine which of these
operators will operate first, the associativity of these operators to be considered
Associativity of Operators
In an expression when several operators of the same precedence appear together, the operators
are to evaluated according to their associativity. An operator can either be left-to-right
associative or right-to-left associative. The operators with the same precedence always have the
same associativity. If operators are left-to-right associative they are applied in a left-to-right
order, i.e. the operator that appears towards the left will be evaluated first. If they are right-to-
left associative they are applied in a right-to-left order, i.e. the operator that appears towards the
right will be evaluated first.
Operators in C
The operators in C are classified on the basis of the following criteria.
The unary operators require only one operand; they perform various operations such as
incrementing/decrementing a value by one, or negating an expression.
10
Binary Operators
A binary operator operates on two operands. It requires an operand towards its left and right.
For example: * (multiplication operator), / (division operator) etc.
Ternary Operators
A ternary operator is an operator, which operates on three operands. Conditional operator (i.e.
?: ) is the only ternary operator available in C.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Miscellaneous Operators
Arithmetic Operators
There are following arithmetic operators supported by C language: Assume variable A holds 10
and variable B holds 20, then:
11
Operator Description Example
Checks if the values of two operands are equal or (A == B) is
==
not, if yes then condition becomes true. not true.
Checks if the values of two operands are equal or
(A != B) is
!= not, if values are not equal then condition becomes
true.
true.
Checks if the value of left operand is greater than
(A > B) is
> the value of right operand, if yes then condition
not true.
becomes true.
Checks if the value of left operand is less than the
(A < B) is
< value of right operand, if yes then condition
true.
becomes true.
Checks if the value of left operand is greater than
(A >= B) is
>= or equal to the value of right operand, if yes then
not true.
condition becomes true.
Checks if the value of left operand is less than or
(A <= B) is
<= equal to the value of right operand, if yes then
true.
condition becomes true.
Logical Operators
Logical operators are used to logically relate sub-expressions. There are following logical
operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
12
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operators supported by C language are listed in the following table. Assume variable
A holds 60 and variable B holds 13, then:
Operator Description
Function call
operator
(i.e. ())
Array Subscript
Operator
(i.e. [])
It returns the size of a variable. For example, sizeof(a),
Sizeof
where a is integer, will return 4.
Conditional operator. If Condition is true ? then it
Condition ? X : Y
returns value X : otherwise value Y
Comma operator causes a sequence of operations to be
, performed. The value of the entire comma expression is the
value of the last expression of the comma-separated list.
. (dot) and -> Member operators are used to reference individual members
(arrow) of classes, structures, and unions.
Casting operators convert one data type to another. For
Cast
example, int(2.2000) would return 2.
address of
It returns the address of an variable. For example &a;
operator (i.e. &)
will give actual address of the variable.
Pointer operator * is pointer to a variable. For example
*
*var; will pointer to a variable var.
14
Precedence and Associativity of C Operators
16