0% found this document useful (0 votes)
5K views9 pages

Topic2 The C Fundamentals Part2

The document discusses C arithmetic expressions and operators. It notes that C provides common arithmetic operators like addition, subtraction, multiplication and division. It also describes operands, parentheses to control order of operations, and type casting. The document provides examples of C arithmetic expressions and discusses string formatting using printf format specifiers. It concludes with some common programming errors in C like uninitialized variables, division by zero, mismatched format specifiers, infinite loops and type mismatch.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5K views9 pages

Topic2 The C Fundamentals Part2

The document discusses C arithmetic expressions and operators. It notes that C provides common arithmetic operators like addition, subtraction, multiplication and division. It also describes operands, parentheses to control order of operations, and type casting. The document provides examples of C arithmetic expressions and discusses string formatting using printf format specifiers. It concludes with some common programming errors in C like uninitialized variables, division by zero, mismatched format specifiers, infinite loops and type mismatch.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

The C Fundamentals

Part 2
C Arithmetic Expressions
Operators
C provides several operators for performing arithmetic operations. The most common
ones include:

‘+’ (Addition): Adds two operands.


‘-’ (Subtraction): Subtracts the right operand from the left operand.
‘*’ (Multiplication): Multiplies two operands.
‘/’ (Division): Divides the left operand by the right operand.
‘%’ (Modulus): Returns the remainder after division of the left operand by the right
operand.
‘++’ (Increment): Increases the value of a variable by 1.
-- (Decrement): Decreases the value of a variable by 1.
C Arithmetic Expressions
Operands
Are the values or variables on which arithmetic operations are performed. They can be literal
constants (e.g., 5, 3.14), variables (e.g., x, y), or expressions (e.g., x + y).

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

The first assignment integerResult = doubleValue; attempts to


implicitly convert the double value to an int. However, this
implicit conversion truncates the fractional part, resulting in data
loss. The value 3.14159 becomes 3 when assigned to an int
variable.

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.

•%d is a format specifier for an integer (age).

•%.2f is a format specifier for a floating-point number


(height) with two decimal places.
String Formatter
Common Format specifiers for ‘printf’

%d, %i: Integers


%f: Floating-point numbers
%c: Characters
%s: Strings
%x, %X: Integers in hexadecimal format
%o: Integers in octal format
%p: Pointers
%u: Unsigned integers
Common Programming Errors
Uninitialized Variables:
Using variables before they have been initialized. The value of an uninitialized variable is undefined.

Division by Zero:
Performing division where the divisor is zero. This results in undefined behavior.

Mismatched Format Specifiers:


Providing incorrect arguments to printf or scanf, which can lead to incorrect output or input.
Common Programming Errors
Infinite Loops:
Creating loops that never terminate due to incorrect loop conditions.

Type Mismatch:
Assigning or passing values of incompatible data types, which can result in unexpected behavior or errors.

You might also like