Basic data types help us tell the computer what kind of information we want to store, like numbers or letters.
Basic data types
int myNumber; float myDecimal; char myLetter; bool myFlag; // requires #include <stdbool.h>
int stores whole numbers.
float stores decimal numbers.
char stores a single character.
bool stores true or false values (needs #include <stdbool.h>).
age.int age = 25;
price.float price = 19.99;
grade. Note the single quotes for characters.char grade = 'A';
isOpen. You must include stdbool.h to use bool.#include <stdbool.h>
bool isOpen = true;This program shows how to use basic data types in C. It prints each value with a message.
#include <stdio.h> #include <stdbool.h> int main() { int age = 30; float temperature = 36.6f; char initial = 'J'; bool isSunny = true; printf("Age: %d\n", age); printf("Temperature: %.1f\n", temperature); printf("Initial: %c\n", initial); printf("Is it sunny? %s\n", isSunny ? "Yes" : "No"); return 0; }
Remember to use single quotes for char values, like 'A'.
Use %d in printf for int, %f for float, and %c for char.
Boolean values print as 1 (true) or 0 (false) by default, so use a conditional to print words.
Basic data types store different kinds of simple information.
int for whole numbers, float for decimals, char for single characters, and bool for true/false.
Use the right type to help the computer understand your data.
Practice
Solution
Step 1: Understand data types for numbers
Whole numbers without decimals are stored using theinttype in C.Step 2: Identify correct type for whole numbers
floatstores decimals,charstores characters, andboolstores true/false values.Final Answer:
int -> Option AQuick Check:
Whole numbers = int [OK]
- Confusing float with int for whole numbers
- Using char for numbers
- Thinking bool stores numbers
Solution
Step 1: Check variable declaration syntax
In C, to declare a float variable, use the keywordfloatfollowed by the variable name and value.Step 2: Match type with value
Assigning 3.14 (a decimal) to a float variable is correct. Other types like int, char, or bool cannot hold decimal values properly.Final Answer:
float number = 3.14; -> Option CQuick Check:
Decimal numbers use float type [OK]
- Using int for decimal values
- Assigning decimal to char
- Using bool for numbers
char letter = 'A';
int number = 5;
float decimal = 3.5;
printf("%c %d %.1f", letter, number, decimal);Solution
Step 1: Understand format specifiers
%cprints a character,%dprints an integer, and%.1fprints a float with one decimal place.Step 2: Match variables to specifiers
letter = 'A'prints 'A',number = 5prints 5, anddecimal = 3.5prints 3.5 with one decimal.Final Answer:
A 5 3.5 -> Option DQuick Check:
Format specifiers match output [OK]
- Printing char as int (shows ASCII code)
- Ignoring decimal precision
- Confusing format specifiers
bool flag = 1; char letter = "B"; int count = 10;
Solution
Step 1: Check bool assignment
Assigning 1 to bool is allowed since 1 means true in C.Step 2: Check char assignment
Characters must be in single quotes ('B'), not double quotes ("B") which are for strings.Final Answer:
char should use single quotes for characters -> Option AQuick Check:
char uses single quotes [OK]
- Using double quotes for char
- Thinking bool can't be 1 or 0
- Confusing int assignments
Solution
Step 1: Choose type for grade letter
Grade letters like 'A' are single characters, socharis the right type.Step 2: Choose type for pass status
Pass/fail is a true/false value, soboolfits perfectly.Final Answer:
char for grade, bool for pass status -> Option BQuick Check:
Letter = char, true/false = bool [OK]
- Using int or float for letter grades
- Using char for true/false values
- Mixing data types incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
