UNIT – 2 :
INPUT/OUTPUT STATEMENT AND OPERATORS :
• UNIT-2: Input/Output Statements and Operators:
➢ 2.1 Input/Output statements:
• 2.1.1 Concepts of Header files (STDIO,CONIO)
• [Link] Concepts of pre-compiler directives.
• [Link] Use of #include and #define
➢ 2.2 Input/Output Statements:
• 2.2.1 Input statements : scanf(), getche(), getch(), gets(), getchar()
• 2.2.2 Output Statements: printf(), putch(),puts(), putchar()
• 2.2.3 Type specifiers (formatting strings) : %d, %ld, %f, %c, %s, %lf
➢ 2.3 Operators :
• 2.3.1 Arithmetic operators ( +, -, *, /, %, ++, --, )
• 2.3.2 Logical Operators ( &&, ||, ! )
• 2.3.3 Relational Operators ( >,<, ==, >=,<=, != )
• 2.3.4 Bit-wise operators ( &, |, ^ , ~,<<,>>)
• 2.3.5 Assignment operators ( =, +=, -=, *=, /=, %=)
• 2.3.6 Ternary Operator and use of sizeof() function.
➢ 2.4 Important Built-in functions:
• 2.4.1 Use of<string.h> : ( strlen, strcmp, strcpy, strcat, strrev)
• 2.4.2 Use of <math.h>: (abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(), tan(), pow() and trunc())
INPUT/OUTPUT STATEMENT :
➢C language provide us console input/output functions.
Read the input from the keyboard by the user accessing the console.
Display the output to the user at the console.
Concept of Header Files(STDIO,CONIO):
• In Link section we have to include some header [Link] that we can use
inbuilt functions of that particular file.
• For ex .if you want to use math function than it need to include
<math.h>file.
• Before a C program is compiled in a compiler, source code is processed
by a program called pre-processor. this process is called pre-processing.
• Pre-processor directives will be written in link section of a C program.
Concept of Header Files(STDIO,CONIO):
• STDIO: This is Standard buffered input/output [Link]
Contains inbuilt function like printf(),scanf(),etc.
• CONIO: This is console input/output [Link] header
declares several useful library functions for programming
“console input and output” from a [Link] contains
inbuilt function like clrscr(), getch()
Concept of pre-complier directives
• Commands used in pre-processor are called pre-processed
directives and they begin with the “#” symbol.
• The C Pre-processor is just a text substitution tool, and it instructs
the compiler to do the required pre-processing before the actual
compilation process
• Pre-processor directives will be in the Link Section of a C program.
[Link]. Directive & Description
1 #define
Substitutes a preprocessor macro.
2 #include
Inserts a particular header from another file.
3 #undef
Undefines a preprocessor macro.
4 #ifdef
Returns true if this macro is defined.
5 #ifndef
Returns true if this macro is not defined.
6 #if
Tests if a compile time condition is true.
7 #else
The alternative for #if.
8 #elif
#else and #if in one statement.
9 #endif
Ends preprocessor conditional.
10 #error
Prints error message on stderr.
11 #pragma
Issues special commands to the compiler, using a standardized method.
#include :
• The #include <header file.h> the angle brackets say look in standard system
directives.
• The #include “myfile.h" the quotation marks say look in current directives.
• Syntax:-
#include <header file.h> or #include “myfile.h"
# define :
• In the C Programming, The #define pre-processor directive is used to “define”
pre-processor “variable”, “constant” or “macro”.
• This pre-processor directive can be used to replace a word with a number
globally.
• It acts as if an editor did a global search-and-replace edit of the file.
• We can use the #define directive for the debugging purpose. We can have
print statements that will be only active while debugging.
• Syntax :
#define constant_name value OR
#define constant_name(expression)
• Constant_name: The name of the constant.
• Value: The value of the constant.
• Expression: It is an expression whose value is assigned to the constant.
The expression must be enclosed in parentheses if it contains any operators.
Use Of #include and #define:
➢Macro Definition (Macro Substitution Using #define)
• In C programming, a macro is a symbolic name or constant that
represents a value, an expression, or a piece of code.
• Macros are defined using the #define preprocessor directive. When
the C preprocessor encounters a macro name in the source code, it
replaces that name with its defined content before the actual
compilation process begins.
• Syntax :
#define identifier string
Types Of Macro Substitution Which Are Given
Below :
• 1. Simple Macro Substitution
Example :
#include<stdio.h>
#include<conio.h>
#define PI 3.14
[Link] Macro Substitution(macro with
argument)
Example :
#include<stdio.h>
#include<conio.h>
#define AREA(r) 3.14*(r*r)
INPUT/OUTPUT STATEMENTS :
• Input Statement : scanf(),getch(),gets(),getchar()
• Output Statement : printf(),putch(),puts(),putchar()
➢ C language provide us console input/output functions.
• Read the input from the keyboard by the user accessing the console.
• Display the output to the user at the console.
Formatted Input and Output
• 1. printf() Function:
• It is a formatted output function. The printf() function is used to print the data
in specified format from the computer’s memory onto a standard output device.
• The function is included in <stdio.h> file.
• Syntax of printf(): printf(“control string”, arg1, arg2,….., argn);
• printf function is used to print the “character,string,float,integer,octal and
hexadecimal values” onto the output screen.
• We use printf ()function with %d format specifiers to display the value of an
integer variable,similarly %c is used to display character,%f for float,%s for
string,%lf for double and %x for hexadecimal variable.
• To generate a newline,we use “\n” in c printf statement.
2. scanf() Function:
• In C programming language, scanf is a function that stands for Scan
Formatted String. It reads data from stdin (standard input stream i.e. usually
keyboard) and then writes the result into the given arguments.
• The scanf() function reads data from the input device and stores them at the
given memory location.
• It can handle any built-in type and strings. The function is included in
<stdio.h> file.
• Syntax: scanf(“control string”, &var1, &var2..);
• Why &?
• While scanning the input, scanf needs to store that input data somewhere. To
store this input data, scanf needs to known the memory location of a variable.
And here comes the ampersand to rescue.
• & is also called as address of the operator.
• For example, &var is the address of var.
• It accepts character, string, and numeric data from the user using standard
input.
scanf also uses format specifiers like printf.
Example type specifiers recognized by scanf:
%d to accept input of integers.
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.
Unformatted Input function : "Thisfunction reads a
character from the
keyboard but doesn't
1. getch(): show it on the screen."
• This function is used to read a character from the console but does not
echo to the screen. This function is included in header file <conio.h>
• Syntax: variable_name=getch( );
• Another use of this function is to maintain the output on the screen until
you have not to press the Enter key. getch() works only on dos like TC
compiler. It does not work on a Linux platform.
2. getche():
• getche( ) function is used to read a character from the console and echoes that
character to the screen. This function is included in header file <conio.h>.
• Syntax: variable_name=getche( );
• The main difference between getch() and getche() is getch() does not echo
character after reading, while getche() echoes character after reading.
• It does not use any buffer, so the entered character is immediately returned
without waiting for the enter key. getche() works only on DOS-like TC compiler.
It does not work on a Linux platform.
Unformatted Output function :
• 3. putch():
• putch() function displays or writes single character to the standard output
device(i.e. stdout). This function is defined in <conio.h> header file.
• Syntax: putch(variable_name);
Character Input and Output Functions in C :
1. getchar( ):
• It reads a single character from input device i.e. stdin. This function is defined
in <stdio.h> header file.
• Syntax: int getchar( );
2. putchar():
• It displays or writes a single character to the standard output device or screen
at the current cursor position. This function is defined in <stdio.h> header file.
• Syntax: int putchar(int c);
String Input and Output Functions in C
• 1. gets():
• gets() function accepts single or multiple characters of string including
spaces from the standard input device until ENTER key is pressed. This
function in <stdio.h> header file.
• Syntax: gets(var_name);
• gets() is also buffered function. It will read a string from the keyboard until
the user presses the Enter key from the keyboard. It will mark the null
character (‘\0’) in the memory at the string when you press the enter key.
• gets stops when either the newline character is read or when the end-of-
file(EOF) is reached, whichever comes first.
• 2. puts():
• puts() function displays or writes a string to the standard output device.
It appends a newline character to string. This function is defined in
<stdio.h> header file.
• Syntax: puts(var_name);
Type of specifiers(formatting string):
• Format specifiers in c are used for input and output purpose.
• The format specifier in C programming is used for input and output purposes.
Through this, we tell the compiler what type of variable we are using for input
using scanf() or printing using printf(). Some examples are %d, %c, %f, etc.
• Using a format specifier the compiler can understand that what type of data is
in input and output operation.
Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i Unsigned integer
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
What is Escape Sequence in C?
• Many programming languages supports the concept of Escape Sequence.
• An escape sequence is a sequence of characters which are used in formatting
the output.
• They are not displayed on the screen while printing. Each character has its
own specific function like \t is used to insert a tab and \n is used to add
newline.
Types of Escape Sequence in C
Escape Sequence Description
\t Inserts a tab
\b Inserts a backspace
\n Inserts a newline.
\r Inserts a carriage return.
\f Inserts a form feed.
\’ Inserts a single quote character.
\” Inserts a double quote character.
\\ Inserts a backslash character.
Operators :
• What is an operator?
• An operator is a symbol used to perform operations in given programming
language.
• Special symbols that are used to perform actions or operations are
known as operators.”
• For example, the symbol plus (+) is used to perform addition so it is an
operator.
Ex. 3 + 4 = 7
A+B
OPERATOR
OPERAND 1 OPERAND 2
NOTE: SINGLE VARIABLE ON THE LHS
• Depending on the number of operands that an operator can act upon, operator
operator can be classified as follows :
• [Link] Operators : those operators that require only single operand to act
upon are known as unary operator.
• [Link] Operators : those operators that require only two operand to act
upon are called binary operators.
• [Link] Operators : these operator requires three operands to act upon.
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical function.
Types Of Operator In C Language :
• Arithmetic operator
• Relational operator
• Logical operator
• Bitwise operator
• Assignment operator
ARITHMATIC OPERATORS
Arithmetic operators are used to perform mathematical operations such as addition,
subtraction etc. Few of the simple arithmetic operators are :
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
ARITHMATIC INSTRUCTION :
VALID INVALID
a = b+c ; b+c=a;
a = b*c ; a = bc ;
a = b/c ; a = b^c;
NOTE – pow(x,y) for x to the power of y
ARITHMATIC INSTRUCTION :
Type Conversion :
• int op int int
• int op float float
• float op float float
Operator Precedence :
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Associativity for the same precedence:
Multiplicative * / % Left to right
Additive +- Left to right
Left to right associativity.
Shift << >> Left to right
Relational < <= > >= Left to right
x = 4*3/6*2
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Relational operator :
Relational operators are used for the comparison between two or more
numbers. Same as Java, C also has six relational operators and their return
value is in Boolean i.e. either True or False (1 or 0).
Operator Description
== Is equal to
!= Is not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Logical Operator :
There are three logical operators i.e. AND, OR and NOT. They can be used to compare Boolean values but are
mostly used to compare conditions to see whether they are satisfying or not.
AND: it returns true when both operators are true or 1.
OR: it returns true when either operator is true or 1.
Not: it is used to reverse the logical state of the operand.
Operator Description Example
&& Logical AND Operator .If both the operands are non zero , (A && B) is false
then the condition is true
|| Logical OR Operator. If any of these two operands is non zero, (A || B) is true
then condition becomes true
! Logical NOT Operator. It is used to reverse the logical state of !(A && B) is true
its operand , If condition is true , then Logical NOT operator
will make it false.
Assignment Operator :
• Assignment operators are used to assign values. They are going to be used in
each and every one of our program.
Operator Description
= Assigns values from right side operands to left side operand
Add AND assignment [Link] adds the right operand to the left operand and assign the result to the left
+=
operand.
Subtract AND assignment [Link] subtracts the right operand from the left operand and assigns the result to
-=
the left operand.
Multiply AND assignment [Link] multiplies the right operand with the left operand and assigns the result to
*=
the left operand.
Divide AND assignment [Link] divides the left operand with the right operand and assigns the result to the
/=
left operand.
Bitwise Operator :
• To perform bit-level operations, bitwise operators are used. They convert the values we
provide to them in binary format and then compare them to provide us the results.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise One’s Complement
<< Bitwise Left
>> Bitwise Right
Truth table of the bitwise operator :
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Ternary operator :
• Conditional Operators[?:]:Ternary Operator
• They are also called as ternary operator.
• Ternary operator takes on 3 arguments.
• Syntax:
expression 1? expression 2 : expression 3
• Where
• expression1 is Condition.
• expression2 is Condition is true.
• expression3 is Condition is false.
Miscellaneous Operators :
Operator Description Example
Sizeof() Return the size of variable. Sizeof(a),where a is integer,will returns int’s size on
that architecture.
& Return the address of variable. &a; return the actual address of the variable.
* Pointer to variable. *a;
?: Conditional expression If condition is true?: then value X: Otherwise value Y
Size of operator :
• sizeof operator is used to calculate the size of data type or variables.
• sizeof operator can be nested.
• sizeof operator will return the size in integer format.
• sizeof operator syntax looks more like a function but it is considered as an
operator in c programming.
Increment and Decrement Operators
• Two most commonly used unary operators are increment operator (++) and
the decrement operator(–).
• The increment ++ operator causes its operand to increase by one. The
decrement — operator causes its operand to be decreased by one. The
operand used with each of these operators must be a single variable.
• Pre-Increment Operator:
• If the operator ++ precedes the operand, it is pre-increment i.e., ++y. It is used
to increment the value of the variable before using the expression.
• Post-Increment Operator:
• If the operator ++ follows the operand, it is post-increment i.e. y++.
• In the post-increment value is first used in an expression and then
incremented.
• Pre-Decrement Operator:
• If the operator -- precedes the operand, it is pre-decrement i.e., --y. It is used
to decrement the value of the variable before using the expression.
• In the pre-decrement, value is first decremented and then used inside the
expression.
• Post-Decrement Operator:
• If the operator -- follows the operand, it is post-decrement i.e., y--. In the
Post-Decrement value is first used in an expression and then decremented.
STRING IN C :
•String is an array of characters. Data of the same
type are stored in an array for example, Integers
can be stored in an integer array, similarly, a group
of characters can be stored in a character array.
• Character arrays are also called strings. A string is a
one-dimensional array of characters that is
terminated by a null (‘\0’).
Declaration of strings:
• Declaring a string is very simple, same as declaring a
one-dimensional array. Below is the syntax for
declaring a string.
Syntax : char string_name[size];
• In the above syntax, string_name is any name given to the string variable,
and size is used to define the length of the string, i.e the number of
characters that the strings will store.
• Keep in mind that there is an extra terminating character which is the null
character (‘\0’) that is used to indicate the termination of string.
• char name[ ] = { ‘P', ‘R', ‘O', ‘G', ‘R’, ’A’, ’M’, '\0' } ;
• char name [ ] = “PROGRAM”;
• Each character in the array, like “P”, occupies one byte of memory and the
last character is always ‘\0’. The null character ‘\0’ looks like two characters,
but it is actually only one character, with the \ indicating that what follows it
is something special. Character array elements are stored in contiguous
memory locations.
• The placeholder for string variables is %s.
• Note: There is a difference between ‘\0’ and ‘0’. The ASCII value of ‘\0’
is 0, and for ‘0’, the value is 48. The terminating null ‘\0’ and ‘0’ are not
the same.
• The null (‘\0’) is important in C programming because it is the only way
the functions that work with a string can know where the string
ends. When a string not terminated by a ‘\0’, then it is not really a
string but merely a collection of characters.
• In order to read a string that contains the spaces, we use the gets()
function.
• The purpose of gets is to ignore the whitespaces.
• When a newline is reached, gets stops reading. For example:
• Conclusion:-
• We have learned about how to use strings in C programming.
• Each member of the array contains one of the characters in the string. By using scanf()
we are not capable of receiving multi-word strings. The way to get a multi-word string is by
using the function gets().
#include <stdio.h>
#include<conio.h>
void main()
{
char name[50];
printf("Enter your name: ");
gets(name);
printf("My name is %s ",name);
getch();
}
#include <stdio.h> Syntax :
#include<conio.h> fgets(str,n,file);
void main()
//stops when n-1 chars input or
{
new line is entered
char name[50];
printf("Enter your name: ");
gets(name); //fgets(name,50,stdin);
printf(“Using printf is %s ",name);
printf(" Using puts is %s ",name);
puts(name);
getch();
}
String Functions In C & string.h Library:
• we will learn to manipulate strings in C using different library functions. C
provides us the useful string handling library functions.
• The string.h library is used to perform string operations. It provides several
functions for manipulating character strings.
• We need to often manipulate or change the strings according to the need
of a problem, the string.h library makes handling string in programming
simple and easy to understand.
• Following are some commonly used string handling functions:
• strlen( ):-
• This function is used to count the number of characters present in a
string. Its example is given below:
• Syntax: strlen(char array_name);
void main( )
{
char str[ ] = “Priya" ;
int str_length;
str_length= strlen(str) ;
printf ( " length = %d", str_length );
getch();
}
Output: length = 5
• strcpy( ):-
• This function is used to copies the contents of one string into another.
• Syntax :strcpy(newstr , oldstr);
void main( )
{
char oldstr[ ] = "CodeWithC" ;
char newstr[ ] = “program”;
strcpy(newstr , oldstr) ;
puts(newstr);
getch();
}
And here is the output...
CodeWithC
• strcat( ):-
This function is used to concatenate the source string at the end of the target
string. For example, “Hello” and “World” on concatenation would result into a
string “HelloWorld”.
• Syntax :strcat (firstStr,secStr);
void main( )
{
char firstStr[ 50] = "Hello" ;
char secStr[ ] = "World" ;
strcat(firstStr,secStr) ;
printf ( "String = %s", firstStr) ;
puts(firstStr);
getch();
}
Output: String = HelloWorld
HelloWorld
• strcmp( ):-
• This function is used to compares two strings to find out whether they are
same or different.
• The strcmp() will compare two strings character by character until there is
a mismatch or end of one of the strings is reached.
• If both of the strings are identical, strcmp( ) returns a value zero.
• If they are not identical, it will return the numeric difference between the
ASCII values of the first non-matching pairs of characters.
• Syntax :strcmp(string1, string2);
Here is a example of strcmp( ).
#include <stdio.h>
#include <string.h>
void main()
{
char string1[ ] = "Harry" ;
char string2[ ] = "Code" ;
int a;
a= strcmp ( string1, string2 ) ;
printf ("\n%d", a) ;
getch();
}
Output: 1
strrev():-
• This function is used to show the reverse of the string.
• Syntax : strrev(char array_name);
#include<stdio.h>
#include<string.h>
Void main()
{
char str[50] = “BCA";
printf("After reversing string is =%s",strrev(str));
getch();
}
Output: After reversing string is = ACB
MATHEMATICAL FUNCTIONS :
• The math.h header defines various mathematical functions and one
macro. All the functions available in this library take double as an
argument and return double as the result.
• Let us discuss some important functions one by one.
• 1. abs(): The C library function abs() returns the absolute value of int x.
• Syntax : int abs(int x)
// C code to illustrate the use of abs function //
#include <stdio.h>
#include<conio.h>
#include <math.h> Output:
The absolute value of a is 1234
void main () The absolute value of b is 344
{
int a, b;
clrscr();
a = abs(1234);
printf("The absolute value of a = %d\n", a);
b = abs(-344);
printf("The absolute value of b =%d\n", b);
getch();
}
• 2. floor(): The C library function double floor(double x) returns the largest
integer value less than or equal to x.
• Syntax : double floor(double x)
// C code to illustrate the use of floor function //
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main ()
{ Output :
double val1, val2, val3, val4;
Value1 =1.0
clrscr();
Value2 =1.0
val1 = 1.6;
Value3 = -3.0
val2 = 1.2;
Value4 = -3.0
val3 = -2.8;
val4 = -2.3;
printf("Value1 = %.1lf\n", floor(val1));
printf("Value2 = %.1lf\n", floor(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
getch();
}
• [Link] ( ) : Round provides the integer value that is nearest to the float
Syntax : double round(double x)
// C code to illustrate the use of round function //
#include <stdio.h>
#include<conio.h> Output :
Value1 = 4.000000
#include <math.h> Value2 = 3.000000
void main ()
{
double val1, val2;
val1 = 3.8;
val2 = 3.2;
clrscr();
printf (“Value1 = %lf\n", round(val1));
printf (“Value2 = %lf\n", round(val2));
getch();
}
• 4. ceil(): The C library function double ceil (double x) returns the
smallest integer value greater than or equal to x.
• Syntax : double ceil(double x)
// C code to illustrate the use of ceil function //
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main ()
{ Output :
double val1, val2, val3, val4; Value1 =2.0
val1 = 1.6; Value2 =2.0
val2 = 1.2; Value3 =-2.0
val3 = -2.8; Value4 =-2.0
val4 = -2.3;
clrscr();
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
getch();
}
• 5. sqrt(): The C library function double sqrt(double x) returns the square
root of x.
• Syntax : double sqrt(double x)
// C code to illustrate the use of sqrt function//
#include <stdio.h> Output:
Square root of 225.000000 is 15.000000
#include<conio.h> Square root of 300.000000 is 17.320508
#include <math.h>
void main ()
{
clrscr();
printf("Square root of %lf is %lf\n", 225.0, sqrt(225.0) );
printf("Square root of %lf is %lf\n", 300.0, sqrt(300.0) );
getch();
}
• 6. exp(): The C library function double exp(double x) returns the value of e
raised to the xth power.
• Syntax : double exp(double x)
// C code to illustrate the use of exp function //
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main ()
{
double x = 0;
clrscr();
printf("The exponential value of %lf is %lf\n", x, exp(x));
printf("The exponential value of %lf is %lf\n", x+1, exp(x+1));
printf("The exponential value of %lf is %lf\n", x+2, exp(x+2));
Output:
getch(); The exponential value of 0.000000 is 1.000000
} The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056
• 7. log(): The C library function double log(double x) returns the natural
logarithm (base-e logarithm) of x.
• Syntax : double log(double x)
// C code to illustrate the use of log function //
#include <stdio.h>
Output :
#include<conio.h>
Log(2.700000) = 0.993252
#include <math.h>
int main ()
{
double x, ret;
x = 2.7;
/* finding log(2.7) */
ret = log(x);
printf("log(%lf) = %lf", x, ret);
return(0);
}
• 8. double pow(double x, double y): The C library function double
pow(double x, double y) returns x raised to the power of y i.e. xy.
• Syntax : double pow(double x,double y)
// C code to illustrate the use of pow function //
#include <stdio.h> Output :
Value 8.0^3 = 512.000000
#include<conio.h> Value 3.05^1.98 = 9.097324
#include <math.h>
void main ()
{
clrscr();
printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
getch();
}
• 9. double modf(double x, double *integer): The C library function double
modf(double x, double *integer) returns the fraction component (part after
the decimal), and sets integer to the integer component.
• Syntax : double modf(double x,double *integer)
// C code to illustrate the use of modf function //
#include<stdio.h>
Output:
#include<conio.h> Integral part = 8.000000
#include<math.h> Fraction part = 0.123456
void main ()
{
double x, fractpart, intpart;
x = 8.123456;
clrscr();
fractpart = modf(x, &intpart);
printf("Integral part = %lf\n", intpart);
printf("Fraction Part = %lf \n", fractpart);
getch();
}
• 10. double cos(double x) : The C library function double cos(double x)
returns the cosine of a radian angle x.
• Syntax : double cos(double x)
// C code to illustrate the use of cos function //
Output :
The cosine of 60.000000 is 0.500000 degrees
#include <stdio.h>
The cosine of 90.000000 is 0.000000 degrees
#include<conio.h>
#include <math.h>
#define PI 3.14159265
void main ()
{
double x, ret, val;
x = 60.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is %lf degrees\n", x, ret);
x = 90.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is %lf degrees\n", x, ret);
getch();
}
• [Link]() :
• The c library function double sin(double x) returns the sine of a radian angle
x.
• Syntax(): double sin (double x)
// C code to illustrate the use of sin function //
#include <stdio.h>
#include<conio.h>
#include <math.h>
#define PI 3.14159265
void main ()
{
double x, ret, val;
clrscr();
x = 45.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The sine of %lf is %lf degrees\n", x, ret);
getch();
}
• [Link]():
• The tan() function returns tangent of the argument passed.
• Syntax : double tan(double x)
// C code to illustrate the use of tan function //
#include<stdio.h>
#include<conio.h>
#include<math.h> Output :
Void main() tan(2.30) = -1.12
{ tan(-2.30) = -1.12
tan(0.00) = 0.00
double x;
double result;
clrscr();
x=2.3;
result = tan(x);
printf(“tan(%.2lf)=%.2lf\n”,x,result);
x=-2.3;
result = tan(x);
printf(“tan(%.2lf)=%.2lf\n”,x,result);
x=0;
result = tan(x);
printf(“tan(%.2lf)=%.2lf\n”,x,result);
getch();
}
• [Link]():
• trunc( ) function in C truncates the decimal value from floating point value
and returns integer value.
• “math.h” header file supports trunc( ) function in C language. Syntax for
trunc( ) function in C is given below.
• Syntax : double trunc (double a);
float truncf (float a);
long double truncl (long double a);
// C code to illustrate the use of trunc function //
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
clrscr();
printf ("truncated value of 16.99 = %f\n", trunc (16.99) );
printf ("truncated value of 20.1 = %f\n", trunc (20.1) );
getch();
}
Output :
truncated value of 16.99 = 16.000000
truncated value of 20.1 = 20.000000
• [Link]() :
• The C library function double fmod(double x, double y) returns the remainder
of x divided by y.
• Syntax : double fmod(double x, double y)
// C code to illustrate the use of fmod function //
#include <stdio.h>
#include<conio.h> Output :
#include <math.h> Remainder of 8.200000/3 is 2.200000
Remainder of 8.200000/5.700000 is 2.500000
void main ()
{
float a, b;
int c;
clrscr();
a = 8.2;
b = 5.7;
c = 3;
printf("Remainder of %f / %d is %lf\n", a, c, fmod(a, c));
printf("Remainder of %f / %f is %lf\n", a, b, fmod(a, b));
getch();
}