Unit 1 C notes
Unit 1 C notes
Algorithm :
Flowchart
A Flowchart helps us graphically visualize the flow of control within the sequence of
statements. Or It is diagrammatical/graphically representation of given algorithm.
Symbol Purpose Description
On-page Connector This symbol is used to transfer the flow of control from
one point on a page to another point on a different
page. This notation is useful when the flowchart spans
more than one page. (Next Page)
Off-page Connector This symbol is used to transfer the flow of control from
one point to another within a page. A connector isusually
labelled to match with its counterpart. (Same Page)
Structured Programming:
Structured programming is a programming paradigm that emphasizes organizing code into
logical, well-structured modules, each with a single entry and exit point. It focuses on:
Modular Programming:
Modular programming takes structured programming a step further by emphasizing the
separation of concerns and independent modules. It involves:
1. Modular design: Breaking down a program into independent, reusable modules, each
with its own interface and implementation.
Introduction of C :
The root of all modern languages is ALGOL (Algorithmic Language). ALGOL was the first
computer programming language to use a block structure, and it was introduced in 1960. In
1967, Martin Richards developed a language called BCPL (Basic Combined Programming
Language). BCPL was derived from ALGOL. In 1970, Ken Thompson created a language
using BCPL called B. Both BCPL and B programming languages were typeless.
After that, C was developed using BCPL and B by Dennis Ritchie at the Bell lab in 1972. So,
in terms of history of C language, it was used in mainly academic environments, but at long
last with the release of many C compilers for commercial use and the increasing popularity of
UNIX, it began to gain extensive support among professionals.
Importance of C
1. C is a high level language.
8. C is a portable
Basic structure of c programs
1. Documentation Section
This section consists of the description of the program, the name of the program, Anything
written as comments will be treated as documentation of the program.
Example:
#include<stdio.h>
#include<math.h>
Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program.
3. Definition Section
The definition section defines all symbolic constants. A symbolic constant is a constant value given
to a name which can't be changed in program.
Example: #define PI 3.14
4. Global Declaration
The global declaration section contains global variables, function declaration, and static
variables. Variables and functions which are declared in this scope can be used anywhere in
the program.
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main()
function.
{
return x+y;
}
Sample of C Programs :
Example 1: A program to print one line of text.
Code: #include<stdio.h>
Void main()
{
/* …… printing begins…….*/
printf(“I see, I remember.”);
/*……..printing ends………*/
}
Output : I see, I remember.
{
int num1=7, b=num2, num3=6,sum, average;
printf(“Enter first number :");
printf("%d\n",num1);
printf("%d\n",num3);
sum=num1+num2+num3;
average =sum/3;
The process of compiling and running a C program involves entering the code, checking
for errors, translating the code into machine language, linking with necessary libraries,
providing input data, executing the program, and verifying the output. This sequence
ensures that the program is syntactically correct, logically sound, and produces the desired
results.
• Source code is the file containing the c program. The program input to a compiler.
The compiler is a system software that translate the source program to object code.
• A given c program may make use of several library functions. The object code of the
program would have to be linked to the object codes of the library functions.
• The linker is a system software that links together all the object codes to generate the
final executable code.
• The executable code is submitted to the operating system for execution whenever
required.
C Tokens
In passage of text, individual words and punctuation marks are called Tokens.
In a C program the smallest individual units are known as C Tokens. C has six types of
tokens. They are
1. C Token – Keywords
• The keywords are pre-defined or reserved words in a programming language.
• Each keyword is meant to perform a specific function in a program. Since keywords
are referred names for a compiler, they can‟t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword which is not allowed.
• You cannot redefine keywords. However, you can specify the text to be substituted for
keywords before compilation by using C preprocessor directives.
C language supports 32 keywords which are given below:
2. C Token – Identifiers
• Identifiers are used as the general terminology for the naming of variables, functions,
and arrays.
• These are user-defined names consisting of an arbitrarily long sequence of letters and
digits with either a letter or the underscore(_) as a first character.
• Identifier names must differ in spelling and case from any keywords.
• You cannot use keywords as identifiers; they are reserved for special use. Once
declared, you can use the identifier in later program statements to refer to the associated
value.
3. C Token- Constants :
The quantity which does not change during the execution of a program is known as constant.
The constants refer to the variables with fixed values. They are like normal variables but with
the difference that their values can not be modified in the program once they are defined.
Constants may belong to any of the data types.
Examples of Constants in C
const int c_var = 20;
Types of C constants.
1. Integer Constants: Whole numbers, either positive, negative, or zero, without a fractional
part.
Example: 123, -456
• Hexadecimal Constants : Hexadecimal integer constants are integer constants
having sequence of digits preceded by 0x or 0X. They may also include
alphabets from A to F representing numbers 10 to 15.
2. (Real) Floating-Point 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 .
Example: 3.14, -0.5, -247.32, +121.44
4. String Constants : String constants are sequence of characters enclosed within double
quotes.
Example: "hello", "goodbye“,”1987”, “College”, “India”
Escape Characters/ Escape Sequences
C allows us to have certain non graphic characters in character constants. Non graphic
characters are those characters that cannot be typed directly from keyboard, for
example, tabs, carriage return, etc.
These non graphic characters can be represented by using escape sequences
represented by a backslash() followed by one or more characters.
Here are some common character constants:
1. Newline: \n
2. Tab: \t
3. Carriage Return: \r
4. Backslash: \
5. Single Quote: „/”
6. Double Quote: „/””
7. Bell: \a
8. Form Feed: \f
9. Vertical Tab: \v
4. C Token – variables
The quantity that changes during the execution of a program is called a variable. A variable
is nothing
but a name given to a storage area that our programs can manipulate.
Syntax: datatype variablename;
Ex : int sum;
Examples: a, b, sum, si, age, city etc..
A variable is a named storage location that holds a value. Variables allow you to store,
manipulate, and reuse data in your code.
Variable names may consist of letters, digits, and the underscore(_) character, subject
to the following
• They must begin with a letter.
• Uppercase and lowercase letters are significant. That variable Total is not same as
total or TOTAL.
[] () {} , ; : * … = #
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.
The integer datatype in C is used to store the integer numbers (any number including
positive, negative and zero without decimal part). Octal values, hexadecimal values,
and decimal values can be stored in int data type in C.
The integer data type can also be used as short int and long int.
short int
short int:
int
syntax: short int var_name;
int: long int
size: 1 byte
format Specifier: %hd syntax: int var_name;
size: 2 bytes
format Specifier: %d
long int:
Declaration of variables :
1. Pre defined type declaration
Basic Syntax: Datatype variable_name;
Examples:
1. int x; // declares an integer variable x
2. float y; // declares a floating-point variable y
3. char c; // declares a character variable c
4. double z; // declares a double-precision floating-point variable z
Unary Operators
Unary operators operate on a single operand. They perform operations such as incrementing a
value, negating it, or performing logical NOT.
Binary Operators
Binary operators operate on two operands. They perform operations such as addition,
subtraction, multiplication, etc.
Ternary Operator
The ternary operator, also known as the conditional operator, operates on three operands. It is
a shorthand for the if-else statement
1. Arithmetic operators :
• Addition (+): The addition operator adds two or more numbers together.
Example: a+b = 5 + 3 = 8.
• Subtraction (-): The subtraction operator subtracts one number from another.
Example: a-b = 10 - 4 = 6.
• Multiplication (*): The multiplication operator multiplies two or more numbers
together.
Example: a*b = 6 * 9 = 54.
• Division (/): The division operator divides one number by another, resulting in a
quotient.
Example: a/b = 20 / 4 = 5.
• Exponentiation (^ or *): The exponentiation operator raises a number to a power.
Example: a^b = 2 ^ 3 = 8 or pow(base, exponential)
#include <stdio.h>
#include<math.h>
int main()
{
int num1 = 10, num2 = 4, sum, sub, mul, div, power;
Output
num1 is 10 and num2 is 4
num1+num2 is 14
num1-num2 is 6
num1*num2 is 40
num1/num2 is 2
num1^ num2 is ^10000
2. Relational operators in C:
These are used to compare two operands given in an expression. They define the relationship
that exists between two constants or variables. Results in either TRUE or FALSE.
There are six relational operators in C
Less than: The ‘< ’ operator compares two operands. It compares less than or not.
For example, x<y. 10<5, result is false.
• Greater than: The ‘>’ operator compares two operands. It compares greater
than or not.
For example, x>y. 20>5,, result is true.
• Less than or equal to: The ‘<=’ operator compares two operands. It compares less
than or equal to or not.
For example, x<=y. 15<=5,, result is false.
• Greater than or equal to: The ‘>=’ operator compares two operands. It compares
greater than or equal to or not.
For example, x>=y. 15>=5,, result is true.
• Equal to: The ‘==’ operator compares two operands. It compares equal to or not.
For example, x==y. 15==5,, result is false.
• Not Equal to: The ‘!=’ operator compares two operands. It compares equal to or not.
For example, x!=y. 15!=5,, result is true.
// C program to demonstrate working of relational operators
#include <stdio.h>
int main()
{
int num1 = 10, num2 = 4;
return 0;
}
Output :
num1 is greater than num2
num1 is greater than or equal to num2
num1 is greater than or equal to num2
num1 is greater than num2
num1 and num2 are not equal
num1 is not equal to num2
3. Logical operators
This class of C operators is used to make decisions. Logical operators are used to evaluate
two or more conditions. The result of these operators is either TRUE or FALSE.
&& operator – If clause‖ becomes true only when both conditions if (a > 0 && b > 0) is
true. Else, it becomes false.
// C program for Logical AND Operator
#include <stdio.h>
int main()
{
printf("Both values are greater than 0\n");
}
else
{
return 0;
}
• Logical OR operator:
The logical OR operator is used to perform OR operation on two logical operands. The result
of logical OR is false, when both the operands are false. Otherwise the answer is True.
|| Operator – ―if clause‖ becomes true when any one of the condition if (a > 0 || b > 0) is
true. It becomes false when none of the condition is true.
// C program for Logical OR Operator
#include <stdio.h>
int main()
{
int num1 = -1, num2 = 20;
if (num1> 0 || num2> 0)
else
{
return 0;
}
Output : Any one of the given value is greater than 0
X !X
0 1
1 0
// C program for Logical NOT Operator
#include <stdio.h>
int main()
{
int num1 = 10, num2= 20;
if (!(num1 > 0 && num2 > 0))
{
// condition returned true but
// logical NOT operator changed
// it to false
printf("Both values are greater than 0\n");
}
else
{
printf("Both values are less than 0\n");
}
return 0;
}
Output: Both values are less than 0
4. Assignment Operator:
Examples:
sum = a + b;
a +=b;
//C program to demonstrate working of Assignment operators
#include <stdio.h>
int main()
{
int num1= 10;
int num2= 5;
num1 += num2; // num1 = num1 + num2, now num1 is 15
printf("num1 += num2: %d\n", num1);
#include <stdio.h>
int main()
{
int num=1;
while(num<10)
{
printf("%d ",num);
num++;
}
}
Output: 1 2 3 4 5 6 7 8 9
Output: 1 2 3 4
Output: 1 2 3 4 5
Output: 9 8 7 6
Output: 9 8 7 6 5
6. Ternary (Conditional) Operator:
This is used to test the relationship between two variables.
The conditional operator takes three operands.
The symbol „?‟ is used as a conditional operator in C.
The general form: Where,
7. Bitwise Operators:
All data items are stored in the computer‟s memory as asequence of bits (0‟s and 1‟s).
C provides six bitwise operators.
• The result of a bitwise AND is a 1 when both the bits are 1. Otherwise, it is a
zero.
• The result of a bitwise OR is a 0 when both the bits are 0. Otherwise,
• it is 1.
• The result of a bitwise XOR is a 1, if the bits are different (1 and 0).
• Otherwise, it is a 0 (when both are 0‟s and both are 1‟s).
• The bitwise complement operator is an unary operator that reverses the state of
each bit within an integer or a character.
• The left shift operator shifts the bits towards left.
• The right shift operator shifts the bits towards right.
Left Shift (<<) Operators
The left shift(<<) is a binary operator that takes two numbers, left shifts the bits of the first
operand, and the second operand decides the number of places to shift. In other words, left-
shifting an integer “a” with an integer “b” denoted as „(a<<b)’ is equivalent to multiplying
a with 2^b (2 raised to power b).
Synatx: a<<b
Example: Let‟s take a=5; which is 101 in Binary Form. Now, if “a is left-shifted by 2”
i.e a=a<<2 then a will become a=a*(2^2). Thus, a=5*(2^2)=20 which can be written
as 10100.
Synatx: a>>b
Example: let‟s take a=5; which is 101 in Binary Form. Now, if “a is right-shifted by 2” i.e
a=a>>2 then a will become a=a/(2^2). Thus, a=a/(2^2)=1 which can be written as 01.
8. Special Operator in C
Below are some of special operators that C language offers.
S No Operators Description
:
There are three types of expression.
1) Arithmetic expression
2) Relational expression
3) Logical expression
1. Arithmetic expression
An expression involving arithmetic operators is called arithmetic expression.
Evaluation of expression
At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then the
arithmetic expression is evaluated from left to right.
There are two priority levels of operators in C.
High priority: * / %
Low priority: + -
The evaluation procedure of an arithmetic expression includes two left to right passes through
the entire expression. In the first pass, the high priority operators are applied as they are
encountered and in the second pass, low priority operations are applied as they are encountered.
Suppose, we have an arithmetic expression as:
x = 9 – 12 / 3 + 3 *2 - 1
Step 3: x = 10
But when parenthesis is used in the same expression, the order of evaluation gets changed.
2. Relational expression
A relational expression can be defined as a meaningful combination of operands and
relational operators.
3. Logical expression:
Expressions involving the logical operators are called the logical expressions.
In the C programming language, implicit type conversion refers to the compiler's conversion
of one data type into another data type while the program is being executed. Another name
for it is automated type conversion.
#include <stdio.h>
int main()
{
int num1= 10; // integer x
char num2 = 'a'; // character c
// num2 implicitly converted to int. ASCII
// value of 'a' is 97
num1 = num1 + num2;
// num1 is implicitly converted to float
float num3 = num1+ 1.0;
printf("num1 = %d, z = %f", num1, num3);
return 0;
}
Output : num1= 107,
num3 = 108.000000
Mathematical Functions :
C provides a large number of library functions which perform specific operations.
Mathematical functions are also included among these library functions.