TP-02 DSP Basic Type

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

វិទ្យាស្ថយនបច្ចេកវិទ្យាកម្ពុជា

Institute of Technology of Cambodia

TP-02
Basic Data Types, Operators and Math
in C/C++

Academic Year: 2023 - 2024


ITC AMS

1. Data Types

A variable in C must be a specified data type:

Type Size (bytes) Format Specifier

int at least 2, usually 4 %d , %i

char 1 %c

float 4 %f

double 8 %lf

short int 2 usually %hd

unsigned int at least 2, usually 4 %u

long int at least 4, usually 8 %ld , %li

long long int at least 8 %lld , %lli

unsigned long
at least 4 %lu
int

unsigned long
at least 8 %llu
long int

signed char 1 %c

unsigned char 1 %c

long double at least 10, usually 12 or 16 %Lf

2. Operators

Operators are used to perform operations on variables and values.

C divides the operators into the following groups:


• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators

Data Structure and Programming-I Page 2


ITC AMS

Arithmetic Operators

Example:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
Assignment operators

Example:
int x = 10;
x += 5;

Data Structure and Programming-I Page 3


ITC AMS

Comparison operators

Example:
int x = 5;
int y = 3;
printf(x > y); //returns 1(true) because 5 is greater than 3

Logical operators

Example:
int x = 5;
int y = 3;
printf (x > y && x < 10); // returns 1 (true)
printf (x > y || x < 10); // returns 1 (true)
printf (!(x > y && x < 10)); // returns 0 (false)

3. C Math
C has many functions that allows you to perform mathematical tasks on numbers.

Max and min


The max(x,y) function can be used to find the highest value of x and y.
And the min(x,y) function can be used to find the lowest value of x and y.

Data Structure and Programming-I Page 4


ITC AMS

Example:
printf max(5, 10); // returns 10
printf min(5, 10); // returns 5

C <math.h> Header
Other functions, such as sqrt (square root), round (rounds a number)
and log (natural logarithm), can be found in the <math.h> header file:

Example:
// Include the cmath library
#include <math.h>

printf sqrt(64); // returns 8


printf round(2.5); // returns 3
printf pow(2, 3); // returns 8

Other Math Functions

Function Description

abs(x) Returns the absolute value of x

acos(x) Returns the arccosine of x

asin(x) Returns the arcsine of x

atan(x) Returns the arctangent of x

cbrt(x) Returns the cube root of x

ceil(x) Returns the value of x rounded up to its nearest integer

cos(x) Returns the cosine of x

cosh(x) Returns the hyperbolic cosine of x

exp(x) Returns the value of Ex

expm1(x) Returns ex -1

fabs(x) Returns the absolute value of a floating x

fdim(x, y) Returns the positive difference between x and y

floor(x) Returns the value of x rounded down to its nearest integer

hypot(x, y) Returns sqrt(x2 +y2) without intermediate overflow or underflow

fma(x, y, z) Returns x*y+z without losing precision

Data Structure and Programming-I Page 5


ITC AMS

fmax(x, y) Returns the highest value of a floating x and y

fmin(x, y) Returns the lowest value of a floating x and y

fmod(x, y) Returns the floating point remainder of x/y

pow(x, y) Returns the value of x to the power of y

sin(x) Returns the sine of x (x is in radians)

sinh(x) Returns the hyperbolic sine of a double value

tan(x) Returns the tangent of an angle

tanh(x) Returns the hyperbolic tangent of a double value

Problem1:
Write a C program to display the following message:
--
*****************************************
** Institute of Technology of Cambodia **
*****************************************

############################################################
\\ Department of Information and Communication Engineering \\
############################################################

#########################################
// Génie Informatique et Communication //
#########################################
--

Problem2:
Write a C program to get a number from a user. Then find the square as well as the
cube of that number.
Ex:
Input: 2
Output: Square of 2 is 4 AND Cube of 2 is 8.

--

Data Structure and Programming-I Page 6


ITC AMS

Problem3:
Write a C program that can convert the time (hour, minute, second) to second
Enter hour : 5
Enter minute : 45
Enter second : 50
=> The above time is equal to 20750 seconds.
--

Problem4:
Write a C program to convert time in second to a
time format including hour, minute and second.
Ex:
Input time in second (s) : 10000
=> It is: 2h 46mn 40s

--

Problem5:
Write a C program that can calculate some formula and display result. This program
asks a user to input x and y then compute all formula below:
f1 = 2x - 3y
f2 = 3x/2
f3 = x^3 - 5x/2 + y^(3/2)
f4 = (x + y^(1/2)) / (2x)
f5 = sin(x) + tan(y)

Remark: create x and y as float


--

Data Structure and Programming-I Page 7

You might also like