Getting Started
Variables, Data Types & Constants
Store values in variables, choose the right data type, print them with the right format specifier, and lock values with constants.
A variable is a named box in memory that holds a value. Every variable in C has a type, which decides how many bytes the box uses and what kind of value it can hold.
#include <stdio.h>
int main(void) {
int age = 19; // whole number
float cgpa = 8.75f; // decimal number
char grade = 'A'; // single character
printf("Age: %d\n", age);
printf("CGPA: %.2f\n", cgpa);
printf("Grade: %c\n", grade);
age = age + 1; // variables can change
printf("Next year: %d\n", age);
return 0;
}Age: 19 CGPA: 8.75 Grade: A Next year: 20
Declaring and naming variables
int count; // declaration (value is garbage until you assign one)
count = 10; // assignment
int total = 0; // declaration + initialisation
int a = 1, b = 2; // several at onceNaming rules (these are called identifiers):
- Use letters, digits and underscores only:
total_marks,num1. - Must not start with a digit:
1numis invalid. - Cannot be a keyword such as
int,return,while. - Case matters:
Sumandsumare different variables.
Data types
| Type | Typical size | Holds | Example |
|---|---|---|---|
char |
1 byte | one character or small integer | 'A' |
short |
2 bytes | small integers | 300 |
int |
4 bytes | integers, about ±2.1 billion | -42 |
long long |
8 bytes | very large integers | 9000000000LL |
float |
4 bytes | decimals, ~7 digits of precision | 3.14f |
double |
8 bytes | decimals, ~15 digits of precision | 3.14159265 |
Add unsigned to an integer type to store only non-negative values with double the positive range. Sizes can vary between machines, so C gives you the sizeof operator to check:
#include <stdio.h>
int main(void) {
printf("char : %zu byte\n", sizeof(char));
printf("short : %zu bytes\n", sizeof(short));
printf("int : %zu bytes\n", sizeof(int));
printf("long long : %zu bytes\n", sizeof(long long));
printf("float : %zu bytes\n", sizeof(float));
printf("double : %zu bytes\n", sizeof(double));
return 0;
}char : 1 byte short : 2 bytes int : 4 bytes long long : 8 bytes float : 4 bytes double : 8 bytes
Format specifiers
The specifier must match the variable's type, or printf prints nonsense.
| Specifier | Type | Specifier | Type |
|---|---|---|---|
%d or %i |
int |
%c |
char |
%u |
unsigned int |
%s |
string |
%ld |
long |
%x / %o |
hex / octal |
%lld |
long long |
%e |
scientific notation |
%f |
float / double |
%p |
pointer address |
%.2f |
2 decimal places | %zu |
sizeof result |
#include <stdio.h>
int main(void) {
int n = 255;
double pi = 3.14159265;
printf("%d in hex is %x and in octal is %o\n", n, n, n);
printf("pi = %f\n", pi);
printf("pi = %.3f\n", pi);
printf("pi = %e\n", pi);
printf("[%5d] [%-5d]\n", 42, 42); // width 5, right and left aligned
return 0;
}255 in hex is ff and in octal is 377 pi = 3.141593 pi = 3.142 pi = 3.141593e+00 [ 42] [42 ]
Characters are numbers
A char stores a small integer — its ASCII code. 'A' is 65, 'a' is 97 and '0' is 48. That is why you can do arithmetic on characters:
#include <stdio.h>
int main(void) {
char c = 'A';
printf("%c has ASCII code %d\n", c, c);
printf("Next letter: %c\n", c + 1);
printf("Lowercase: %c\n", c + 32);
printf("Digit '7' as a number: %d\n", '7' - '0');
return 0;
}A has ASCII code 65 Next letter: B Lowercase: a Digit '7' as a number: 7
Constants
Some values should never change. C gives you two ways to make them:
#include <stdio.h>
#define GRAVITY 9.8 // preprocessor constant: text replaced before compiling
int main(void) {
const int DAYS_IN_WEEK = 7; // typed constant: the compiler refuses changes
printf("Weeks in 28 days: %d\n", 28 / DAYS_IN_WEEK);
printf("Weight of 50 kg: %.1f N\n", 50 * GRAVITY);
return 0;
}Weeks in 28 days: 4 Weight of 50 kg: 490.0 N
Trying DAYS_IN_WEEK = 8; would be a compile error: assignment of read-only variable.
Test yourself
4 questions · pick an answer to check it instantly
- 1
Which is a valid variable name in C?
Names may contain letters, digits and underscores, cannot start with a digit, and cannot be keywords.my-scorecontains a minus sign. - 2
Which format specifier prints a
doublewith exactly two decimal places?%.2frounds to two digits after the decimal point. - 3
What does this print?
Cchar c = 'B'; printf("%d", c);%dprints the character's ASCII code, and'B'is 66. - 4
What is the main difference between
const int X = 5;and#define X 5?The preprocessor swapsXfor5before compilation.constis a real variable the compiler type-checks.