WEEK 9 to 10
COURSE OUTLINE
COURSE CODE : IT
TITLE : Introduction to C Programming 2
TARGET POPULATION : All BS Information Technology Students
INSTRUCTOR : MA. ANJELLY E. FUSINGAN
Overview:
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to
develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11
computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and
essentially all UNIX application programs have been written in C.
Content
Strings
Arithmetic Functions
Objectives:
To be able to use the function of the character handling library
To be able to use the string and character input/output functions of the
standard input/output
To be able to use the string conversions functions of the general utilities library.
Instruction to the Learner
Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose.
The units are characterized by continuity, and are arranged in such a manner that the present unit is
related to the next unit. For this reason, you are advised to read this module. After each unit, there are
exercises to be given. Submission of task given will be every Monday during your scheduled class hour.
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."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization, then you can write the above statement as follows:
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C/C++:
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 ()
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
When the above code is compiled and executed, it produces the following result:
Greeting message: Hello
C supports a wide range of functions that manipulate null-terminated strings:
S.N. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
7
strrev(s1)
Returns reverse of the given string.
8 strlwr(s1)
Returns string characters in lowercase.
9 Strupr(s1)
Returns string characters in uppercase.
The following example uses some of the above-mentioned functions:
#include<stdio.h>
#include<string.h>
int main ()
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12]; int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
When the above code is compiled and executed, it produces the following result:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
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.
pow ( ) This is used to find the power of the given number.
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