5992 - File - Week 9 To 10 Function
5992 - File - Week 9 To 10 Function
COURSE OUTLINE
COURSE CODE : IT
TITLE : Introduction to C Programming 2
TARGET POPULATION : All BS Information Technology Students
INSTRUCTOR : MA. ANJELLY E. FUSINGAN
Overview:
Content
Strings
Arithmetic Functions
Objectives:
STRINGS
Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-
terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null
character at the end of the array, the size of the character array containing the string is one more than
the number of characters in the word "Hello."
If you follow the rule of array initialization, then you can write the above statement as follows:
Actually, you do not place the null character at the end of a string constant. The C compiler automatically
places the '\0' at the end of the string when it initializes the array. Let us try to print the above
mentioned string:
#include<stdio.h>
int main ()
return 0;
When the above code is compiled and executed, it produces the following result:
Greeting message: Hello
8 strlwr(s1)
Returns string characters in lowercase.
9 Strupr(s1)
Returns string characters in uppercase.
#include<string.h>
int main ()
strcpy(str3, str1);
len = strlen(str1);
return 0;
When the above code is compiled and executed, it produces the following result:
strlen(str1) : 10
Arithmetic functions
C Programming allows us to perform mathematical operations through the functions defined in
<math.h> header file. The <math.h> header file contains various methods for performing mathematical
operations such as sqrt(), pow(), ceil(), floor() etc.
Function Description
This function returns the absolute value of an integer. The absolute value of a number is
abs ( ) always positive. Only integer values are supported in C.
This function returns the nearest integer which is less than or equal to the argument passed
floor ( ) to this function.
This function returns the nearest integer value of the float/double/long double argument
passed to this function. If decimal value is from “.1 to .4”, it returns integer value less than
the argument. If decimal value is from “.5 to .9”, it returns the integer value greater than the
round ( ) argument.
This function returns nearest integer value which is greater than or equal to the argument
ceil ( ) passed to this function.
sqrt ( ) This function is used to find square root of the argument passed to this function.
trunc ( ) This function truncates the decimal value from floating point value and returns integer value.
Example
#include<stdio.h>
#include <math.h>
int main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12)); return 0; }
Output
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12