0C Data Types_112316
0C Data Types_112316
Data Types
As explained in the Variables chapter, a variable in C must be a
specified data type, and you must use a format specifier inside
the printf() function to display it:
Example
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
#include <stdio.h>
int main() {
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
Example 1 :
#include <stdio.h>
int main() {
int myNum = 5; // integer
printf("%d\n", myNum);
printf("%i\n", myNum);
return 0;
}
Example 2 :
#include <stdio.h>
int main() {
float myFloatNum = 5.99; // Floating point number
printf("%f", myFloatNum);
return 0;
}
Example 3 :
#include <stdio.h>
int main() {
double myDoubleNum = 19.99; // Double (floating point number)
printf("%lf", myDoubleNum);
return 0;
}
Example 4 :
#include <stdio.h>
int main() {
char myLetter = 'D'; // Character
printf("%c", myLetter);
return 0;
}
Example 5 :
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
Exercise:
Add the correct data type for the following variables:
myNum = 5;
myFloatNum = 5.99;
myLetter = 'D';
Exercise:
Add the correct format specifier to print the value of the following variable: