0% found this document useful (0 votes)
13 views21 pages

Unit 2 UGCSA102

C program professor pratik notes
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)
13 views21 pages

Unit 2 UGCSA102

C program professor pratik notes
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/ 21

‭VIVEKANANDA GLOBAL UNIVERSITY‬

‭LECTURE NOTES‬
‭Unit II‬
‭ Basics, Operators, Loops‬
C
‭(6L)‬

‭CONTENT:‬
‭ .‬‭Introduction to C‬
1
‭2.‬‭features of C‬
‭3.‬‭basic C program structure‬
‭4.‬‭C tokens‬
‭5.‬‭data types‬
‭6.‬‭C Operators and precedence‬
‭7.‬‭Type conversion‬
‭8.‬‭Control Structures‬
‭a.‬‭If‬
‭b.‬‭If-else‬
‭c.‬ ‭nested if‬
‭d.‬‭switch- case‬
‭e.‬ ‭While‬
‭f.‬ ‭Do-while‬
‭g.‬‭for statements‬
‭9.‬‭Unconditional control statements‬
‭a.‬‭Break‬
‭b.‬‭Continue‬

‭ otes By:- Pratik Pandey‬


N ‭1‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭What is C?‬

‭A. Definition:‬

‭●‬ ‭C is a general-purpose, procedural programming language created in‬


‭the early 1970s by Dennis Ritchie at Bell Labs.‬
‭●‬ ‭It is known for its efficiency, simplicity, and low-level features,‬
‭making it widely used for developing system software and‬
‭applications.‬

‭B. Characteristics:‬

‭Mid-Level Language:‬
‭●‬ ‭C combines low-level features for direct hardware‬
‭manipulation with high-level constructs for programming ease.‬
‭Procedural Language:‬
‭●‬ ‭C follows a procedural programming paradigm, organizing‬
‭code into functions or procedures.‬
‭Portable:‬
‭●‬ ‭Code written in C is highly portable, meaning it can run on‬
‭different platforms with minimal modification.‬
‭Efficient:‬
‭●‬ ‭C provides direct access to memory and system resources,‬
‭making it efficient for system-level programming.‬

‭Features of C Programming Language‬

‭I. Overview:‬

‭ otes By:- Pratik Pandey‬


N ‭2‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭ is a versatile and widely-used programming language that has stood the‬
C
‭test of time. Understanding its key features is crucial for effective‬
‭programming and software development.‬

‭II. Key Features:‬

‭A. Simplicity:‬

‭●‬ ‭C is designed with simplicity in mind, providing a minimalistic set of‬


‭keywords and a straightforward syntax.‬
‭●‬ ‭It allows for low-level programming while maintaining a high-level‬
‭structure.‬

‭B. Portability:‬

‭●‬ ‭C programs are highly portable, meaning they can be easily adapted‬
‭to run on different platforms with minimal modifications.‬
‭●‬ ‭This portability is due to the use of standard libraries and‬
‭well-defined specifications.‬

‭C. Efficiency:‬

‭●‬ ‭C allows for direct manipulation of hardware through pointers,‬


‭making it efficient for system-level programming.‬
‭●‬ ‭It provides low-level features like bit manipulation and direct‬
‭memory access.‬

‭D. Modularity:‬

‭●‬ ‭C supports modularity through functions and libraries, allowing‬


‭developers to break down large programs into smaller, manageable‬
‭parts.‬

‭ otes By:- Pratik Pandey‬


N ‭3‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭●‬ ‭This modular approach enhances code readability, reusability, and‬
‭maintainability.‬

‭E. Extensibility:‬

‭●‬ ‭C is extensible, enabling developers to add new features and‬


‭capabilities through the creation of user-defined functions and‬
‭libraries.‬
‭●‬ ‭This feature encourages the growth of the language based on specific‬
‭project requirements.‬

‭F. Powerful Standard Library:‬

‭●‬ ‭C comes with a powerful standard library that provides a rich set of‬
‭functions for input/output, string manipulation, memory allocation,‬
‭and more.‬
‭●‬ ‭This library simplifies common programming tasks and promotes‬
‭code efficiency.‬

