Data Types
Part of the Fundamentals section of Coddy's C journey — lesson 5 of 63.
In C, every piece of data in your program has a specific type. Data types define what kind of data a variable can hold and how much memory it needs. Let's look at the basic data types in C:
Create an integer variable:
int age;This declares a variable named age that can hold whole numbers.
age = 25;This assigns the value 25 to age. But you can also declare and initialize in one step:
int score = 100;Here, int score = 100; declares the variable and sets its value at the same time.
Here are the primary data types in C:
int number = 42; // Integer (whole number)
float price = 10.5f; // Floating-point (decimal)
double pi = 3.14159; // Double precision floating-point
char grade = 'A'; // Single characterint stores whole numbers. float stores decimal numbers — the f suffix (e.g. 10.5f) tells C to treat the value as a float rather than a double. double stores decimal numbers with greater precision than float. char stores a single character enclosed in single quotes.
Challenge
EasyYou are given a program, store in the variable named price the value of 120.
Cheat sheet
In C, every variable must have a specific data type that defines what kind of data it can hold:
Basic Data Types:
int number = 42; // Integer (whole number)
float price = 10.5f; // Floating-point (decimal)
double pi = 3.14159; // Double precision floating-point
char grade = 'A'; // Single characterVariable Declaration and Assignment:
int age; // Declare variable
age = 25; // Assign value
int score = 100; // Declare and initialize in one stepTry it yourself
#include <stdio.h>
int main() {
// Change the question mark to the correct number
int price = ?;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge