General Aspect of ‘C’
• C was originally developed in the 1970s, by Dennis Ritchie at
Bell Telephone Laboratories, Inc.
• C is a High level , general –purpose structured programming
language. Instructions of C consists of terms that are very
closely same to algebraic expressions, consisting of certain
English keywords such as if, else, for ,do and while.
• C contains certain additional features that allows it to be used
at a lower level , acting as bridge between machine language
and the high level languages.
• This allows C to be used for system programming as well as
for applications programming
The Character set of ‘C’
• C language consist of some characters set, numbers and
some special symbols.
• The character set of C consist of all the alphabets of English
language. C consist of
• Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
• The words formed from the character set are building
blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language.
• The following different types of token are used in C
• 1) Identifiers 2)Keywords 3)Constants
• 4) Operators 5)Punctuation Symbols
Calculations
• In any C program we typically do lots of calculations. The results of these
calculations are stored in computer’s memory.
• Like human memory, the computer’s memory also consists of millions of
cells.
• The calculated values are stored in these memory cells. To make the retrieval
andusage of these values easy, these memory cells (also called memory
• locations) are given names.
• Since the value stored in each location may change, the names given to these
locations are called variable names.
• Let us understand this with the help of an example.
Example
• Since the location whose name is x can hold different values at
different
• times x is known as a variable (or a variable name). As against this, 3
or 5 do not change, hence are known as constants.
• In programming languages, constants are often called literals,
whereas, variables are called identifiers.
Rules for Constructing Integer Constants
• (a) An integer constant must have at least one digit.
• (b) It must not have a decimal point.
• (c) It can be either positive or negative.
• (d) If no sign precedes an integer constant, it is assumed to be
positive.
• (e) No commas or blanks are allowed within an integer constant.
• (f) The allowable range for integer constants is -2147483648 to
• +2147483647.
Real constants
• Following rules must be observed while constructing real constants
expressed in fractional form:
• (a) A real constant must have at least one digit.
• (b) It must have a decimal point.
• (c) It could be either positive or negative.
• (d) Default sign is positive.
• (e) No commas or blanks are allowed within a real constant.
Rules for Constructing Character Constants
• (a) A character constant is a single alphabet, a single digit or a single
• special symbol enclosed within single inverted commas.
• (b) Both the inverted commas should point to the left. For example,
’A’
• is a valid character constant whereas ‘A’ is not.
Variables
• A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive. There are following basic variable
types −
Type Description
• char Typically a single octet(one byte). This is an integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• double A double-precision floating point value.
• void Represents the absence of type.
Rules for Constructing Variable Names
• (a) A variable name is any combination of 1 to 26 alphabets, digits or
• underscores. Some compilers allow variable names whose length
• could be up to 247 characters. Still, it would be safer to stick to the
• rule of 31 characters. Do not create unnecessarily long variable
• names as it adds to your typing effort.
• (b) The first character in the variable name must be an alphabet or
• underscore ( _ ).
• (c) No commas or blanks are allowed within a variable name.
• (d) No special symbol other than an underscore (as in gross_sal) can be
• used in a variable name.
Identifiers
• A 'C' program consist of two types of elements , user defined
and system defined.
• Identifiers is nothing but a name given to these elements.
• An identifier is a word used by a programmer to name a
variable , function, or label.
• Identifiers consist of letters and digits, in any order, except that
the first charecter or lable.
• Identifiers consist of letters and digits if any order,except that
the first charecter must be letter.
• Both Upper and lowercase letters can be used
Keywords
• Keywords are nothing but system defined auto double int struct
identifiers.
break else long switch
• Keywords are reserved words of the
language. case enum register typedef
• They have specific meaning in the language
and cannot be used by the programmer as char extern return union
variable or constant names
• C is case senitive, it means these must be const float short unsigned
used as it is
continue for signed void
• 32 Keywords in C Programming
default goto sizeof volatile
do if static while
Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special
meaning in C programming. For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
• For example: \n is used for newline. The backslash ( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler.Escape
Sequences Character
• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
What is main( )?
• main( ) forms a crucial part of any C program
• main( ) is a function. A function is nothing but a container for a set of
statements
• main( ) function always returns an integer value,
hence there is an int before main( ).
• The integer value that we are returning is 0. 0
indicates success.
• If for any reason the statements in main( ) fail to
do their intended work we can return a non-zero
number from main( ). This would indicate failure.
• Some compilers like Turbo C/C++ even permit us to return nothing
from main( ).
• In such a case we should precede it with the keyword void.
Types of Instructions
• There are basically three types of instructions in C:
• (a) Type Declaration Instruction – This instruction is used to declare
the type of variables used in a C program.
• (b) Arithmetic Instruction – This instruction is used to perform
arithmetic operations on constants and variables.
• (c) Control Instruction – This instruction is used to control the
sequence of execution of various statements in a C program.
Operators in C:An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.
C programming has wide range of operators to perform various
operations. For better understanding of operators, these
operators can be classified as:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
Arithmetic Operator
• Operator Meaning of Operator
•+ addition or unary plus
•- subtraction or unary minus
•* multiplication
•/ division
•% remainder after division( modulo
division)
C Assignment Operators
• An assignment operator is used for assigning a value
to a variable. The most common assignment operator
is =
• Operator Example Same as
•= a=b a=b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
C Relational Operators
• A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
• Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
• == Equal to 5 == 3 returns 0
•> Greater than 5 > 3 returns 1
•< Less than 5 < 3 returns 0
• != Not equal to 5 != 3 returns 1
• >= Greater than or equal to 5 >= 3 returns 1
• <= Less than or equal to 5 <= 3 return 0
Logical Operator
• Logical AND ( && )
• Logical OR ( || )
• Logical NOT ( ! )
1. Logical AND Operator ( && )
If both operands are non zero then the condition becomes true.
Otherwise, the result has a value of 0. The return type of the result is
int. Below is the truth table for the logical AND operator.
X Y X && Y
1 1 1
1 0 0
0 1 0
0 0 0
Logical OR Operator ( || )
The condition becomes true if any one of them is non-zero.
Otherwise, it returns false i.e., 0 as the value. Below is the truth
table for the logical OR operator.
X Y X || Y
1 1 1
1 0 1
0 1 1
0 0 0
Logical NOT Operator ( ! )
If the condition is true then the logical NOT operator will make it false and vice-
versa. Below is the truth table for the logical NOT operator.
X !X
0 1
1 0
conditional operator
• The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the
conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary
operator in C as it operates on three operands.
• Syntax of Conditional/Ternary Operator in C
• The conditional operator can be in the form
• variable = Expression1 ? Expression2 : Expression3;
• Or the syntax can also be in this form
• variable = (condition) ? Expression2 : Expression3;
• Working of Conditional/Ternary Operator in C
• The working of the conditional operator in C is as follows:
• Step 1: Expression1 is the condition to be evaluated.
• Step 2A: If the condition(Expression1) is True then Expression2 will be executed.
• Step 2B: If the condition(Expression1) is false then Expression3 will be executed.
• Step 3: Results will be returned.
Bitwise Operators
• 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise
operations in C.
• The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1
only if both bits are 1.
• The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any
of the two bits is 1.
• The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if
the two bits are different.
• The << (left shift) in C takes two numbers, the left shifts the bits of the first operand, and the second operand decides the
number of places to shift.
• The >> (right shift) in C takes two numbers, right shifts the bits of the first operand, and the second operand decides the
number of places to shift.
• The ~ (bitwise NOT) in C takes one number and inverts all bits of it.