0% found this document useful (0 votes)
34 views

CCP - Unit 3 Notes

c notes

Uploaded by

Santosh Patil
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)
34 views

CCP - Unit 3 Notes

c notes

Uploaded by

Santosh Patil
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

Unit 3: Syllabus

Part A

Input and output with C

• Formatted I/O functions - printf and scanf,


• control strings and escape sequences,
• output specifications with printf functions;
• Unformatted I/O functions to read and display single
character and a string
◦ getchar, putchar, gets and puts functions.
• Character handling library functions -
◦ toascii, toupper, tolower, isalpha, isnumeric

Part B

C Operators & Expressions

• 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.

Input and output with C

Formatted I/O functions - printf and scanf


Input Output functions in C
Computer programming requires input/output (I/O) operations. Using I/O operations, the
data is read and written to and from various sources, including files, keyboards, and screens. I/O
operations can be either formatted or unformatted in the C computer language. In this blog post, we
will cover the distinctions between formatted and unformatted I/O in C, as well as code samples and
outputs.

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:

printf(“Format Specifier”, var1, var2, …., varn);

or

printf(“Format Specifier”, Variable List);

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:

scanf(“Format Specifier”, &var1, &var2, …., &varn);

or

scanf(“Format Specifier”, Address List);


Example for Formated I/O Program

#include <stdio.h>
void main() {
char name[20];
int age;

printf("Enter your name: ");


scanf("%s", name);

printf("Enter your age: ");


scanf("%d", &age);

printf("Your name is %s and your age is %d\n", name, age);


}

Control Strings / Format Specifier


Example:

#include <stdio.h>

int main() {
int integer = 42;
float floating = 3.14159;
char character = 'A';
char string[] = "Hello, World!";

printf("Integer: %d\n", integer); // %d for integer


printf("Float: %f\n", floating); // %f for float
printf("Character: %c\n", character); // %c for character
printf("String: %s\n", string); // %s for string
printf("Hexadecimal: %x\n", integer); // %x for hexadecimal
printf("Octal: %o\n", integer); // %o for octal
printf("Unsigned: %u\n", integer); // %u for unsigned integer
printf("Scientific: %e\n", floating); // %e for scientific notation

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 Input/Output functions

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.

Why they are called unformatted I/O?

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.

The following unformatted I/O functions


getchar()
putchar()
gets()
puts()

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():

In C programming puts() function is used to display a group of characters or strings which is


already stored in a character array. This function is declared in stdio.h(header file).

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);
}

Character handeling functions in C

1. toascii
2. toupper
3. tolower
4. isalpha
5. isnumeric

ctype.h header file contains all character functions

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.

Addition (+): Adds two operands.


Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of the division of the first operand by the second.

2. Relational Operators
Used to compare two values.

Equal to (==): Checks if two operands are equal.


Not equal to (!=): Checks if two operands are not equal.
Greater than (>): Checks if the left operand is greater than the right.
Less than (<): Checks if the left operand is less than the right.
Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right.
Less than or equal to (<=): Checks if the left operand is less than or equal to the right.

3. Logical Operators
Used to perform logical operations.

Logical AND (&&): Returns true if both operands are true.


Logical OR (||): Returns true if at least one operand is true.
Logical NOT (!): Reverses the logical state of its operand.

4. Bitwise Operators
Used to perform bit-level operations.

AND (&): Performs a bitwise AND.


OR (|): Performs a bitwise OR.
XOR (^): Performs a bitwise XOR.
NOT (~): Performs a bitwise NOT.
Left shift (<<): Shifts bits to the left.
Right shift (>>): Shifts bits to the right.

5. Assignment Operators
Used to assign values to variables.

Assignment (=): Assigns the right operand to the left operand.


Add and assign (+=): Adds the right operand to the left operand and assigns the result to the left
operand.
Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to
the left operand.
Multiply and assign (*=): Multiplies the left operand by the right operand and assigns the result to
the left operand.
Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the
left operand.
Modulus and assign (%=): Takes the modulus using the left and right operands and assigns the
result to the left operand.

6. Other Operators
Includes miscellaneous operators.

Increment (++): Increases an integer value by one.


Decrement (--): Decreases an integer value by one.
Conditional (?:): Returns one of two values depending on the condition.
Comma (,): Separates expressions.
Sizeof: Returns the size of a data type.
Pointer (*): Used to declare and dereference pointers.
Address-of (&): Returns the address of a variable.

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;
}

2. Not equal to (!=)


Checks if two operands are not equal.

#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;
}

3. Greater than (>)


Checks if the left operand is greater than the right operand.

#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;
}

4. Less than (<)


Checks if the left operand is less than the right operand.

#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;
}

5. Greater than or equal to (>=)


Checks if the left operand is greater than or equal to the right operand.

#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;
}

6. Less than or equal to (<=)


Checks if the left operand is less than or equal to the right operand.

#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:

1. Logical AND (&&)


The logical AND operator returns true if both operands are true. If either operand is false, the result
is false.

#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;
}

3. Logical NOT (!)


The logical NOT operator reverses the logical state of its operand. If the operand is true, it becomes
false, and vice versa.

#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:

Simple Assignment Operator (=)

This operator assigns the value on the right to the variable on the left.

int x = 10; // x is assigned the value 10

Compound Assignment Operators


These operators combine an arithmetic operation with assignment, making the code more concise.

Addition Assignment (+=)


x += 5; // Equivalent to x = x + 5;

