CCPROG1
LOGIC FORMULATION AND
INTRODUCTORY
PROGRAMMING
Functions
and Programs
Introduction
• C programs can be made up of different
functions.
• A function is a group of instructions in C
that performs a particular task or as the
name implies, a specific function.
Introduction
• Functions in C are similar to functions in
mathematics, for example, consider the
function f(x):
f(x) = x2 + 1
Basic Program Structure
• The basic structure of a simple C program
is as follows:
Preprocessor Directives
Function Definitions
Main Function
Preprocessor Directives
• Including header files / libraries:
e.g.
#include <stdio.h>
• Defining macros for constants:
e.g.
#define PI 3.1416
The main function
• Syntax:
int main()
{
<variable declaration>
<statements>
return 0;
}
The main function
• Example:
int main()
{
/* variable declaration */
float fBase, fHeight;
float fArea;
/* statements */
fBase = 10.5;
fHeight = 5.0;
fArea = fBase * fHeight;
return 0;
}
Predefined
Functions
C Standard Libraries
• To include the header files:
#include < <header file> >
• Example:
#include <stdio.h>
Function prototypes
• Used to describe functions:
• return_type - the data type of the return value
• function_name - a valid identifier that is associated
with the function
• parameters - zero or more input variables that are
used to pass information to and/or from the function.
Parameters are the values that the function needs in
order to perform a particular task
Standard Input/Output Library
<stdio.h>
• The printf() is used to display strings
and values on the screen
• Syntax:
int printf( const char *format, ... );
Examples of printf()
printf(“Hello World!”);
Hello World
Format String
Conversion
Data Type
Character
c character
d or i decimal integer
f floating-point number
s string
ld long decimal integer
lf double-precision floating-point
* use %% to print percent symbol
Examples of printf()
int nNum = 10;
float fValue = 7.7;
char cKey = 'a';
107.700000a
printf("%d", nNum);
printf("%f", fValue),
printf("%c", cKey);
Examples of printf()
int nNum = 10;
float fValue = 7.7;
char cKey = 'a';
10
printf("%d\n", nNum); 7.700000
printf("%f\n", fValue), a
printf("%c\n", cKey);
/* ‘\n’ is the new line */
Examples of printf()
printf("%d%f%c", nNum, fValue, cKey);
107.700000a
Examples of printf()
printf("%d %f %c", nNum, fValue, cKey);
10 7.700000 a
Examples of printf()
printf("The value of fValue is %f units.\n",
fValue);
The value of fValue is 7.700000 units.
Standard Input/Output Library
<stdio.h>
• The scanf() is is able to capture data as an
input stream of characters from the standard
input device.
• Syntax:
int scanf( const char *format, ... );
Conversion Characters
Conversion
Data Type
Character
c character
d or i decimal integer
f floating-point number
s string
ld long decimal integer
lf double-precision floating-point
* use %% to print percent symbol
Examples of scanf()
int nNum;
float fValue;
char cKey;
_
scanf("%d", &nNum);
scanf("%f", &fValue),
scanf("%c", &cKey);
The C Math Library
<math.h>
• The sqrt() computes the square root of
a number.
• Syntax:
double sqrt(double x);
Examples of sqrt()
double dValue;
float fValue ;
dValue = sqrt (25.0);
printf("%lf\n", dValue); 5.000000
2.50
fValue = sqrt (25.0)/2; 5.0
printf("%.2f\n", fValue);
printf("%.1f\n", sqrt(25));
The C Math Library
<math.h>
• The pow() raises a number to a particular
exponent.
• Syntax:
double pow(double x, double y);
Examples of pow()
double dValue;
float fValue;
dValue = pow(2.0, 4.0);
printf("%lf\n", dValue); 16.000000
9.00
fValue = pow(3,2); 5.000000
printf("%.2f\n", x);
printf("%f\n", pow(25.0, 0.5));
Other Math Functions
Function Function Prototype Description
acos double acos(double x); Calculates the arc cosine of x.
asin double asin(double x); Calculates the arc sine of x.
atan double atan(double x); Calculates the arc tangent of x.
Calculates the double value representing the
ceil double ceil(double x); smallest integer that is greater than or equal
to x.
cos double cos(double x); Calculates the cosine of x.
double exp(double x);
Calculates the exponential function of a
exp
floating-point argument x.
double floor(double x);
Calculates the floating-point value representing
floor
the largest integer less than or equal to x.
log double log(double x); Calculates the natural logarithm of x.
log10 double log10(double x); Calculates the base 10 logarithm of x.
sin double sin(double x); Calculates the sine of x.
tan double tan(double x); Calculates the tangent of x.
The C Standard Library
<stdlib.h>
• The abs() returns the absolute value of a
number.
• Syntax:
int abs(int x)
Examples of abs()
int a, b;
a = abs(-5);
printf("%d\n", a); 5
5
b = abs(5);
printf("%d\n", b);
The C Standard Library
<stdlib.h>
• The rand() returns a pseudo-random
integer in the range of 0 to RAND_MAX
• Syntax:
int rand(void)
Examples of rand()
int a, b;
/ * assigned a random value from 0 to RAND_MAX */
a = rand();
345
printf("%d\n", a); 5
/ * assigned a random value from 1 to 10 */
b = rand()%10 + 1;
printf("%d\n", b);
Examples of
rand() & srand()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{ int nLower, nUpper;
int nRange, nRandom;
printf(Enter the lower and upper bound: ");
scanf("%d%d", &nLower, &nUpper); Enter the lower and
nRange = nUpper - nLower + 1; upper bound: 50 100
srand(time(NULL)); A random number: 76
nRandom = rand() % nRange + nLower;
printf(“\nA random number: %d\n", nRandom);
return 0;
}
Exercises
• Write a program that inputs a 3-digit number and then displays each
of the digits.
• Write a program that asks the user to enter the radius of a circle and
then computes for its area. Recall that the formula to compute for the
area is AREA = 3.1416 * R2 where R is the radius. The output of the
program must be similar to the one below:
The area of the circle with radius 2 cm. is 12.56 sq. cm.
• Write a program that will ask the user that asks for a distance in
kilometers and converts it to its metric equivalent.
• Write a program that inputs two real numbers then exchanges their
values.
Exercises
• Workers at a particular company were given a 15.5% salary
increase. Moreover, the increase was effective 2 months ago.
Write a program that takes the employee’s old salary as input
and output the amount of retroactive pay (the increase that
wasn’t given for the past 2 months) and the employee’s new
salary.
• Create a program that will get the roots of a quadratic
equation given a, b, and c of the equation: ax2 + bx + c.
Assume that there are two roots. The roots are obtained by
the following equation:
−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝒙=
𝟐𝒂
References:
Hanly, J. and Koffman, E. (2012). Problem Solving and Program Design in C (7th
Edition). Pearson.
Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd.
ed.). Pearson.
Acknowledgments:
This course material is a revised edition of the Fundamentals of C Programming, a
course material for CMPROG1 by Nathalie Lim-Cheng in 2002. The development
of this material was funded by the University Research Coordination Office
(URCO) of De La Salle University, Manila.