Unit 3
Unit 3
There is also a list of math functions available, that allows you to perform
mathematical tasks on numbers. 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.
To use them, you must include the math.h header file in your program:
#include <math.h>
Square Root
To find the square root of a number, use the sqrt() function:
Example
printf("%f", sqrt(16));
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and
the floor() method rounds a number downwards to its nearest integer, and
returns the result:
Example
printf("%f", ceil(1.4));
printf("%f", floor(1.4));
Power
The pow() function returns the value of x to the power of y (xy):
Example
printf("%f", pow(4, 3));
There are various methods in math.h header file. The commonly used functions of math.h
header file are given below.
No. Function Description
1) ceil(number) rounds up the given number. It returns the integer value which is greater than
or equal to given number.
2) floor(number) rounds down the given number. It returns the integer value which is less than
or equal to given number.
C Math Example
Let's see a simple example of math functions found in math.h header file.
1. #include<stdio.h>
2. #include <math.h>
3. int main(){
4. printf("\n%f",ceil(3.6));
5. printf("\n%f",ceil(3.3));
6. printf("\n%f",floor(3.6));
7. printf("\n%f",floor(3.2));
8. printf("\n%f",sqrt(16));
9. printf("\n%f",sqrt(7));
10. printf("\n%f",pow(2,4));
11. printf("\n%f",pow(3,3));
12. printf("\n%d",abs(-12));
13. return 0;
14. }
Output:
Play Video
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
int/signed
1 %d int used for I/O signed integer value
5 %ld long int Used for I/O long signed integer value
6 %u unsigned int Used for I/O unsigned integer value
#include <stdio.h>
int main()
int a;
a = 20;
return 0;
Output
20
Syntax 2:
To display any string or a message
printf(“Enter the text which you want to display”);
#include <stdio.h>
int main()
printf("This is a string");
return 0;
Output
This is a string
scanf():
scanf() function is used in the C program for reading or taking any value from
the keyboard by the user, these values can be of any data type like integer,
float, character, string, and many more. This function is declared in
stdio.h(header file), that’s why it is also a pre-defined function. In scanf()
function we use &(address-of operator) which is used to store the variable value
on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
#include <stdio.h>
int main()
{ int num1;
scanf("%d", &num1);
return 0;
Output
Enter a integer number: You have entered 0
Output:
Enter a integer number: 56
You have entered 56
sprintf():
sprintf stands for “string print”. This function is similar to printf() function but
this function prints the string into a character array instead of printing it on the
console screen.
Syntax:
sprintf(array_name, “format specifier”, variable_name);
#include <stdio.h>
int main()
{ char str[50];
int a = 2, b = 8;
printf("%s", str);
return 0;
Output
2 and 8 are even number
sscanf():
sscanf stands for “string scanf”. This function is similar to scanf() function but
this function reads data from the string or character array instead of the console
screen.
Syntax:
sscanf(array_name, “format specifier”, &variable_name);
#include <stdio.h>
int main()
{ char str[50];
int a = 2, b = 8, c, d;
return 0;
Output
c = 2 and d = 8
Unformatted Input/Output functions
Unformatted I/O functions are used only for character data type or character
array/string and cannot be used for any other datatype. These functions are
used to read single input from the user at the console and it allows to display
the value at the console.
Why they are called unformatted I/O?
These functions are called unformatted I/O functions because we cannot use
format specifiers in these functions and hence, cannot format these functions
according to our needs.
The following unformatted I/O functions will be discussed in this section-
1. getch()
2. getche()
3. getchar()
4. putchar()
5. gets()
6. puts()
7. putch()
getch():
getch() function reads a single character from the keyboard by the user but
doesn’t display that character on the console screen and immediately returned
without pressing enter key. This function is declared in conio.h(header file).
getch() is also used for hold the screen.
Syntax:
getch();
or
variable-name = getch();
#include <conio.h>
#include <stdio.h>
int main()
getch();
return 0;
}
Output:
Enter any character:
getche():
getche() function reads a single character from the keyboard by the user and
displays it on the console screen and immediately returns without pressing the
enter key. This function is declared in conio.h(header file).
Syntax:
getche();
or
variable_name = getche();
#include <conio.h>
#include <stdio.h>
int main()
getche();
return 0;
Output:
Enter any character: g
getchar():
The getchar() function is used to read only a first single character from the
keyboard whether multiple characters is typed by the user and this function
reads one character at one time until and unless the enter key is pressed. This
function is declared in stdio.h(header file)
Syntax:
Variable-name = getchar();
Example: // C program to implement the getchar() function
#include <conio.h>
#include <stdio.h>
int main()
char ch;
ch = getchar();
printf("%c", ch);
return 0;
Output:
Enter the character: a
a
putchar():
The putchar() function is used to display a single character at a time by passing
that character directly to it or by passing a variable that has already stored a
character. This function is declared in stdio.h(header file)
Syntax:
putchar(variable_name);
Example: // C program to implement the putchar() function
#include <conio.h>
#include <stdio.h>
int main()
{ char ch;
// Reads a character
ch = getchar();
putchar(ch);
return 0;
Output:
Enter any character: Z
Z
gets():
gets() function reads a group of characters or strings from the keyboard by the
user and these characters get stored in a character array. This function allows
us to write space-separated texts or strings. This function is declared in
stdio.h(header file).
Syntax:
char str[length of string in number]; //Declare a char type variable of any
length
gets(str);
Example: // C program to implement the gets() function
#include <conio.h>
#include <stdio.h>
int main()
// of length 50 characters
char name[50];
gets(name);
return 0;
Output:
Please enter some texts: geeks for geeks
You have entered: geeks for geeks
puts():
In C programming puts() function is used to display a group of characters or
strings which is already stored in a character array. This function is declared in
stdio.h(header file).
Syntax:
puts(identifier_name );
#include <stdio.h>
int main()
{ char name[50];
gets(name);
// Displays string
puts(name);
return 0;
Output:
Enter your text: GeeksforGeeks
Your text is: GeeksforGeeks
putch():
putch() function is used to display a single character which is given by the user
and that character prints at the current cursor location. This function is declared
in conio.h(header file)
Syntax:
putch(variable_name);
Example: // C program to implement the putch() functions
#include <conio.h>
#include <stdio.h>
int main()
{ char ch;
ch = getch();
putch(ch);
return 0;
Output:
Enter any character:
Entered character is: d
Formatted I/O vs Unformatted I/O
S
No. Formatted I/O functions Unformatted I/O functions
These functions allow us to take input or These functions do not allow to take
display output in the user’s desired input or display output in user desired
1 format. format.
These functions support format These functions do not support format
2 specifiers. specifiers.
These are used for storing data more These functions are not more user-
3 user friendly friendly.
printf(), scanf, sprintf() and sscanf() are getch(), getche(), gets() and puts(), are
5 examples of these functions. some examples of these functions.