Unit 1 Notes
Unit 1 Notes
4. Modern Languages
o Languages like Python, Java are even more advanced.
o They focus on making coding simple, powerful, and versatile for different uses like web development,
apps, and AI.
Why Evolution of Programming Languages was Needed:
Debugging:
If there are errors, find and fix them. This is called debugging.
Algorithm
Characteristics:
What is a Comment?
In programming, a comment is a piece of text in the code that is not executed by the computer. It’s used to
provide explanations, notes, or reminders for anyone reading the code. Comments help make the code easier to
understand by giving context about what certain parts of the code do.
1. Clarity: Comments make the code more understandable, especially when it is complex. They explain what the
code is doing and why certain decisions were made.
2. Maintenance: If someone else (or even the original programmer after some time) has to come back to the code,
comments help them quickly grasp the functionality without having to read through every line.
3. Collaboration: In team projects, comments help team members understand each other's code, making it easier
to work together.
Imagine you work at a tech company and write some complex code without any comments. You think it’s fine
and leave the company for a new job. A few months later, a new developer joins and has to work on your code.
Confused, they think, “What does this even do?” With no comments to explain your logic, they spend hours trying to
figure it out. Frustrated, they might end up pulling their hair out!
The Introduction to C
C is a high-level programming language created by Dennis Ritchie in the 1970s. It's known for:
Real-Life Example: C is used in operating systems like Linux and Windows, as well as in embedded systems
like washing machines and cars.
C is faster than other languages because it works closer to the computer's hardware, allowing direct control over
memory and resources. Also, since it turns code into machine language before running, it executes tasks more quickly
The structure of C
Input Statement: The scanf function is used to take input from the user. It reads data from the standard input (usually
the keyboard) and stores it in a specified variable. For example, to read an integer, you would use:
Here %d is the format specifier for integers, and &variable refers to the memory location where the input will be
stored.
Output Statement: The printf function is used to display information to the user. It sends formatted output to the
standard output (usually the screen). For example, to print the value of a variable, you would write:
What are Variables?
A variable in C is like a container that holds data. It has a name, so you can easily refer to it in your program. For
example, if you want to store a person’s age, you can create a variable called age.
Declaring a Variable: Before using a variable, you need to tell the program what type of data it will hold. For example:
Identifiers are crucial for naming elements in your program. Choosing meaningful
names helps others (and your future self) understand the code better. For instance,
naming a variable totalAmount is more informative than just a.
What are constants?
Constants in C are fixed values that do not change during the execution of a program. They can be of various data types
and are used to represent values that remain the same throughout the program, making code easier to read and
maintain.
Scope
Scope refers to the visibility or accessibility of variables and functions in different parts of a program. In C,
there are two main types:
Suppose You keep the toy in your bedroom. Only you can play with it no
outsider can use it,. The toy is not available anywhere else this is a local
scope vaiable(toy). You leave the toy in the living room. Now, everyone in
the house can play with it, no matter where they are this is global variable
scope.
This is how scope works. A local variable is like the bedroom toy—limited to one place. A global variable is
like the living room toy—accessible to everyone.
Binding
in simple terms, binding means connecting a name to something, like a variable to a value or a function to a task. When
you say, "x = 5" in a program, you're binding the name "x" to the value 5. It helps the program know what "x" represents
during its execution.
L and R value
L-value (Left value): It refers to something that has a location in
memory, like a variable. It can appear on the left side of an
assignment. Example: x = 5; Here, x is an L-value because you can
assign a value to it.
R-value (Right value): It refers to the value itself. It appears on the
right side of an assignment. Example: x = 5; Here, 5 is an R-value
because it's the value being assigned.
So, L-value is where the value is stored, and R-value is the value being used or assigned.
What are increment/decrement?
1. Prefix Increment (++x): Increases the value before using it. For example, if x = 5, then ++x makes it 6
first.
2. Postfix Increment (x++): Increases the value after using it. For example, if x = 5, then x++ uses 5 first
and then changes it to 6.
operators
Arithmetic Operators
Examples:
o + (Addition): Adds two numbers together.
Example: 5 + 3 gives 8.
o - (Subtraction): Subtracts one number from another.
Example: 5 - 3 gives 2.
2. Relational Operators
Definition: These operators compare two values and return true or false based on the comparison.
Examples:
o == (Equal to): Checks if two values are equal.
Example: 5 == 5 is true.
o != (Not equal to): Checks if two values are not equal.
Example: 5 != 3 is true.
3. Assignment Operators
Examples:
o = (Assign): Sets a variable to a specific value.
Example: int x = 5; sets x to 5.
o += (Add and assign): Adds a value to a variable and updates the variable.
Example: x += 3; adds 3 to x (if x was 5, now x becomes 8).
4. Bitwise Operators
Examples:
o (Bitwise AND): Compares each bit of two numbers; the result has a 1 only where both bits are 1.
Example: 5 & 3 (binary: 101 & 011) gives 1 (binary: 001).
o | (Bitwise OR): Compares each bit; the result has a 1 where at least one bit is 1.
Example: 5 | 3 gives 7 (binary: 111).
Operator precedence
Operator precedence determines the order in which operators are evaluated in an expression. Higher
precedence operators are evaluated before lower precedence ones. Here’s a brief list from highest to lowest
precedence:
1. Parentheses ()
2. Increment/Decrement ++, --
3. Arithmetic: *, /, %
4. Arithmetic: +, -
5. Relational: <, >, <=, >=
6. Equality: ==, !=
7. Logical: &&, ||
8. Assignment: =, +=, -=
Example