Full Unit 1 Notes
Full Unit 1 Notes
SRM Institute of Science and Technology, SRM Institute of Science and Technology,
Chennai Chennai
types: integer, floating point Non-Numeric Data types: char and string - L value and R 2. Maintainability
3. Efficient
value in expression, Increment and decrement operator - Comma, Arrow and Assignment
4. Reliable
operator, Bitwise and Size-of operator - Arithmetic, Relational and logical Operators -
5. Machine Independence
Condition Operators, Operator Precedence - Expressions with pre / post increment operator 6. Cost Effectiveness
7. Flexible
In the global, we have about 500+ programming languages with having their own syntax and features. • Developed by Alick Glennie
1952 Autocode • It is the first compiled computer programming language
• Ex: FORTRAN, COBOL
In the early days, Charles Babbage had made the device, but he was confused about
how to give instructions to the machine, and then Ada Lovelace wrote the instructions • Developed by IBM
1883
for the analytical engine. 1957 FORTRAN • Numeric computation and scientific computing
• Software for NASA was written in FORTRAN
• It is a type of low-level language • ALGOrithmic language
• AL can be easily understandable by machine • It is the fundamental for C, C++ and Java
1949 Assembly
• It is also used to create computer viruses 1958 ALGOL
• The first PL to have a code block like “Begin” that indicates that your program has
Language
• Real time programs such as simulation of flight navigation system and medical started and “End” means you have ended your code
equipment.
• It stands for Common Business Oriented Language
1959 COBOL
• In 1997, 80% of the world business ran on COBOL
• The language is very easy to understand. Famous language among data scientists
1991 Python
and analysts.
• C#(C-sharp) is mainly used for making games. Unity engine uses C# for making
2000 C#
amazing games for all platforms
Writing Algorithms and Pseudo Code Writing Algorithms and Pseudo Code
An algorithm is a set of steps designed to solve a problem or accomplish a task. Algorithms are usually Characteristics of a good algorithm
written in pseudocode, or a combination of your speaking language and one or more programming languages, 1: Input and output must be specified
in advance of writing a program. 2: All important steps must be mentioned
Step-1: Obtain detailed information on the problem. 3: Instructions must be perfectly ordered
Step-2: Analyze the Problem 4: Short and effective descriptions
Step-3: Think of a problem solving approach 5: The algorithm must contain finite number of steps
Step-4: Review the problem solving approach and try to think of a better alternative
Step-5: Develop a basic structure of the algorithm
Step-6: Optimize, improve and refine.
Writing Algorithms and Pseudo Code Writing Algorithms and Pseudo Code
Examples of Algorithm Examples of Algorithm
1. Addition of two numbers 2. Comparison of 3 numbers to find the largest number
1: Start
Step-1: Start 2: Declare variables num1, num2, and num3
Step-2: Declare variables num1, num2, and sum 3: Read values of num1 and num2 and num3
4: Compare num1, num2 and num3
Step-3: Read values of num1 and num2 5: If num1>num2 and num1>num3
Step-4:Add the values of num1 and num2 and assign the result Display num1 is the largest number
Else
Step-5: Display the sum If num2>num1 and num2>num3
Step-6: End Display num2 is the largest number
Else
Display num3 is the largest number
6: End
Single line and Multi line comments Single line and Multi line comments
❖ C is a procedure oriented programming language. Dennis Ritchie invented it in the year 1972. It
was created primarily as a system programming language for creating an operating system.
❖ The main features of the C language include low-level memory access, a simple set of keywords,
and a clean style, these features make C language suitable for system programming like an operating
system or compiler development.
❖ Many advanced programming languages have borrowed syntax/features directly or indirectly from
the C language. The languages like Java, PHP, Java script and many other programming languages
are mainly based on the C language.
❖ Documentation section
❖ Preprocessor section
❖ Definition section
❖ Global declaration
❖ Main function
VARIABLES VARIABLES
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can Rules for defining variables:
be reused many times. ❖ A variable can have alphabets, digits, and underscore.
It is a way to represent memory location through symbol so that it can be easily identified. ❖ A variable name can start with the alphabet, and underscore only. It can't start with a digit.
Syntax: ❖ No whitespace is allowed within the variable name.
datatype variable_name; ❖ A variable name must not be any reserved word or keyword, e.g. int, float, etc.
int a; Valid variable names:
float b; int a;
char c; int _ab;
Here, a, b, c are variables. The int, float, char are the data types. We can also provide values while
int a30;
declaring the variables as given below:
Invalid variable names:
int a=10,b=20;//declaring 2 variable of integer type
int 2;
float f=20.8;
int a b;
char c='A';
int long;
VARIABLES VARIABLES
VARIABLES VARIABLES
VARIABLES IDENTIFIER
automatic variable:
"Identifiers" or "symbols" are the names you supply for variables, types, functions, and labels in your program.
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly
Identifier names must differ in spelling and case from any keywords. You can't use keywords (either C or
declare an automatic variable using auto keyword.
Microsoft) as identifiers; they're reserved for special use. Identifiers must be unique. They are created to give a
unique name to an entity to identify it during the execution of the program. For example:
void main(){
int money;
int x=10;//local variable (also automatic)
double accountBalance;
auto int y=20;//automatic variable
}
Here, money and accountBalance are identifiers
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because
int is a keyword.
IDENTIFIER Constant
A constant is a value or variable that can't be changed in the program. C Constants is the most
Rules for Naming Identifiers fundamental and essential part of the C programming language. Constants in C are the fixed values that
❖ A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. are used in a program, and its value remains the same during the entire execution of the program.
❖ The first letter of an identifier should be either a letter or an underscore. ❖ Constants are also called literals.
❖ You cannot use keywords like int, while etc. as identifiers. ❖ Constants can be any of the data types.
❖ There is no rule on how long an identifier can be. However, you may run into problems in some ❖ It is considered best practice to define constants using only upper-case names.
compilers if the identifier is longer than 31 characters. Two ways to define constant in C
1. const keyword
2. #define preprocessor
Constant Constant
Output:
Note: If we try to change the value of PI, it will throw compile time error.
Compile Time Error: Cannot modify a const object
Constant Constant
Constant Constant
Constant KEYWORDS
Scope Scope
Local Variables:
A block or a region where a variable is declared, defined and used and when a block or a region ends, Variables that are declared within the function block and can be used only within the function are called
variable is automatically destroyed. local variables.
#include <stdio.h>
Local Scope or Block Scope
int main()
{ A local scope or block is a collective program statement placed and declared within a function or block (a
int var = 34; // Scope of this variable is within main() function only.
specific area surrounded by curly braces). C also has a provision for nested blocks, which means that a
// Therefore, called LOCAL to main() function.
printf("%d", var); block or function can occur within another block or function. So it means that variables declared within a
return 0;
block can be accessed within that specific block and all other internal blocks of that block but cannot be
}
accessed outside the block.
Scope Scope
Example (Local Variable): Example (Global Variable):
#include <stdio.h> #include <stdio.h>
int main () int z; //global variable
{ int main ()
//local variable definition and initialization {
int x,y,z; //local variable definition and initialization
//actual initialization int x,y;
x = 20; //actual initialization
y = 30; x = 20;
z = x + y; y = 30;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z); z = x + y;
return 0; printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
} return 0;
}
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
Types Description
They are arithmetic types and are further classified into: (a) integer types and
Basic Types
(b) floating-point types.
Enumerated They are again arithmetic types, and they are used to define variables that
Types can only assign certain discrete integer values throughout the program.
They include (a) Pointer types, (b) Array types, (c) Structure types, (d)
Derived Types
Union types and (e) Function types.
OPERATORS
Operator Precedence
❖ The precedence of operators in C indicates the order in which the operators will be evaluated in the An operator is a symbol that operates on a value or a variable.
❖ Associativity, on the other hand, defines the order in which the operators of the same precedence will A symbol that instructs the compiler to perform specific mathematical or logical functions is known as an
be evaluated in an expression. Also, associativity can occur from either right to left or left to right. operator.
❖ The precedence of operators determines which operator is executed first if there is more than Types of Operators
1. Unary Operator
one operator in an expression.
2. Arithmetic Operator
3. Relational Operator
4. Logical Operator
5. The ternary or conditional operator
6. Increment and Decrement operator
7. Bitwise Operator
8. Comma Operator
9. Sizeof Operator
OPERATORS OPERATORS
Arithmetic Operator
An arithmetic operator performs mathematical operations on numerical values such as addition,
subtraction, multiplication, and division (constants and variables).
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
OPERATORS OPERATORS
OPERATORS OPERATORS
OPERATORS OPERATORS
OPERATORS OPERATORS
OPERATORS OPERATORS
OPERATORS OPERATORS
OPERATORS OPERATORS
❖ Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short,
long. Operator Description Example
❖ Bitwise operators operate on individual bits of integer (int and long) values.
❖ If an operand is shorter than int, it is promoted to int before doing the operations. & (AND) Returns AND of input values a&b
❖ In the calculation, just the individual bits of a number are considered, not the complete number.
❖ Negative integers are stored or represented in two’s complement form. For example, -4 is 1111 1111 1111
| (OR) Returns OR of input values a|b
1111 1111 1111 1111 1100.
Truth Table int a=2; // 0010 Truth Table int a=2; // 0010
int b=3; // 0011 int b=3; // 0011
0010 0010
0011 0011
Output: Output:
--------- ---------
0010 0011
Truth Table int a=2; // 0010 Truth Table int a=2; // 0010
int b=3; // 0011
0010 0010
0011 ---------
Output: Output:
--------- 11 01
00 01
Program Program
Signed integers are numbers with a “+” or “-“ sign. If n bits are used to represent a signed
No
-8>>0 1000 -8
binary integer number, then out of n bits,1 bit will be used to represent a sign of the number change
and rest (n - 1)bits will be utilized to represent magnitude part of the number itself.
-8>>1 1000 1100 -4
There are various ways of representing signed numbers in a computer −
•Sign and magnitude -4>>1 1100 1110 -2
decimal value , again take 2’s complement. The MSB will be used to represent a sign of the printf("%d",-13<<2);
return 0;
number. Input Output Task
-13 1101 Binary representation of 13
}
1101 001 0 One’s complement How? Output:
0010 0011 Add 1 -52
Output=(-4)
OPERATORS OPERATORS
#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;
}
EXPRESSIONS EXPRESSIONS
In C, an expression is a set of operands and operators that computes a single value stored in a An expression can be defined depending on the position and number of its operator and operands:
variable. The operator represents the action or operation to be carried out. The operands are the Infix Expression (operator is used between the operands)
items to which the operation is applied. a=x+y
Postfix Expression (operator is used after the operands)
xy+
Prefix Expression (operator is used before the operands)
+xy
Unary Expression (one operator and one operand)
x++
Binary Expression (one operator and two operands)
x+y
EXPRESSIONS EXPRESSIONS
Arithmetic Expression
It consists of arithmetic operators ( + , - , * , and / ) and computes values of int, float, or double type.
Relational Expression
It usually consists of comparison operators (> , < , >= , <= , === , and !== ) and computes the
answer in the bool type, i.e., true (1) or false (0).
Logical Expression
It consists of logical operators (&&, ||, and !) and combines relational expressions to compute
answers in the bool type.
EXPRESSIONS EXPRESSIONS
EXPRESSIONS
Operator Precedence
Let us consider an example:
int x=5-17*9;
In C, the precedence of * is higher than – and =. Hence, 17*9 is evaluated first. Then – is evaluated and
The arithmetic expression returns: 10
The relational expression returns: 1 result is assigned to the variable x.
The logical expression returns: 0
The conditional expression returns: 1
The pointer expression returns: 0x7ffdb0430704
The bitwise expression returns: 5