You are on page 1of 16

Basic Concepts

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.

Letters A-Z, a-z

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:

An identifier can consist of alphabets, digits and/or underscores


It must not start with a digit
It must be different from a keyword
An identifier must be unique in its scope
C is case sensitive that is upper case and lower case letters are considered different from
each other. Thus, Earth and earth are two different identifiers in C
The maximum number of characters allowed in an identifier name is compiler dependent,
but the limit imposed by all the compilers provides enough flexibility to create
meaningful identifier names

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.

Value Escape sequence


Newline \n
horizontal tab \t
vertical tab \v
Backspace \b
carriage return \r
form feed \f
Alert \a
Backslash \\
single quote \'
double quote \"

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.

The following characters are used as punctuators in C.

Brackets Opening and closing brackets indicate single and


[ ] multidimensional array subscript.
Parentheses Opening and closing brackets indicate functions calls,;
( ) function parameters for grouping expressions etc.
Braces Opening and closing braces indicate the start and end of a
{ } compound statement.
Comma It is generally used as a separator in a function argument
, list.
Semicolon
It is used as a statement terminator.
;
Colon It indicates a labeled statement or conditional operator
: symbol.
Asterisk It is used in pointer declaration or as multiplication
* operator.
Equal sign
It is used as an assignment operator.
=
Pound sign
It is used as pre-processor directive.
#
Operators
An Operator is a special symbol with specific meaning, which is used for performing an
Operation. For example we know that + is used for adding two numbers.

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:

Basic data types (primitive data types)


Derived data types
User-defined data types

Basic data types in C

1. Integer data type

Integer data type allows a variable to store numeric values.


int keyword is used to refer integer data type.
The storage size of int data type is 2 or 4 or 8 byte.
It varies depend upon the processor in the CPU that we use. If we are using 16 bit
processor, 2 byte (16 bit) of memory will be allocated for int data type. Likewise, 4 byte
(32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor
is allocated for int data type.
int (2 byte) can store values from -32,768 to +32,767
int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.

2. Character data type

Character data type allows a variable to store character value.


Storage size of character data type is 1. We can store only one character using character
data type.
char keyword is used to refer character data type.

3. Floating point data type

Floating point data type consists of 2 types. They are,

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.

Derived Data Types


These data types are derived from the basic data types. Derives data types available in C are:

Array type
Pointer type
Function type

User Defined Data Types


In C language a user can also define its own data types which are known as user-defined data
types. The user-defined data types in C can be created by using:

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.

Type Storage size Value range


char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
5
-32,768 to 32,767 or -2,147,483,648 to
int 2 or 4 bytes
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
The sizes of variables might be different from those shown in the above table, depending on the
compiler and the computer you are using.

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:

type variable_name = value;


Initialization of variable
When we declare a variable it's default value is undetermined. We can declare a variable with
some initial value.

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:

const float PI = 3.1415;

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:

All short and char are automatically converted to int, then,

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.

Here is the list of some commonly used conversion specifiers:

Specifier Argument Type

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

Format Specifier Meaning

%d For taking integer value

%c For taking a character

%s For taking string

%f For taking float and double value

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:

Single Line Comment


A single line comment starts with two forward slashes (i.e. //) and is automatically terminated
with the end of the line. For example

// This is an example of single line comment

Multi Line Comment


A multi line comment starts with /* and ends with */. For example

/* This is an example of multi line comment

which spans more than

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.

1. The number of operands on which an operator operates

2. The role of an operator

Classification based on number of operands


Unary Operators

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.

Classification based on role of an operator


Based upon their role, C provides the following types of operators:

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:

Operator Description Example


A + B will give
+ Adds two operands
30
A - B will give
- Subtracts second operand from the first
-10
A * B will give
* Multiplies both operands
200
B / A will give
/ Divides numerator by de-numerator
2
Modulus Operator and remainder of after an B % A will give
%
integer division 0
Increment operator, increases integer value by
++ A++ will give 11
one
Decrement operator, decreases integer value by
-- A-- will give 9
one
Relational Operators
There are following relational 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:

Operator Description Example


Called Logical AND operator. If both the operands are (A && B) is
&&
non-zero, then condition becomes true. false.
Called Logical OR Operator. If any of the two operands (A || B) is
||
is non-zero, then condition becomes true. true.
Called Logical NOT Operator. Use to reverses the
!(A && B)
! logical state of its operand. If a condition is true,
is true.
then Logical NOT operator will make false.
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and
^ are as follows:

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 Example


Binary AND Operator copies a bit to
(A & B) will give 12 which is
& the result if it exists in both
0000 1100
operands.
Binary OR Operator copies a bit if (A | B) will give 61 which is
|
it exists in either operand. 0011 1101
Binary XOR Operator copies the bit
(A ^ B) will give 49 which is
^ if it is set in one operand but not
0011 0001
both.
(~A ) will give -61 which is
Binary Ones Complement Operator is
1100 0011 in 2's complement
~ unary and has the effect of
form due to a signed binary
'flipping' bits.
number.
Binary Left Shift Operator. The left
operands value is moved left by the A << 2 will give 240 which is
<<
number of bits specified by the 1111 0000
right operand.
Binary Right Shift Operator. The
left operands value is moved right A >> 2 will give 15 which is
>>
by the number of bits specified by 0000 1111
the right operand.
Assignment Operators
There are following assignment operators supported by C language:

Operator Description Example


C = A + B will
Simple assignment operator, Assigns values
= assign value of A +
from right side operands to left side operand
B into C
Add AND assignment operator, It adds right
C += A is equivalent
+= operand to the left operand and assign the
to C = C + A
result to left operand
Subtract AND assignment operator, It
C -= A is equivalent
-= subtracts right operand from the left operand
to C = C - A
and assign the result to left operand
13
Multiply AND assignment operator, It C *= A is equivalent
*= multiplies right operand with the left to C = C * A
operand and assign the result to left operand
Divide AND assignment operator, It divides
C /= A is equivalent
/= left operand with the right operand and
to C = C / A
assign the result to left operand
Modulus AND assignment operator, It takes
C %= A is equivalent
%= modulus using two operands and assign the
to C = C % A
result to left operand
C <<= 2 is same as C
<<= Left shift AND assignment operator
= C << 2
C >>= 2 is same as C
>>= Right shift AND assignment operator
= C >> 2
C &= 2 is same as C
&= Bitwise AND assignment operator
= C & 2
C ^= 2 is same as C
^= bitwise exclusive OR and assignment operator
= C ^ 2
C |= 2 is same as C
|= bitwise inclusive OR and assignment operator
= C | 2
Misc Operators
There are few other operators supported by C Language.

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

Symbol Type of Operation Associativity


[ ]
( )
.
Expression Left to right
>
postfix ++
postfix
prefix ++
prefix
sizeof
&
* Unary Right to left
+

~
!
typecasts Unary Right to left
*
/ Multiplicative Left to right
%
+
Additive Left to right

<<
Bitwise shift Left to right
>>
<
>
Relational Left to right
<=
>=
==
Equality Left to right
!=
& Bitwise-AND Left to right
^ Bitwise-exclusive-OR Left to right
| Bitwise-inclusive-OR Left to right
&& Logical-AND Left to right
|| Logical-OR Left to right
? : Conditional-expression Right to left
=
*=
/=
%=
+=
Simple and compound assignment Right to left
=
<<=
>>=
&=
^= |=
, Sequential evaluation Left to right
15
1. Operators are listed in descending order of precedence. If several operators appear on the
same line or in a group, they have equal precedence.

2. All simple and compound-assignment operators have equal precedence.

16

You might also like