[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

Unit 1 Notes

btech notes

Uploaded by

ayush singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Unit 1 Notes

btech notes

Uploaded by

ayush singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 1

Evolution of programing and languages


In the early days, programming was done in machine language, which is just 0s and 1s (binary). It was extremely difficult
and took a lot of time to write even simple programs. This caused errors and slowed down progress. So, there was a
need for simpler languages to make programming easier and faster.

How Evolution Happened

1. Machine Language (1st Generation)


o Programs were written directly in binary.
o Very difficult to understand and use.

2. Assembly Language (2nd Generation)


o Introduced symbols (like ADD, SUB) to replace binary.
o Easier than machine language but still tied to hardware.

3. High-Level Languages (3rd Generation)


o C, FORTRAN, COBOL allowed programmers to write code in English-like commands.
o Made programming faster and easier to understand.

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:

Ease of Writing: Early languages, like machine language (0s and


1s), were very difficult to write and understand. A small mistake
could lead to major errors. This made it hard for programmers
to develop software quickly and accurately.

Error Reduction: Programming directly in binary was prone to


errors because it was too detailed and complex. More human-
friendly languages were needed to reduce these errors and
make debugging easier.

Faster Development: As technology advanced, programming


had to become faster and more efficient. Developers needed
languages that would allow them to focus on solving problems
rather than dealing with machine-level details.

Adapting to New Uses: Computers started being used in


different fields, like science, business, and personal use. This
created a need for specialized languages that could handle
specific tasks efficiently, like database management, web
development, etc.
Problem solving through programing

 Understand the Problem:


Read the problem carefully. Identify what needs to be solved.

 Plan the Solution:


Create an algorithm with step-by-step instructions. Break the problem
into smaller parts.

 Choose a Programming Language:


Select a language you're comfortable with (e.g., Python, Java).

 Write the Code:


Translate your algorithm into code. Keep it simple and readable.

 Test the Code:


Run your code with different inputs to check if it works correctly.

 Debugging:
If there are errors, find and fix them. This is called debugging.

 Optimize the Solution:


Look for ways to make your code faster and more efficient.

Algorithm

 Definition: An algorithm is a step-by-step set of instructions to solve a


specific problem or perform a task.
 Characteristics:
o Clear and unambiguous: Each step should be easy to
understand.
o Well-defined inputs and outputs: Specify what goes in and
what comes out.
o Finiteness: It should terminate after a finite number of steps.
o Effectiveness: Each step should be basic enough to be carried
out.

Example of an Algorithm: Finding the Largest Number in a


List

1. Start with the first number as the largest.


2. Compare each number in the list with the largest.
3. If a number is larger, update the largest.
4. After checking all numbers, the largest is the result.
Pseudo code

 Definition: Pseudocode is a way of representing an


algorithm using plain language, which helps to plan
out the code before actual programming. It is not
bound to any specific programming language.

 Characteristics:

 Readable: Should be easy for anyone to


understand.
 Structured: Follows a logical structure
similar to programming syntax.

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.

Why Are Comments Important?

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.

The Importance of Comments in Code

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.

They open your code and see something like this:

def fn(a, b, c): return a + b * c - (a / b)

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:

 Simplicity: Easy to learn and use.


 Efficiency: High performance in execution.
 Portability: Works on different platforms with minimal changes.
 Low-level Access: Direct manipulation of memory and hardware through pointers.
 Structured Programming: Breaks code into functions for better organization.

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

1. Preprocessor Directives: Include libraries, e.g., #include <stdio.h>.


2. Main Function: Every program starts with int main() { }.
3. Variable Declarations: Declare variables, e.g., int a;.
4. Statements: Write code logic and operations.
5. Return Statement: Ends the program, usually return 0;

The Input and output statement

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:

Assigning a Value: You can give a variable a value. For example:

Main Data Types in C and Their Sizes

1. Integer (int): Represents whole numbers.


o Size: 4 bytes (typically)
o Example: int age = 25;
2. Floating Point (float): Represents decimal numbers.
o Size: 4 bytes
o Example: float weight = 65.5;
3. Double (double): Represents double-precision floating-point numbers for more precision.
o Size: 8 bytes
o Example: double pi = 3.14159;
4. Character (char): Represents a single character.
o Size: 1 byte
o Example: char initial = 'J';
5. Short (short): Represents smaller whole numbers than int.
o Size: 2 bytes
o Example: short temperature = 15;
6. Long (long): Represents larger whole numbers than int
o Size: 4 bytes (typically; may vary based on the system)
o Example: long population = 1000000;

What are identifiers?


Basically names to the variable given are called identifiers and there are some
rules also for naming the identifiers

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.

Difference Between Constants and Literals


What are keywords
In simple terms, a keyword in C is a special word that the language already understands and uses for specific tasks. You
can't use these words for your own variables or names. For example, words like int (for numbers), if (for conditions),
and return are keywords.

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:

1. Local Scope: Variables declared inside a function are only accessible


within that function. They are "local" to it.
2. Global Scope: Variables declared outside all functions can be accessed
from anywhere in the program. They are "global."

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?

The increment operator (++) is used to add 1 to a variable in C.

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.

Decrement is same as the increment, just use the –x sign instead

operators
Arithmetic Operators

Definition: These operators are used for basic mathematical operations.

 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

Definition: These operators are used to assign a value to a variable.

 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

Definition: These operators perform operations on individual bits of integer values.

 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

For int x = 5 + 3 * 2;:

1. 3 * 2 is calculated first (gives 6).


2. Then 5 + 6 (gives 11).

So, x becomes 11. Use parentheses to control the order of evaluation!

You might also like