‭G. Static Typing:‬

‭●‬ ‭C is statically typed, meaning variable types need to be declared‬


‭before compilation.‬
‭●‬ ‭This approach catches type-related errors during the compilation‬
‭phase, enhancing program reliability.‬

‭H. Recursion:‬

‭ ‬ ‭C supports recursion, allowing functions to call themselves.‬



‭●‬ ‭This feature facilitates elegant solutions to certain problems and‬
‭promotes efficient use of memory.‬

‭ otes By:- Pratik Pandey‬


N ‭4‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭I. Pointers:‬

‭●‬ ‭Pointers in C enable direct memory manipulation, enhancing the‬


‭ability to work with addresses and optimize memory usage.‬
‭●‬ ‭While powerful, the use of pointers requires careful handling to‬
‭avoid memory-related errors.‬

‭J. Low-level Manipulation:‬

‭●‬ ‭C allows low-level manipulation of bits and bytes, making it suitable‬


‭for tasks such as device driver development and embedded systems‬
‭programming.‬
‭●‬ ‭This capability grants fine-grained control over the hardware.‬

‭Basic Structure of a C Program‬

‭I. Introduction:‬

‭ efore diving into writing C programs, it's crucial to understand the basic‬
B
‭structure that every C program follows. This structure ensures that the‬
‭compiler can understand and execute the code correctly.‬

‭II. Components of a C Program:‬

‭A. Header Files:‬

‭●‬ ‭Begin your program by including necessary header files. These files‬
‭provide essential information to the compiler about functions used in‬
‭the program.‬

‭ otes By:- Pratik Pandey‬


N ‭5‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭#include <stdio.h> // Standard Input/Output functions‬

‭B. main() Function:‬

‭●‬ ‭Every C program starts execution from the main() function. It serves as the‬
‭entry point for the program.‬

i‭nt main() {‬
‭// Program logic goes here‬
‭return 0; // Indicates successful execution‬
‭}‬

‭●‬ ‭The‬‭int‬‭before‬‭main()‬‭indicates that the function returns an‬


i‭nteger value.‬
‭●‬ ‭The‬‭return 0;‬‭statement signifies that the program executed‬
‭successfully. A non-zero value typically indicates an error.‬

‭C. Comments:‬

‭●‬ ‭Use comments to document your code. They don't affect the program‬
‭but provide information to programmers.‬

‭// This is a single-line comment‬

‭/*‬

‭This is a multi-line‬

‭comment‬

‭*/‬

‭ otes By:- Pratik Pandey‬


N ‭6‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭D. Statements and Expressions:‬

‭●‬ ‭Write statements to perform specific actions. Statements are typically‬


‭terminated with a semicolon.‬

‭printf("Hello, World!"); // Print a message to the console‬

‭●‬ ‭The‬‭printf‬‭function is part of the standard input/output library.‬

‭III. Complete Example:‬

‭#include <stdio.h>‬

‭int main() {‬

‭// Program logic‬

‭printf("Hello, World!"); // Display a message‬

‭return 0; // Indicates successful execution‬

‭}‬

‭IV. Explanation:‬