Subtraction Assignment (-=)


x -= 3; // Equivalent to x = x - 3;

Multiplication Assignment (*=)


x *= 2; // Equivalent to x = x * 2;

Division Assignment (/=)


x /= 4; // Equivalent to x = x / 4;

Modulus Assignment (%=)


x %= 3; // Equivalent to x = x % 3;

Example
Here’s a simple program demonstrating the use of assignment operators:

#include <stdio.h>

int main() {
int num = 10;

num += 5; // num = num + 5


printf("+= operator: %d\n", num); // Output: 15

num -= 3; // num = num - 3


printf("-= operator: %d\n", num); // Output: 12

num *= 2; // num = num * 2


printf("*= operator: %d\n", num); // Output: 24

num /= 4; // num = num / 4


printf("/= operator: %d\n", num); // Output: 6

num %= 5; // num = num % 5


printf("%%= operator: %d\n", num); // Output: 1

return 0;
}
Summary:

=: Assigns the value on the right to the variable on the left.


+=: Adds the value on the right to the variable on the left.
-=: Subtracts the value on the right from the variable on the left.
*=: Multiplies the variable on the left by the value on the right.
/=: Divides the variable on the left by the value on the right.
%=: Computes the remainder of dividing the variable on the left by the value on the right.

Increment & Decrement operators

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.

Increment Operator (++)


The increment operator can be used in two forms:

Prefix Increment: ++variable


Postfix Increment: variable++

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

Decrement Operator (-)


The decrement operator also has two forms:

Prefix Decrement: --variable


Postfix Decrement: variable--

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;

printf("Initial values: a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

a++; // Postfix increment


++b; // Prefix increment
c--; // Postfix decrement
--d; // Prefix decrement

printf("After operations: a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

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 and Associativity

In C, operator precedence and associativity determine the order in which parts of an


expression are evaluated. This is crucial for understanding how complex expressions are computed.

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:

1. Postfix: (), [], ->, . (Left-to-Right)


2. Unary: +, -, ++, --, !, ~, &, *, sizeof, (type) (Right-to-Left)
3. Multiplicative: *, /, % (Left-to-Right)
4. Additive: +, - (Left-to-Right)
5. Shift: <<, >> (Left-to-Right)
6. Relational: <, <=, >, >= (Left-to-Right)
7. Equality: ==, != (Left-to-Right)
8. Bitwise AND: & (Left-to-Right)
9. Bitwise XOR: ^ (Left-to-Right)
10. Bitwise OR: | (Left-to-Right)
11. Logical AND: && (Left-to-Right)
12. Logical OR: || (Left-to-Right)
13. Conditional: ?: (Right-to-Left)
14. Assignment: =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= (Right-to-Left)
15. Comma: , (Left-to-Right)
Operator Associativity

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;

Here’s how it is evaluated based on precedence and associativity:

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

Precedence determines which operator is evaluated first.


Associativity determines the order of evaluation for operators of the same precedence.

Evaluation of arithmetic expressions

Evaluating arithmetic expressions in C involves understanding the order in which operations


are performed. This order is determined by operator precedence and associativity, as well as the use
of parentheses to explicitly define the desired order of operations.

Steps for Evaluating Arithmetic Expressions

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));

Here’s how it is evaluated step-by-step:

1. Parentheses: Evaluate the innermost parentheses first.


(5 - 2) → 3
2. Parentheses: Evaluate the next set of parentheses.
(30 / 3) → 10
3. Parentheses: Evaluate the remaining parentheses.
(10 + 20) → 30
4. Multiplication: Finally, perform the multiplication.
30 * 10 → 300
So, the value of result is 300.

Detailed Example with Multiple Operators


Let’s look at a more complex expression:

int result = 10 + 20 * 30 / 5 - 2;

Here’s the step-by-step evaluation based on precedence and associativity:

Multiplication and Division (same precedence, left-to-right associativity):


20 * 30 → 600
600 / 5 → 120
Addition and Subtraction (same precedence, left-to-right associativity):
10 + 120 → 130
130 - 2 → 128
So, the value of result is 128.

Mathematical library functions in C using math.h

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

• Ceiling and floor functions: ceil(), floor()


• Absolute value: fabs()
• Power and square root: pow(), sqrt()
• Logarithmic functions: log(), log10()
• Trigonometric functions: sin(), cos(), tan()

Common Mathematical Functions

Ceiling and Floor Functions

ceil(double x): Returns the smallest integer greater than or equal to x.


floor(double x): Returns the largest integer less than or equal to x.

#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

fabs(double x): Returns the absolute value of x.

#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;
}

Power and Square Root

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

log(double x): Returns the natural logarithm (base e) of x.


log10(double x): Returns the common logarithm (base 10) of x.

#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

sin(double x): Returns the sine of x (in radians).


cos(double x): Returns the cosine of x (in radians).
tan(double x): Returns the tangent of x (in radians).

#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;
}

Type casting & conversion

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.

Types of Type Casting

a)Implicit Type Casting

Performed automatically by the compiler.


Converts a lower data type to a higher data type (e.g., int to float).
Also known as type promotion.
int x = 10;
float y = x; // Implicit conversion from int to float

b) Explicit Type Casting

Performed manually by the programmer.


Can convert any data type to another, even if it results in data loss.
Also known as type demotion or narrowing conversion.
float x = 10.5;
int y = (int)x; // Explicit conversion from float to int

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.

You might also like