0% found this document useful (0 votes)
11 views13 pages

C Arithmetic Operators and Functions

The document provides an overview of basic arithmetic operations and operators in C programming, including addition, subtraction, multiplication, division, and modulus. It explains the difference between expressions and statements, operator precedence, and includes examples of commonly used built-in math functions from the <math.h> library. Key functions such as sqrt, pow, sin, and log are highlighted with their usage examples.

Uploaded by

delfinjarvisgyle
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

C Arithmetic Operators and Functions

The document provides an overview of basic arithmetic operations and operators in C programming, including addition, subtraction, multiplication, division, and modulus. It explains the difference between expressions and statements, operator precedence, and includes examples of commonly used built-in math functions from the <math.h> library. Key functions such as sqrt, pow, sin, and log are highlighted with their usage examples.

Uploaded by

delfinjarvisgyle
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

BASIC MATH OPERATIONS

ARITHMETIC OPERATORS IN C

• Arithmetic operators perform basic mathematical operations

Operator Description Example

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

/ Division a/b

Modulus (remainder
% a%b
after integer division)
ARITHMETIC OPERATORS
EXPRESSIONS

• An expression in C is a combination of operands (variables,


constants, literals) and operators that computes a value.
Examples include:
• 10 + x * y
• a+b–c
• x/y+z
• Expressions can be simple (like a single variable) or complex
(involving multiple operators and function calls).
STATEMENTS

• A statement in C is an instruction to the computer to perform an action.


The most common types are:
• Expression Statements: An expression followed by a semicolon, causing the
expression to be evaluated.
• Example: c = a + b;
• Compound Statements: A group of statements enclosed in braces { } .
• Example:
{
float pi = 3.141593;
float circumference = 2.0 * pi * radius;
float area = pi * radius * radius;
}
• Control Statements: Include selection (if, switch) and iteration (for, while, do-
while) statements
EXPRESSIONS VS STATEMENTS

• Expressions combine variables, constants, and operators to


compute values.
• Statements are instructions that perform actions, often using
expression.
OPERATOR PRECEDENCE

Operator precedence determines the order in which operators are


evaluated in expressions. Operators with higher precedence are
evaluated before those with lower precedence.
KEY PRECEDENCE RULES

• Parenthesis ( ) have the highest precedence: expressions inside


them are evaluated first.
• Multiplication * , division /, and modulus % have higher
precedence than addition + and subtraction -.
OPERATOR PRECEDENCE

int x = (7 + 3) * 2;
printf(“%d”, x);

• Here 7 + 3 is evaluated first because the expression is found


inside the parenthesis (resulting to 10), then multiplied by 2
(10 * 2), then finally the resulting value (20) is assigned to the
variable x.
• Performing the printf() statement will output in the console:
20
OPERATOR PRECEDENCE TABLE

Precedence Operators Associativity


Highest () Left to right
*/% Left to right
+- Left to right

Lowest = =
COMMONLY USED BUILT-IN MATH
FUNCTIONS IN C

• C provides a rich set of built-in mathematical functions in


the <math.h> standard library.
• These functions cover a wide range of operations, including
arithmetic, trigonometry, exponentiation, logarithms, rounding,
and more.
• To use these functions, include the math.h header at the
beginning of the program
• These built-in functions are essential for performing advanced
mathematical calculations efficiently in C programs.
EXAMPLES OF COMMON MATH
FUNCTIONS
Function Description Example Usage
sqrt(x) Square root of x sqrt(16.0) → 4.0
pow(x, y) x raised to the power y pow(2.0, 3.0) → 8.0
sin(x) Sine of x (x in radians) sin(3.14159/2) → 1.0
cos(x) Cosine of x (x in radians) cos(0) → 1.0
tan(x) Tangent of x (x in radians) tan(0) → 0.0
log(x) Natural logarithm (base e) of x log(2.71828) → 1.0
log10(x) Base-10 logarithm of x log10(100.0) → 2.0
exp(x) Exponential of x (e^x) exp(1.0) → 2.71828
fabs(x) Absolute value of x fabs(-5.0) → 5.0
ceil(x) Smallest integer not less than x ceil(2.3) → 3.0
floor(x) Largest integer not greater than x floor(2.7) → 2.0
fmod(x, y) Floating-point remainder of x/y fmod(7.0, 3.0) → 1.0
acos(x) Arc cosine of x (result in radians) acos(1.0) → 0.0
asin(x) Arc sine of x (result in radians) asin(0.0) → 0.0
atan(x) Arc tangent of x (result in radians) atan(1.0) → 0.785398
round(x) Rounds x to the nearest integer round(2.5) → 3.0
EXAMPLES OF COMMON MATH
FUNCTIONS

You might also like