‭●‬ ‭The program includes the necessary header file (‬‭#include <stdio.h>‬‭)‬
f‭ or standard input/output functions.‬
‭●‬ ‭The‬‭main()‬‭function serves as the entry point.‬
‭●‬ ‭Inside‬‭main()‬‭, the‬‭printf‬‭function is used to display "Hello, World!" on‬
‭the console.‬
‭●‬ ‭The‬‭return 0;‬‭statement signals successful program execution.‬

‭ otes By:- Pratik Pandey‬


N ‭7‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭I. C Tokens:‬

‭A. Definition:‬

‭●‬ ‭In C programming, a token is the smallest unit of a program that‬


‭carries a specific meaning to the compiler. Tokens are the building‬
‭blocks of a C program.‬

‭B. Types of C Tokens:‬

‭Keywords:‬
‭●‬ ‭Reserved words that have a predefined meaning in the C‬
‭language. Examples include‬‭int‬‭,‬‭if‬‭,‬‭else‬‭,‬‭while‬‭, and‬‭return‬‭.‬
‭Identifiers:‬
‭●‬ ‭Names given to entities such as variables, functions, and arrays‬
‭created by the programmer. Must follow certain rules, e.g., no‬
‭spaces or special characters except underscore.‬
‭Constants:‬
‭●‬ ‭Fixed values that do not change during the program's‬
‭execution.‬
‭●‬ ‭Numeric Constants: e.g.,‬‭25‬‭,‬‭3.14‬
‭●‬ ‭Character Constants: e.g.,‬‭'A'‬‭,‬‭'7'‬
‭●‬ ‭String Constants: e.g.,‬‭"Hello, World!"‬
‭String Literals:‬
‭●‬ ‭Sequences of characters enclosed in double quotes,‬
‭representing string constants.‬
‭printf("This is a string literal");‬

‭Operators:‬
‭●‬ ‭Symbols that perform operations on variables and values.‬
‭Examples include‬‭+‬‭,‬‭-‬‭,‬‭*‬‭,‬‭/‬‭,‬‭%‬‭,‬‭==‬‭,‬‭!=‬‭.‬
‭ otes By:- Pratik Pandey‬
N ‭8‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭Punctuation Symbols:‬
‭●‬ ‭Symbols used to separate statements or indicate program‬
‭structure. Examples include‬‭;‬‭(semicolon),‬‭,‬‭(comma),‬‭()‬
‭(parentheses),‬‭{}‬‭(curly braces).‬
‭Comments:‬
‭●‬ ‭Used to provide explanations or notes within the code.‬
‭Comments are ignored by the compiler.‬

‭// This is a single-line comment‬

‭/*‬

‭This is a‬

‭multi-line comment‬

‭*/‬

‭II. Data Types in C:‬

‭A. Definition:‬

‭●‬ ‭Data types define the type of data a variable can hold. C supports‬
‭various data types to accommodate different kinds of values.‬

‭B. Basic Data Types:‬

‭int:‬
‭●‬ ‭Integer data type, used to store whole numbers.‬

‭ otes By:- Pratik Pandey‬


N ‭9‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭●‬ ‭Example:‬‭int age = 25;‬
‭float:‬
‭●‬ ‭Floating-point data type, used to store decimal numbers.‬
‭●‬ ‭Example:‬‭float price = 3.99;‬
‭double:‬
‭●‬ ‭Double-precision floating-point data type, provides more‬
‭precision than‬‭float‬‭.‬
‭●‬ ‭Example:‬‭double pi = 3.14159265359;‬
‭char:‬
‭●‬ ‭Character data type, used to store single characters.‬
‭●‬ ‭Example:‬‭char grade = 'A';‬

‭I. C Operators:‬

‭A. Definition:‬

‭●‬ ‭Operators in C are symbols that perform operations on variables and‬


‭values. They help manipulate data and perform computations.‬

‭B. Types of Operators:‬

‭Arithmetic Operators:‬
‭●‬ ‭Perform basic mathematical operations.‬

‭int a = 10, b = 5;‬

‭int sum = a + b; // Addition‬

‭int diff = a - b; // Subtraction‬

‭int product = a * b; // Multiplication‬

‭int quotient = a / b; // Division‬


‭ otes By:- Pratik Pandey‬
N ‭10‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭int remainder = a % b; // Modulus (remainder)‬

‭Relational Operators:‬
‭●‬ ‭Compare two values and return a true or false result.‬

‭int x = 8, y = 12;‬

‭printf("%d\n", x > y); // Greater than‬

‭printf("%d\n", x < y); // Less than‬

‭printf("%d\n", x == y); // Equal to‬

‭printf("%d\n", x != y); // Not equal to‬

‭Logical Operators:‬
‭●‬ ‭Combine relational expressions and return a true or false result.‬

‭int p = 1, q = 0;‬

‭printf("%d\n", p && q); // Logical AND‬

‭printf("%d\n", p || q); // Logical OR‬

‭printf("%d\n", !p); // Logical NOT‬

‭Assignment Operators:‬
‭●‬ ‭Assign values to variables and perform operations in a single‬
‭step.‬

‭ otes By:- Pratik Pandey‬


N ‭11‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭int num = 5;‬

‭num += 3; // Equivalent to num = num + 3;‬

‭Increment and Decrement Operators:‬


‭●‬ ‭Increase or decrease the value of a variable by 1.‬

‭int count = 10;‬

‭count++; // Increment by 1‬

‭count--; // Decrement by 1‬

‭II. Operator Precedence:‬

‭●‬ ‭Operator precedence determines the order in which operators are‬


‭evaluated in an expression.‬

‭A. Example:‬

‭int result = 5 + 3 * 2;‬

‭●‬ ‭Here, multiplication has higher precedence than addition. So,‬‭3 * 2‬‭is‬
‭evaluated first, and then the result is added to‬‭5‭.‬‬

‭B. Precedence Levels:‬

‭●‬ ‭Operators with higher precedence are evaluated first.‬

‭ otes By:- Pratik Pandey‬


N ‭12‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭●‬ ‭Parentheses can be used to change the default precedence.‬

‭Precedence Orders‬

‭1. Parentheses ()‬

‭2. Unary Operators +, -, ++, --‬

‭3. Multiplicative Operators *, /, %‬

‭4. Additive Operators +, -‬

‭5. Relational Operators <, >, <=, >=‬

‭6. Equality Operators ==, !=‬

‭7. Logical AND &&‬

‭8. Logical OR ||‬

‭9. Assignment Operators =, +=, -=, *=, /=, %=‬

‭III. Type Conversion:‬

‭A. Implicit Type Conversion (Automatic):‬

‭●‬ ‭The compiler automatically converts data from one type to another.‬

‭int integerNumber = 10;‬

‭float floatNumber = integerNumber; // Implicit conversion from int to float‬

‭ otes By:- Pratik Pandey‬


N ‭13‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭B. Explicit Type Conversion (Casting):‬

‭●‬ ‭The programmer explicitly specifies the type conversion.‬

‭float pi = 3.14159;‬

‭int approxPi = (int)pi; // Explicitly convert float to int‬

‭C. Type Casting Functions:‬

‭●‬ ‭int()‬‭,‬‭float()‬‭,‬‭char()‬‭, etc., can be used for type casting.‬

‭float price = 45.99;‬

‭int approxPrice = (int)price; // Explicit casting‬

‭Control Structure Statements in C‬

‭I.‬‭if‬‭Statement:‬

‭A. Definition:‬

‭●‬ ‭The‬‭if‬‭statement is used for conditional execution. It evaluates a‬


‭condition and executes a block of code if the condition is true.‬

‭B. Syntax:‬

‭if (condition) {‬

‭// Code to execute if condition is true‬

‭}‬

‭ otes By:- Pratik Pandey‬


N ‭14‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭II.‬‭if-else‬‭Statement:‬

‭A. Definition:‬

‭●‬ ‭The‬‭if-else‬‭statement extends the‬‭if‬‭statement by providing an‬


‭alternative block of code to execute when the condition is false.‬

‭B. Syntax:‬

‭if (condition) {‬

‭// Code to execute if condition is true‬

‭} else {‬

‭// Code to execute if condition is false‬

‭}‬

‭III. Nested‬‭if‬‭Statement:‬

‭A. Definition:‬

‭●‬ ‭Nested‬‭if‬‭statements are‬‭if‬‭statements inside other‬‭if‬‭or‬‭else‬‭blocks.‬

‭B. Syntax:‬

‭if (condition1) {‬

‭// Code to execute if condition1 is true‬

‭if (condition2) {‬

‭ otes By:- Pratik Pandey‬


N ‭15‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭// Code to execute if both condition1 and condition2 are true‬

‭}‬

‭}‬

‭IV.‬‭switch-case‬‭Statement:‬

‭A. Definition:‬

‭●‬ ‭The‬‭switch-case‬‭statement provides a way to handle multiple‬


‭conditions based on the value of an expression.‬

‭B. Syntax:‬

‭switch (expression) {‬

‭case value1:‬

‭// Code to execute if expression equals value1‬

‭break;‬

‭case value2:‬

‭// Code to execute if expression equals value2‬

‭break;‬

‭// ... other cases ...‬

‭default:‬

‭// Code to execute if expression doesn't match any case‬

‭}‬

‭ otes By:- Pratik Pandey‬


N ‭16‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬

‭V.‬‭while‬‭Loop:‬

‭A. Definition:‬

‭●‬ ‭The‬‭while‬‭loop executes a block of code repeatedly as long as a‬


‭specified condition is true.‬

‭B. Syntax:‬

‭while (condition) {‬

‭// Code to execute while the condition is true‬

‭}‬

‭VI.‬‭do-while‬‭Loop:‬

‭A. Definition:‬

‭●‬ ‭The‬‭do-while‬‭loop is similar to the‬‭while‬‭loop but guarantees the‬


e‭ xecution of the block of code at least once, as the condition is‬
‭checked after the loop body.‬

‭B. Syntax:‬

‭do {‬

‭// Code to execute at least once‬

‭} while (condition);‬

‭ otes By:- Pratik Pandey‬


N ‭17‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭VII.‬‭for‬‭Loop:‬

‭A. Definition:‬

‭●‬ ‭The‬‭for‬‭loop provides a concise way to write loops by combining the‬


‭initialization, condition, and increment/decrement steps.‬

‭B. Syntax:‬

‭for (initialization; condition; increment/decrement)‬

‭for (i=1;i<=10;i++) {‬

‭// Code to execute as long as the condition is true‬

‭}‬

‭Unconditional Control Statements in C - break and continue‬

‭I. break Statement:‬

‭A. Definition:‬

‭●‬ T
‭ he break statement is used to terminate the execution of the innermost loop‬
‭or switch-case statement.‬

‭B. Usage:‬

‭for (int i = 0; i < 10; i++) {‬

‭if (i == 5) {‬

‭break; // Exit the loop when i reaches 5‬

‭ otes By:- Pratik Pandey‬


N ‭18‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭}‬

‭// Code inside the loop‬

‭}‬

‭C. Use Cases:‬

‭‬ B
● ‭ reaking out of a loop prematurely based on a certain condition.‬
‭●‬ ‭Terminating the execution of a switch-case block.‬

‭II. continue Statement:‬

‭A. Definition:‬

‭●‬ T
‭ he continue statement is used to skip the rest of the code inside the loop for‬
‭the current iteration and move to the next iteration.‬

‭B. Usage:‬

‭for (int i = 0; i < 10; i++) {‬

‭if (i == 5) {‬

‭continue; // Skip the rest of the loop for i=5‬

‭}‬

‭// Code inside the loop‬

‭}‬

‭ otes By:- Pratik Pandey‬


N ‭19‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬
‭C. Use Cases:‬

‭‬ S
● ‭ kipping certain iterations of a loop based on a specific condition.‬
‭●‬ ‭Avoiding the execution of remaining code for a particular case within a loop.‬

‭III. break vs. continue:‬

‭ ‬ b‭ reak is used to exit the loop entirely.‬



‭●‬ ‭continue is used to skip the remaining code inside the loop for the current‬
‭iteration and move to the next iteration.‬

‭IV. Example:‬

‭#include <stdio.h>‬

‭int main() {‬

‭for (int i = 0; i < 10; i++) {‬

‭if (i == 3) {‬

‭continue; // Skip the rest of the loop for i=3‬

‭}‬

‭if (i == 7) {‬

‭break; // Exit the loop when i reaches 7‬

‭}‬

‭printf("%d ", i);‬

‭}‬

‭ otes By:- Pratik Pandey‬


N ‭20‬
‭Assistant Professor‬
‭VIVEKANANDA GLOBAL UNIVERSITY‬
‭LECTURE NOTES‬

‭return 0;‬

‭}‬

‭* * * * * * * * * * * * * Thank You * * * * * * * * * * * * *‬

‭ otes By:- Pratik Pandey‬


N ‭21‬
‭Assistant Professor‬

You might also like