Topic2 The C Fundamentals Part2
Topic2 The C Fundamentals Part2
Part 2
C Arithmetic Expressions
Operators
C provides several operators for performing arithmetic operations. The most common
ones include:
Parentheses
Parentheses ‘()’ can be used to group expressions and control the order of operations. Expressions
inside parentheses are evaluated first.
Operator Precedence:
Parentheses () can be used to group expressions and control the order of operations. Expressions
inside parentheses are evaluated first.
Type Casting
In C, it's important to be aware of the data types of operands in arithmetic expressions. If you mix
different data types (e.g., integers and floating-point numbers), you may need to perform type
casting to ensure the correct result.
C Arithmetic Expressions
Type Casting
also known as type conversion, is the process of converting a value from one data type to another
in a programming language.
Implicit Conversion
Implicit Conversion
The second assignment integerResult = (int)doubleValue; performs an
explicit type casting using the (int) syntax. This conversion explicitly converts
the double value to an int, discarding the fractional part without generating a
compilation error. The value 3.14159 is explicitly cast to 3 and assigned to
the integerResult variable.
C Arithmetic Expressions
Examples of C arithmetic expressions:
String Formatter
There isn't a built-in string formatting function like you might find in some other programming languages (e.g., Python's
str.format() or C#'s string.Format). However, C provides a powerful and flexible way to format strings using the printf
function and its related functions, such as sprintf and snprintf.
“Printf”
is a standard library function in C that is used to format and print data to the standard output (usually the console). It
uses format specifiers to control the formatting of data. These format specifiers are placeholders in the string where
data is inserted and formatted.
Division by Zero:
Performing division where the divisor is zero. This results in undefined behavior.
Type Mismatch:
Assigning or passing values of incompatible data types, which can result in unexpected behavior or errors.