float is short hand for floating point value, allows us to store real numbers. That is numbers with decimal places. 0 is also a float.
float x = 5.2;double is very similar to a float.
double y = -9.8;double can store numbers with a higher level of precision, i.e more accuracy. double has a greater range, more decimal places. Better for high accuracy calculations, but uses more computer memory.
We can assign modifiers to char, int, float and double types. These modifiers are signed, unsigned, short and long.
- unsigned, makes the numbers only positive.
unsigned intcontains at least the [0, 65,535] range. 4 Bytes.
unsigned int;-
signed, makes the number both negative and positive. Same effect as using just int with no modifiers.
-
short, reduces the maximum number that can be stored, to save bits. Capable of containing at least the [−32,767, +32,767] range. 2 bytes.
short int x;
short x; // same as above- long, capable of containing at least the [−2,147,483,647, +2,147,483,647] range. Use 32 bits to store long int.
long int;Using the scanf function with floats. Placeholder for float is %f.
printf("x: ");
scanf("%f, &x); // store float in variable x memory location.Using the printf function with floats. Placeholder for float is %f.
printf("x: %f\n", x);#include <stdio.h>
int main(void)
{
float x = 5.2;
double y = -9.8;
printf("Enter value for x: ");
scanf("%f", &x); // Get float from user assign to x memory location
printf("x: %f\n", x); // print x
}Compile file d.c to create new file d
Open terminal and type:
$ gcc -o d d.cExecute file d.
$ ./dExpected output
$ ./d
Enter value for x: 5.2
x: 5.200000Notice the output 5.200000 we can use modifiers to change the appearance.
float does not store the exact number given. See below:
$ ./d
Enter value for x: -52.245
x: -52.244999This is due to the way the computer stores the number in memory. It cannot store exactly the 52.245 but it is very very close.
To use double with scanf we use the placeholder %lf large float.
To use double with printf we can also user %lf but %f will also work.
printf("Enter value for y: ");
scanf("%lf", &y); // Get double from user, assign to y memory location
printf("y: %lf\n", y); // print y
}Recompile and run program.
Expected output
$ ./d
Enter value for x: 20.2
x: 20.200001
Enter value for y: -10.88
y: -10.880000char variable. For storing characters, must be assigned in single quotes. char are fundamentally integer values. To guarantee char is treated as signed data it is best practice to use the signed keyword.
Using the ASCII code which is a integer. For example the ASCII code for d is 100.
signed char c = 'd'; // ASCII 100The ASCII code can also be used.
signed char c = 100; // 'd'%c is the placeholder for using char variables.
printf("c: ");
scanf("%c", &c);
printf("c: %c\n", c);
}Can also use %d which in normally for integers. To print the ASCII integer value of the character d i.e 100.
printf("c: %d\n", c);Compile and run program.
Expected output.
$ ./d
c: d
c: d
c: 100