CCP - Unit 3 Notes
CCP - Unit 3 Notes
Part A
Part B
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment & Decrement operators
• Operator Precedence and Associatively
• Evaluation of arithmetic expressions
• Mathematical library functions in C using math.h
• Type casting & conversion.
Formatted I/O in C
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a
certain format. Printf() and scanf() are two examples of C routines that handle formatted I/O. The
type and format of the data to be read or written are specified by format strings, which are used by
these operations. The program's execution replaces the placeholders for the data found in the format
strings with the actual data.
1) printf():
printf() function is used in a C program to display any value like float, integer, character,
string, etc on the console screen. It is a pre-defined function that is already declared in the
stdio.h(header file).
Syntax:
or
2) scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard
by the user, these values can be of any data type like integer, float, character, string, and many more.
This function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In
scanf() function we use &(address-of operator) which is used to store the variable value on the
memory location of that variable.
Syntax:
or
#include <stdio.h>
void main() {
char name[20];
int age;
#include <stdio.h>
int main() {
int integer = 42;
float floating = 3.14159;
char character = 'A';
char string[] = "Hello, World!";
return 0;
}
Escape Sequence in C
C Program to demonstrate escape squence
#include <stdio.h>
void main() {
printf("Hello, World!\n"); // New line
printf("Hello,\tWorld!\n"); // Horizontal tab
printf("Hello, \"World!\"\n"); // Double quote
printf("Hello, \\World!\\\n"); // Backslash
printf("Hello, \bWorld!\n"); // Backspace
printf("Hello, \rWorld!\n"); // Carriage return
printf("Hello, \vWorld!\n"); // Vertical tab
}
Unformatted I/O functions are used only for character data type or character array/string and
cannot be used for any other datatype. These functions are used to read single input from the user at
the console and it allows to display the value at the console.
These functions are called unformatted I/O functions because we cannot use format
specifiers in these functions and hence, cannot format these functions according to our needs.
1)getchar():
The getchar() function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one character at one time
until and unless the enter key is pressed. This function is declared in stdio.h(header file)
Syntax:
Variable-name = getchar();
Example:
#include <stdio.h>
// Driver code
void main()
{
// Declaring a char type variable
char ch;
printf("Enter the character: ");
// Taking a character from keyboard
ch = getchar();
// Displays the value of ch
printf("%c", ch);
}
2)putchar():
The putchar() function is used to display a single character at a time by passing that
character directly to it or by passing a variable that has already stored a character. This function is
declared in stdio.h(header file)
Syntax:
putchar(variable_name);
Example:
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
}
3)gets():
gets() function reads a group of characters or strings from the keyboard by the user and these
characters get stored in a character array. This function allows us to write space-separated texts or
strings. This function is declared in stdio.h(header file).
Syntax:
//Declare a char type variable of any length
char str[length of string in number];
gets(str);
Example:
#include <stdio.h>
void main()
{
// Declaring a char type array of length 50 characters
char name[50];
printf("Please enter some texts: ");
// Reading a line of character or a string
gets(name);
// Displaying this line of character or a string
printf("You have entered: %s", name);
}
4)puts():
Syntax:
puts(identifier_name);
Example:
#include <stdio.h>
int main()
{
char name[50];
printf("Enter your text: ");
// Reads string from user
gets(name);
printf("Your text is: ");
// Displays string
puts(name);
}
1. toascii
2. toupper
3. tolower
4. isalpha
5. isnumeric
1. toascii
Converts a character to its ASCII value.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
printf("ASCII value of %c is %d\n", ch, toascii(ch));
return 0;
}
2. toupper
Converts a lowercase letter to uppercase.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'a';
printf("Uppercase of %c is %c\n", ch, toupper(ch));
return 0;
}
3. tolower
Converts an uppercase letter to lowercase.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
printf("Lowercase of %c is %c\n", ch, tolower(ch));
return 0;
}
4. isalpha
Checks if a character is an alphabetic letter.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isalpha(ch)) {
printf("%c is an alphabetic character.\n", ch);
} else {
printf("%c is not an alphabetic character.\n", ch);
}
return 0;
}
5. isnumeric
Checks if a character is a digit.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '5';
if (isnumeric(ch)) {
printf("%c is a numeric character.\n", ch);
} else {
printf("%c is not a numeric character.\n", ch);
}
return 0;
}
These functions are part of the <ctype.h> library in C, which provides a range of functions for
character handling
Operators
In C, operators are symbols that perform operations on variables and values. They are
essential components of the language, enabling various types of computations and manipulations.
Here are the main categories of operators in C:
1. Arithmetic Operators
Used to perform basic mathematical operations.
2. Relational Operators
Used to compare two values.
3. Logical Operators
Used to perform logical operations.
4. Bitwise Operators
Used to perform bit-level operations.
5. Assignment Operators
Used to assign values to variables.
6. Other Operators
Includes miscellaneous operators.
1)Arithmetic Operators
Arithmetic operators in C are used to perform basic mathematical operations. Here are the main
arithmetic operators along with examples to demonstrate their usage:
1. Addition (+)
Adds two operands.
#include <stdio.h>
int main() {
int a = 10, b = 5;
int result = a + b;
printf("a + b = %d\n", result); // Output: 15
return 0;
}
2. Subtraction (-)
Subtracts the second operand from the first.
#include <stdio.h>
int main() {
int a = 10, b = 5;
int result = a - b;
printf("a - b = %d\n", result); // Output: 5
return 0;
}
3. Multiplication (*)
Multiplies two operands.
#include <stdio.h>
int main() {
int a = 10, b = 5;
int result = a * b;
printf("a * b = %d\n", result); // Output: 50
return 0;
}
4. Division (/)
Divides the first operand by the second. Note that if both operands are integers, the result will be an
integer (fractional part is discarded).
#include <stdio.h>
int main() {
int a = 10, b = 5;
int result = a / b;
printf("a / b = %d\n", result); // Output: 2
return 0;
}
5. Modulus (%)
Returns the remainder of the division of the first operand by the second.
#include <stdio.h>
int main() {
int a = 10, b = 3;
int result = a % b;
printf("a %% b = %d\n", result); // Output: 1
return 0;
}
2) Relational Operators
Relational operators in C are used to compare two values. The result of a relational operation is a
boolean value, which is either true (non-zero) or false (zero). Here are the main relational operators
in C:
1. Equal to (==)
Checks if two operands are equal.
#include <stdio.h>
int main() {
int a = 5, b = 5;
if (a == b) {
printf("a is equal to b\n");
} else {
printf("a is not equal to b\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 5, b = 3;
if (a != b) {
printf("a is not equal to b\n");
} else {
printf("a is equal to b\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 5, b = 3;
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is not greater than b\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 3, b = 5;
if (a < b) {
printf("a is less than b\n");
} else {
printf("a is not less than b\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 5, b = 5;
if (a >= b) {
printf("a is greater than or equal to b\n");
} else {
printf("a is not greater than or equal to b\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 3, b = 5;
if (a <= b) {
printf("a is less than or equal to b\n");
} else {
printf("a is not less than or equal to b\n");
}
return 0;
}
3) Logical Operators
Logical operators in C are used to perform logical operations, typically on boolean values.
They are essential for decision-making in programs, allowing you to combine multiple conditions.
Here are the main logical operators in C:
#include <stdio.h>
int main() {
int a = 10, b = 20;
if (a > 0 && b > 0) {
printf("Both values are greater than 0\n");
} else {
printf("One or both values are not greater than 0\n");
}
return 0;
}
2. Logical OR (||)
The logical OR operator returns true if at least one of the operands is true. If both operands are
false, the result is false.
#include <stdio.h>
int main() {
int a = -1, b = 20;
if (a > 0 || b > 0) {
printf("At least one value is greater than 0\n");
} else {
printf("Neither value is greater than 0\n");
}
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
if (!(a > 0)) {
printf("a is not greater than 0\n");
} else {
printf("a is greater than 0\n");
}
return 0;
}
Assignment Operators
Assignment operators in C are used to assign values to variables. They can also perform
arithmetic operations and update the variable’s value in a single step. Here are the most common
assignment operators:
This operator assigns the value on the right to the variable on the left.
Example
Here’s a simple program demonstrating the use of assignment operators:
#include <stdio.h>
int main() {
int num = 10;
return 0;
}
Summary:
In C, the increment (++) and decrement (–) operators are unary operators used to increase or
decrease the value of a variable by 1, respectively. These operators are commonly used in loops,
array traversal, and pointer arithmetic.
Prefix Increment
In the prefix form, the value of the variable is incremented first, and then the updated value
is used in the expression.
int a = 5;
int result = ++a; // a is incremented to 6, then result is assigned the value 6
Postfix Increment
In the postfix form, the current value of the variable is used in the expression first, and then
the variable is incremented.
int b = 5;
int result = b++; // result is assigned the value 5, then b is incremented to 6
Prefix Decrement
In the prefix form, the value of the variable is decremented first, and then the updated value
is used in the expression.
int c = 5;
int result = --c; // c is decremented to 4, then result is assigned the value 4
Postfix Decrement
In the postfix form, the current value of the variable is used in the expression first, and then
the variable is decremented.
int d = 5;
int result = d--; // result is assigned the value 5, then d is decremented to 4
Example
Here’s a simple program demonstrating both increment and decrement operators:
#include <stdio.h>
int main() {
int a = 5, b = 5, c = 5, d = 5;
return 0;
}
Output:
Initial values: a = 5, b = 5, c = 5, d = 5
After operations: a = 6, b = 6, c = 4, d = 4
Operator Precedence
Operator precedence defines the priority of operators. Operators with higher precedence are
evaluated before operators with lower precedence. Here’s a simplified table of operator precedence
from highest to lowest:
Associativity determines the direction in which operators of the same precedence are
evaluated. It can be either left-to-right or right-to-left.
Left-to-Right Associativity: Most operators, including arithmetic (+, -, *, /), relational (<, >, <=,
>=), and logical (&&, ||) operators, are left-to-right associative.
Right-to-Left Associativity: Unary operators (++, --, !, ~), assignment operators (=, +=, -=, etc.),
and the conditional operator (?:) are right-to-left associative.
Example
Consider the following expression:
int result = 10 + 20 * 30 / 5;
Multiplication (*) and division (/) have higher precedence than addition (+).
Among * and /, they are evaluated left-to-right due to their associativity.
So, the expression is evaluated as:
20 * 30 → 600
600 / 5 → 120
10 + 120 → 130
Summary
1. Parentheses: Expressions within parentheses are evaluated first. If there are nested
parentheses, the innermost parentheses are evaluated first.
2. Operator Precedence: Operators with higher precedence are evaluated before operators with
lower precedence.
3. Associativity: When operators of the same precedence appear, associativity determines the
order of evaluation. Most arithmetic operators are left-to-right associative.
Example
Consider the following expression:
int result = (10 + 20) * (30 / (5 - 2));
int result = 10 + 20 * 30 / 5 - 2;
The math.h header file in C provides a variety of mathematical functions that you can use to
perform common mathematical operations. Here are some of the most commonly used functions:
Summary
#include <math.h>
#include <stdio.h>
int main() {
double val1 = 1.6, val2 = -2.3;
printf("ceil(%.1f) = %.1f\n", val1, ceil(val1)); // Output: 2.0
printf("floor(%.1f) = %.1f\n", val2, floor(val2)); // Output: -3.0
return 0;
}
Absolute Value
#include <math.h>
#include <stdio.h>
int main() {
double val = -3.14;
printf("fabs(%.2f) = %.2f\n", val, fabs(val)); // Output: 3.14
return 0;
}
pow(double base, double exponent): Returns base raised to the power of exponent.
sqrt(double x): Returns the square root of x.
#include <math.h>
#include <stdio.h>
int main() {
double base = 2.0, exponent = 3.0;
printf("pow(%.1f, %.1f) = %.1f\n", base, exponent, pow(base, exponent)); // Output: 8.0
printf("sqrt(%.1f) = %.1f\n", base, sqrt(base)); // Output: 1.4
return 0;
}
Logarithmic Functions
#include <math.h>
#include <stdio.h>
int main() {
double val = 100.0;
printf("log(%.1f) = %.2f\n", val, log(val)); // Output: 4.61
printf("log10(%.1f) = %.2f\n", val, log10(val)); // Output: 2.00
return 0;
}
Trigonometric Functions
#include <math.h>
#include <stdio.h>
int main() {
double angle = 1.0; // in radians
printf("sin(%.1f) = %.2f\n", angle, sin(angle)); // Output: 0.84
printf("cos(%.1f) = %.2f\n", angle, cos(angle)); // Output: 0.54
printf("tan(%.1f) = %.2f\n", angle, tan(angle)); // Output: 1.56
return 0;
}
In C, type casting and type conversion are techniques used to convert a variable from one
data type to another. While they achieve similar outcomes, they differ in how and when the
conversion happens.
Type Conversion
Type conversion, also known as implicit conversion, is automatically performed by the
compiler when it encounters mixed data types in an expression. This conversion is done to ensure
that operations are performed correctly and without data loss.
Example
int a = 10;
float b = 5.5;
float result = a + b; // 'a' is implicitly converted to float
In this example, the integer a is implicitly converted to a float before the addition, ensuring that the
result is a float.
Type Casting
Type casting, also known as explicit conversion, is performed by the programmer to convert
a variable from one type to another. This is done using the casting operator (type).
Example
int a = 10;
int b = 3;
float result = (float)a / b; // 'a' is explicitly cast to float
Here, a is explicitly cast to a float before the division, ensuring that the result is a float and not an
integer.
Summary
Type Conversion: Automatic, performed by the compiler, usually safe and involves promotion.
Type Casting: Manual, performed by the programmer, can be unsafe and involves both promotion
and demotion.