Getting Started
Variables and Types
Explore C data types, variable declarations, constants, and type sizes.
C Data Types
C has a small set of built-in types:
| Type | Size | Range |
|---|---|---|
char | 1 byte | -128 to 127 |
int | 4 bytes | ~-2 billion to 2 billion |
float | 4 bytes | ~7 decimal digits |
double | 8 bytes | ~15 decimal digits |
long | 8 bytes | larger int |
short | 2 bytes | smaller int |
Type Modifiers
unsigned— only positive values (doubles the max)signed— positive and negative (default)long— larger sizeshort— smaller size
Constants
Use const keyword or #define preprocessor macro.
Scope
Variables declared inside {} have local scope. Variables outside functions are global.
Example
c
#include <stdio.h>
#include <limits.h> /* INT_MAX, INT_MIN, etc. */
#include <float.h> /* FLT_MAX, DBL_MAX, etc. */
/* Constant with #define (preprocessor) */
#define PI 3.14159265
#define MAX_SIZE 100
/* Constant with const keyword */
const int DAYS_IN_WEEK = 7;
int globalVar = 10; /* Global variable */
int main() {
/* Signed integers */
int a = 42;
short s = 32767;
long l = 1234567890L;
long long ll = 9876543210LL;
/* Unsigned integers */
unsigned int u = 4294967295U;
unsigned char byte = 255;
/* Floating point */
float f = 3.14f;
double d = 3.14159265358979;
/* Character */
char c = 'X';
char str[] = "Hello"; /* char array = string */
/* sizeof operator */
printf("int size: %zu bytes\n", sizeof(int));
printf("double size: %zu bytes\n", sizeof(double));
/* Type limits */
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
/* Implicit type conversion */
int x = 10, y = 3;
double result = (double)x / y; /* explicit cast to get 3.333 */
printf("%.3f\n", result);
return 0;
}Try it yourself — C