Unit 2 UGCSA102
Unit 2 UGCSA102
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
A. Definition:
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.
I. Overview:
A. Simplicity:
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:
D. Modularity:
E. Extensibility:
● 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.
H. Recursion:
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.
● Begin your program by including necessary header files. These files
provide essential information to the compiler about functions used in
the program.
● Every C program starts execution from the main() function. It serves as the
entry point for the program.
int main() {
// Program logic goes here
return 0; // Indicates successful execution
}
C. Comments:
● Use comments to document your code. They don't affect the program
but provide information to programmers.
/*
This is a multi-line
comment
*/
#include <stdio.h>
int main() {
}
IV. Explanation:
● The program includes the necessary header file (#include <stdio.h>)
f or standard input/output functions.
● Themain()function serves as the entry point.
● Insidemain(), theprintffunction is used to display "Hello, World!" on
the console.
● Thereturn 0;statement signals successful program execution.
A. Definition:
Keywords:
● Reserved words that have a predefined meaning in the C
language. Examples includeint,if,else,while, andreturn.
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
multi-line comment
*/
A. Definition:
● Data types define the type of data a variable can hold. C supports
various data types to accommodate different kinds of values.
int:
● Integer data type, used to store whole numbers.
I. C Operators:
A. Definition:
Arithmetic Operators:
● Perform basic mathematical operations.
Relational Operators:
● Compare two values and return a true or false result.
int x = 8, y = 12;
Logical Operators:
● Combine relational expressions and return a true or false result.
int p = 1, q = 0;
Assignment Operators:
● Assign values to variables and perform operations in a single
step.
count++; // Increment by 1
count--; // Decrement by 1
A. Example:
● Here, multiplication has higher precedence than addition. So,3 * 2is
evaluated first, and then the result is added to5.
Precedence Orders
● The compiler automatically converts data from one type to another.
float pi = 3.14159;
I.ifStatement:
A. Definition:
B. Syntax:
if (condition) {
}
A. Definition:
B. Syntax:
if (condition) {
} else {
}
III. NestedifStatement:
A. Definition:
B. Syntax:
if (condition1) {
if (condition2) {
}
}
IV.switch-caseStatement:
A. Definition:
B. Syntax:
switch (expression) {
case value1:
break;
case value2:
break;
default:
}
V.whileLoop:
A. Definition:
B. Syntax:
while (condition) {
}
VI.do-whileLoop:
A. Definition:
B. Syntax:
do {
} while (condition);
A. Definition:
B. Syntax:
for (i=1;i<=10;i++) {
}
A. Definition:
● T
he break statement is used to terminate the execution of the innermost loop
or switch-case statement.
B. Usage:
if (i == 5) {
}
B
● reaking out of a loop prematurely based on a certain condition.
● Terminating the execution of a switch-case block.
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:
if (i == 5) {
}
}
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.
IV. Example:
#include <stdio.h>
int main() {
if (i == 3) {
}
if (i == 7) {
}
}
return 0;
}
* * * * * * * * * * * * * Thank You * * * * * * * * * * * * *