Double and Char - C Programs
Double in C
Example:
#include <stdio.h>
int main()
{
double num = 10.5;
printf("The value of num is : %lf \n", num);
return 0;
}
Output:
The value of num is : 10.500000
Explanation:
In C programming, a double
variable is used to represent floating-point numbers, which can have decimal points. double
is a data type that is used to represent double-precision floating-point numbers. To declare a variable as double, we write double
before the variable name.
In the example above, we declared a double
variable num
with the value of 10.5
. We used the format specifier %lf
to print the value of the num
variable.
Char in C
Example:
#include<stdio.h>
int main()
{
char c = 'A';
printf("The character is %c", c);
return 0;
}
Output:
The character is A
Explanation:
In C programming, the char
data type is used to represent a single character. It is always enclosed in single quotes. To declare a variable as char
, we write char
before the variable name.
In the example above, we declared a char
variable c
with the value 'A'
. We used the format specifier %c
to print the value of the c
variable.
Use
Double and Char data types are used extensively in C programming. Double is used in scientific calculations, financial calculations, and graphics. Char is used to store ASCII values of characters, as well as in decision-making statements.
Summary
- Double is used to represent floating-point numbers.
- Char is used to represent a single character.
- Double is declared as
double
before the variable name. - Char is declared as
char
before the variable name. - Double and Char are extensively used in C programming for various operations.