Rajarata University of Sri Lanka: ICT1402 Principles of Program Design & Programming
Rajarata University of Sri Lanka: ICT1402 Principles of Program Design & Programming
ICT1402
PRINCIPLES OF PROGRAM DESIGN &
PROGRAMMING
Lecture 07
Type Casting, Command Line Arguments
and Defining Constants
K.A.S.H. Kulathilake
Ph.D., M. Phil., MCS, B.Sc. (Hons) IT, SEDA(UK)
2
Objectives
• At the end of this lecture students should be able
to;
▫ Define type cast and type promotion in C
programming language.
▫ Define command line arguments in C
Programming language.
▫ Declare constants according to the C
programming.
▫ Apply math.h header file for problem solving.
▫ Apply taught concepts for writing programs.
3
Type Casting
• Type cast is an instruction to the compiler to
convert one type into another.
• For example, if you want to store a 'long' value into a
simple integer then you can type cast 'long' to 'int'.
• Here, target-type specifies the desired type to
convert the specified expression to.
• You can convert the values from one type to another
explicitly using the cast operator as follows:
(type_name) expression
4
int main() {
int sum = 17, count = 5;
double mean;
mean = sum / count;
printf("Value of mean : %f\n", mean );
return 0;
}
sum = i + c;
printf("Value of sum : %d\n", sum );
return 0;
}
9
F:\C\L04>CommandLineArguments.exe test
The first argument supplied is CommandLineArguments.exe
The second argument supplied is test
16
argc is 2 here
17
Defining Constants
• There are two ways to define constant in C
programming:
▫ Using the const key word.
const data_type constant_name = constant_value;
const float PI = 3.1412;
▫ Using the #define directive.
▫ #define come before the program main block.
#define constant_name constant_value
#define PI 3.1412
20
#include <stdio.h>
#define PI 3.1412
int main ()
{
float radius;
printf ("Enter the radius in mm : \n");
scanf ("%f",&radius);
int main ()
{
const float PI = 3.1412;
float radius;
printf ("Enter the radius in mm : \n");
scanf ("%f",&radius);
Math Functions
• The math.h header defines various
mathematical functions.
• All the functions available in this library
take double as an argument and
return double as the result.
• You can find documentation via:
▫ http://devdocs.io/c/numeric/math
23
1 double acos(double x)
Returns the arc cosine of x in radians.
2 double asin(double x)
Returns the arc sine of x in radians.
3 double atan(double x)
Returns the arc tangent of x in radians.
4 double atan2(double y, double x)
Returns the arc tangent in radians of y/x based on the signs of both values to
determine the correct quadrant.
5 double cos(double x)
Returns the cosine of a radian angle x.
6 double cosh(double x)
Returns the hyperbolic cosine of x.
7 double sin(double x)
Returns the sine of a radian angle x.
24
13 double log(double x)
Returns the natural logarithm (base-e logarithm) of x.
14 double log10(double x)
Returns the common logarithm (base-10 logarithm) of x.
25
int main ()
{
printf("Value 8.0 ^ 3 = %.2lf\n", pow(8.0, 3));
printf("Sqrt of 100 = %.2f\n", sqrt(100));
printf("log (100) = %.2f\n", log(100.0));
printf("sin (90) = %.2f\n", sin (90.0));
printf("ceil (10.45) = %.2f\n", ceil(10.45));
printf("floor (10.45) = %.2f\n", floor(10.45));
printf("fabs (-10.45) = %.2f\n", fabs(-10.45));
printf("round (10.45) = %.2f\n", round (10.45));
return(0);
}
27
Objective Re-cap
• Now you should be able to:
▫ Define type cast and type promotion in C
programming language.
▫ Define command line arguments in C
Programming language.
▫ Declare constants according to the C
programming.
▫ Apply math.h header file for problem solving.
▫ Apply taught concepts for writing programs.
28
References
• Appendix A, section 5 - Programming in C, 3rd
Edition, Stephen G. Kochan
29
Next: C Operators