Unit-2 Final
Unit-2 Final
Unit-2 Final
Character Set
- Set of characters that are used to form words, numbers and expression in C is called
Ccharacter set.
- Characters in C are grouped into the following four categories:
1. Letters or Alphabets:
Uppercase alphabets → A…….Z
Lowercase alphabets → a…….z
2. Digits:
All decimal digits → 0, 1, 2, ........ ,9
3. Special characters:
, → comma ; → semicolon “→ quotation mark & → ampersand etc.
4. White spaces:
Blank spaces, horizontal tab, vertical tab etc.
C Tokens
- C tokens are the basic buildings blocks in C language which are constructed together
to write a C program.
- Each and every smallest individual unit in a C program is known as C tokens.
- C tokens are of six types.
3 C Programming Reference Note
Keywords
- Keywords are predefined words for a C programming language.
- All keywords have fixed meaning and these meanings cannot be changed.
- The keywords cannot be used as variable
names. E.g.
4 C Programming Reference Note
Identifiers
- Every word used in C program to identify the name of variables, functions, arrays,
pointers and symbolic constants are known as identifiers.
- These are user defined names consisting of arbitrarily long sequence of letters and digits
with either a letter or the underscore ( _ ) as a first character.
- There are certain rules that should be followed while naming C identifiers:
1. They must begin with a letter or underscore (_).
2. They must consist of only letters, digits, or underscore. No other special character is
allowed.
3. It should not be a keyword.
4. It must not contain white space.
5. It should be up to 31 characters long as only first 31 characters are significant.
6. Uppercase and lowercase letters are not interchagable.
E.g. Valid and Invalid identifiers
Constants
- A C constant refers to the data items that do not change their value during the program
execution.
- These fixed values are also called literals.
- Several types of C constants that are allowed in C are:
5 C Programming Reference Note
Integer constants:
- Integer constants are whole numbers without any fractional part. It must have at least one
digit and may contain either + or – sign. A number with no sign is assumed to be positive.
- There are three types of integer constants: Decimal integer constant, Octal integer
constant and Hexadecimal integer constant.
- A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for
decimal.
Decimal integer constant: 1, 3, 65, 5436664, -785
Octal integer constant: 037, 0, 0320, 0432
Hexadecimal integer constant: 0x4, 0X563, 0x1A
Real constants:
- The numbers having fractional parts are called real or floating point constants. These may
be represented in one of the two forms called fractional form or the exponent form and
may also have either + or – sign preceding it.
- Example of valid real constants in fractional form or decimal notation: 0.05, -0.905,
562.05, 0.015
Representing a real constant in exponent form:
- The general format in which a real number may be represented in exponential or scientific
form is: mantissa e exponent
- The mantissa must be either an integer or a real number expressed in decimal notation.
- The letter e separating the mantissa and the exponent can also be written in uppercase i.e.
E and, the exponent must be an integer.
- Examples of valid real constants in exponent form are: 252E85, 0.15E-10, -3e+8, 4.1e8
Character constants:
- A character constant contains one single character enclosed within single quotes.
E.g. ‘a’, ‘Z’, ‘5’
- It should be noted that character constants have numerical values known as ASCII values,
for example, the value of ‘A’ is 65 which is its ASCII value.
6 C Programming Reference Note
String constant:
- String constants are sequence of characters enclosed within double quotes.
- May contain letters, numbers, special characters or blank spaces.
- For e.g., “hello”, “abc”, “hello91”, “2077”
Symbolic constants
A symbolic constant is a name given to some numeric value, or a character constant or
string constant.
Defining symbolic constants:
1) Using pre-processor directive #define
Syntax: #define CONSTANT_NAME literal
e.g. #define PI 3.14159
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose.
[] () {} , ; : * . = #
Braces { }: These opening and ending curly braces marks the start and end of a block of code
containing more than one executable statement.
Parentheses ( ): These special symbols are used to indicate function calls and function
parameters.
Brackets [ ]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.
Escape Sequence
- An escape sequence is a non-printing characters used in C.
- These non-printing characters can be represented by using escape sequences represented
by a backslash(\) followed by one or more characters.
- Each sequence are typically used to specify actions such as carriage return, backspace,
line feed or move cursors to next line.
E.g.
7 C Programming Reference Note
#include <stdio.h>
#include <conio.h>
main()
{
printf("Hello\tworld!!\n"); Output:
printf("Hello!\n How are you?"); Hello world!!
getch(); Hello!
How are you?
return 0;
}
Delimiters
- A delimiter is a unique character or series of characters that indicates the beginning or
end of a specific statement, string or function body set.
- Delimiter examples include:
- Parentheses: ( )
- Curly brackets: { }
- Escape sequence or comments: /*
- Double quotes for defining string literals: “ ”
Data Types
- A data type is a type of data.
- Data type is a data storage format that can contain a specific type or range of values.
- Data type in C refers to an extensive system used for declaring variable for function of
different types. The type of a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted.
ANSI C supports three classes of data types:
Primary/fundamentals data types
User-defined data types
Derived data types
Defined as:
int a;
int x=5;
5. Void type:
The void type has no value.
This is usually used to specify a type of function when it does not return any value to
the calling function.
E.g. void main()
Conversion specifier
%d → integer #include <stdio.h>
%f → floating point int main()
%c → character {
%s → string char ch = 'A';
printf("%c\n", ch);
Input/Output return 0;
scanf()/printf() }
#include <stdio.h>
printf("%d",100); int main()
{
scanf("conversion specifier", &variable_name); float a = 12.67;
printf("%f\n", a);
if x is an integer variable. return 0;
scanf("%d",&x) }
10 C Reference Note
Programming
Variables
- A symbolic name which is used to store data item i.e. a numerical quantity or a character
constant.
- 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 same variable can store different value at different portion of a program.
- Variable name may consists of letters, digits or underscore characters.
- The rules for naming variables are similar to those of identifiers.
Variable Declaration:
- Any variable should be defined before using it in a program.
- The variable are declared using following syntax:
data_type variable_name1, variable_name2, ............. ;
E.g. int n1;
int v1, v2, v3;
char c;
float radius;
Preprocessor Directives
- Collection of special statements that are executed at the beginning of a compilation
process.
- Placed in the source program before the main function.
#include<stdio.h> //used for file inclusion
#define PI 3.1416 //defining symbolic constant PI
- These statements are called preprocessor directives as they are processed before
compilation of any other source code in the program.
Statement
- Statement is the complete direction to computer to perform specific task.
- In C, a statement is terminated by semicolon(;)
Types of statement:
1) Null statement: Does nothing.
;
2) Compound statement: Block of statement
{
…
… Compound statement
…
;
…
…
…
;
}
. What is statement? Explain the different types of statement?
Ans: A smallest executable entity within a program code is called a statement. An instruction
or one line of code written to do a specific task in a program is called programming
statement. Statements are the basic building blocks of c programming language.
Simple statement: A simplest executable entity is called statement. A simple statement
is a basic part of program and it is a single line expression which is used to carry out
assignment, calculation or to test logical decision.
Compound statement: A single instruction composed of two or more individual
instructions is called a compound statement. This type of statement is used to combing
two or more statement in on single line of code. This will shorten the written within a
program.
Control statement: A statement that affects the flow of execution through a program is
called control statement. Control statements are also known, as control structures in C
language. There are two types of control statements are also known as control
structures in C language. There are two types of control statements: selection and
iteration
Expression
- Combination of variable, constant, operators etc. on the basis of language
grammar.
- Every expression consists of at least on operand and can have one or more
operators.
- Operands are values and operators are symbols that represent particular
actions.
- E.g. a+b, a+b*c, a*b/3
Types of operator
C operators can be classified into following types:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
Conditional Operators
Bitwise Operators
Special Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations. There are five arithmetic
operators:
College Note
Division Rule:
- int/int = int
- float/float = float
- int/float = float
- float/int = float
Note: For modulo operator, the sign of the result is always the sign of the first operand.
E.g. 10%3=1, -10%3=-1, -10%-3=-1, 10%-3=1
College Note
/* Program to Perform Arithmetic Operations in C */
#include<stdio.h>
int main()
{
int a = 12, b = 3;
int add, sub, mul, div, mod;
add = a + b;
sub = a - b;
mul = a * b;
div = a / b;
mod = a % b;
printf("Addition of two numbers a, b is : %d\n", add);
printf("Subtraction of two numbers a, b is : %d\n", sub);
printf("Multiplication of two numbers a, b is : %d\n", mul);
printf("Division of two numbers a, b is : %d\n", div);
printf("Modulus of two numbers a, b is : %d\n", mod);
}
Relational Operators
- Relational operators are used to compare two operands and taking decisions based on
their relation.
- Result of relational expression is either True(1) or False(0).
- Relational operators are used in decision making and loops.
- Relational operators are:
College Note
Logical Operators
- Logical operators are used to compare logical and relational expression.
- The operands of logical operators must be either Boolean value (1 or 0) or expression that
produces Boolean value.
- The output of these operators is always 0 (flase) or 1 (true).
- The logical operators are:
// logical OR example
if (a > b || c == d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal to d\n");
return 0;
}
College Note
Assignment Operator
- Assignment operators are used to assign the result of an expression to a variable.
- The mostly used assignment operator is ‘=’.
- C also supports shorthand assignment operators which simplify operation with
assignment.
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a); //10
a += 10;
printf("Value of a is %d\n", a); //20
a -= 10;
printf("Value of a is %d\n", a); //10
a *= 10;
printf("Value of a is %d\n", a); //100
a /= 10;
printf("Value of a is %d\n", a); //10
return 0;
}
College Note
Pre-increment operator (++a): the value is incremented first and then the expression is
evaluated.
E.g. a= 10; b=++a; after this statement, a= 11, b = 11.
Post-increment operator (a++): the expression is evaluated first and then the value is
incremented.
E.g. a= 10; b=a++; after this statement, a= 11, b = 10.
Pre-decrement operator (- -a): the value is decremented first and then the expression is
evaluated.
E.g. a= 10; b=--a; after this statement, a= 9, b = 9.
Post-decrement operator (a- -): the expression is evaluated first and then the value is
decremented.
E.g. a= 10; b=a--; after this statement, a= 9, b = 10.
#include <stdio.h>
int main()
{
int a = 5;
int b = 6;
printf("a=%d, b=%d",a,b); //a=5, b=6
b=++a;
printf("a=%d, b=%d",a,b); //a=6,b=6
b=a++;
printf("a=%d, b=%d",a,b); //a=7,b=6
b=a--;
printf("a=%d, b=%d",a,b); //a=6,b=7
b=--a;
printf("a=%d, b=%d",a,b); //a=5, b=5
return 0;
}
if a>b
return 1;
else
E.g. (a>b) ? 1 : 0; return 0;
College Note
6 C Programming Reference Note
Q. Write a program to read two numbers from user and determine the larger number using
conditional (ternary) operator.
#include <stdio.h>
int main()
{
int n1, n2, larger;
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
larger = (n1>n2)?n1:n2;
printf("The larger number is %d", larger);
return 0;
}
Bitwise Operator
- Bitwise operators are used for manipulating data at bit level.
- These operators are used for testing the bits or shifting them to the left or to the right.
- Can be applied only to integer-type operands and not to float or double.
- Three types of bitwise operators:
(i) Bitwise logical operators
(ii) Bitwise shift operators
(iii) One’s compliment operator
E.g.
If a = 65, b=15
Equivalent binary values of 65 = 0100 0001; 15 = 0000 1111
7 C Programming Reference Note
0 0 1 1
8 C Programming Reference Note
Special Operators
Comma operator (,):
- The comma operator can be used link related expressions together.
- A comma-linked list of expression are evaluated from left-to-right and the value of the
rightmost expression is the value of the combined expressions.
E.g. X=(a=5, b=10, a+b);
- The first assign the value 5 to a
- Assign the value 10 to b
- Assign sum(a+b) to X
Sizeof operator
- It is used with an operand to return the number of bytes it occupies.
- The operand may be constant, variable or a data type qualifier.
E.g.
9 C Programming Reference Note
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
E.g.
#include <stdio.h>
int main()
{
int x = 13; // integer x
char c = 'a'; // character c
float sum;
x = x + c; // c implicitly converted to int. ASCII ('a'=97)
sum = x + 1.0; // x is implicitly converted to float
printf("x = %d, sum = %f", x, sum);
return 0;
}
2. Explicit Type Conversion:
- The type conversion performed by the programmer by posing the data type of theexpression of
specific type is known as explicit type conversion.
- The explicit type conversion is also known as type casting.
- Type casting in C is done in the following form:
(data_type)expression;
where, data_type is any valid C data type, and expression may be constant, variableor expression.
E.g.
#include<stdio.h> int main()
{
float a = 1.2; int b;
b = (int)a + 1; // a is explicitly converted to int type
printf("Value of a is %f\n", a); printf("Value of b
is %d\n",b); return 0;
}