Lecture 4 Library Functions and Header Files
Lecture 4 Library Functions and Header Files
Files
Function
vFunction: A function is a block of codes that performs specific
tasks.
Dissection of a Function
Function
Header int add( int x, int y )
{
Function int sum = x + y;
return sum;
Body
Return Statement
}
How do connect Functions?
#include <stdio.h>
int main()
{
Function call
int n1, n2, result;
Calling function
……………………………
result = add(n1, n2);
…………………………….
}
i = integer d = double
Some important library functions
Function Return Type Purpose
ceil(d) int Round up to the next integer value
floor(d) int Round down to the next integer value
round(d) int Round to the nearest integer value
exp(d) double Raise e to the power d
log(d) double Return natural logarithm of d
fmod(d1, d2) double Return remainder of d1/d2
sqrt(d) double Return the square root of d
rand() int Return a random positive integer
srand(u) void Initialize the random number generator
toascii(c) int Convert to ASCII
tolower(c) int Convert letter to lower case
toupper(c) int Convert letter to upper case
int i = 123456;
float x = 345.678;
² printf(“%3f %10f %13f”, x, x, x) Six digits will be displayed after decimal point
Output: 345.678000 345.678000 345.678000
char line[12];
printf(“:%15s %15.5s %.5s:\n”, line, line, line);
printf(“:%-15s %-15.5s %-.5s:\n”, line, line, line);
Output:
: lower-case lower lower:
:lower-case lower lower:
printf() function
int i = 1234, j=01777, k =0xa08c;
float x = 12.0, y = -3.3;
OUTPUT:
: 1234 1777 a08c 12 -3.3e+00:
: 1234 1777 a08c 12 -3.3e+00 :
: +1234 +1777 +a08c +12 -3.3e+00:
: +1234 +1777 +a08c +12 -3.3e+00 :
: 1234 01777 0xA08C:
: 00001234 00001777 0000A08C:
: 12 12. -3.3 -3.300000:
printf() function
Conversion Character Meaning
c Character
d Decimal integer
hd or h Short integer
ld Long integer
D Long integer
u Unsigned integer
o Octal
x Hexadecimal
i Integer, decimal or hexadecimal or octal
f Floating point number
lf Double
e Floating point in exponential or scientific notation
le Double in exponential notation
s string
[…] String
printf() function
Flag Meaning
- Left justified
+ A sign (either + or -) will precede each signed numeric data item
0 Causes leading zeros
“ Blank space
# ² 0 and 0x will be displayed before octal and hexadecimal number
² A decimal point will be displayed in all floating point numbers, even
if the data item is a whole number.