Introduction & Syntax
Declarations and Initializations · Study material
A declaration in C introduces a name and tells the compiler its type; an initialization gives an object its first value as it is created. int marks = 85; does both. Getting declarations right decides what a name means, where it can be used, which file owns it and what value it starts with – and interview questions on extern, prototypes, linkage, const/volatile and initializer rules come straight from here. This page covers all of it, including C23's new forms, and gives you 40 practice questions checked on a real compiler.
On this page
- Declaring variables: syntax, names and several declarators
- Initialising scalars, arrays, strings and structures (designated initializers)
- Declaration vs definition,
extern, prototypes and linkage - Literal types,
constandvolatile, and C23 additions
Declaring a variable
A declaration lists optional storage class and qualifiers, a type, and one or more declarators: static const unsigned long count;. Names may contain letters, digits and the underscore – the only special symbol allowed – and must not start with a digit or be a keyword. Each declarator in a list stands on its own: in int *p, q; only p is a pointer.
Initialising variables
An initializer follows = in the declaration. Arrays and structures take brace lists; members you do not mention become zero. Designated initializers (C99) name the element or member: [3] = 7, .x = 1.
#include <stdio.h>
struct point { int x, y; };
int main(void) {
int a = 5, b, c = a * 2; /* several declarators; b has no initial value yet */
b = 1;
double rate = 7.5;
char grade = 'A';
int marks[5] = {90, 85}; /* the remaining elements become 0 */
char name[] = "Asha"; /* size 5, taken from the string */
struct point p = {.y = 3, .x = 1}; /* designated initializers, any order */
printf("%d %d %d %.1f %c %d %zu %d,%d\n", a, b, c, rate, grade, marks[4], sizeof name, p.x, p.y);
return 0;
}
5 1 10 7.5 A 0 5 1,3
| Object | Without an initializer |
|---|---|
| Local (automatic) variable | indeterminate – garbage |
Global or static variable | zero (pointers NULL, doubles 0.0) |
| Array or struct with a partial initializer | the rest is zero |
Heap memory from malloc() | indeterminate (calloc() zeroes it) |
Declaration vs definition
A declaration says a name exists; a definition creates it (storage for a variable, the body of a function). extern int total; and a function prototype are declarations only. Functions have external linkage by default, so extern int fun(); and int fun(); mean the same. A name may be declared many times but defined once.
#include <stdio.h>
extern int total; /* declaration: total is defined later (or in another file) */
int square(int x); /* prototype: a declaration of the function */
int main(void) {
total = square(7);
printf("%d\n", total);
return 0;
}
int total; /* definition – zero-initialised, static storage */
int square(int x) { /* definition – the function body */
return x * x;
}
49
Linkage decides whether the same name in different files refers to the same thing: external (globals and functions – shared), internal (static at file scope – one file) and none (locals). A file-scope int x; without an initializer is a tentative definition: repeating it is allowed, and it becomes a zero-initialised definition if no other definition appears.
The type of a literal
Every constant has a type. A real number such as 3.14 is a double by default; suffixes change it (3.14f float, 10L long, 10u unsigned). A character constant like 'A' is an int in C, and a string literal is a char array including its terminator.
#include <stdio.h>
int main(void) {
/* 3.14 is a double, 3.14f a float, 10L a long, 'A' an int, "hi" a char[3] */
printf("%zu %zu %zu %zu %zu\n", sizeof 3.14, sizeof 3.14f, sizeof 10L, sizeof 'A', sizeof "hi");
return 0;
}
8 4 8 4 3
Constant and volatile variables
const makes an object read-only after initialisation, so it must be initialised in its declaration. volatile tells the compiler the value may change outside the program's control – a hardware register, a variable changed by a signal handler – so every read and write must really happen and cannot be cached or optimised away.
#include <stdio.h>
int main(void) {
const int max_users = 100; /* read-only once initialised */
volatile int sensor = 0; /* may change outside the program: every access really touches memory */
for (int i = 0; i < 3; i++)
sensor += i;
printf("%d %d\n", max_users, sensor);
return 0;
}
100 3
Static storage and compound literals
Objects with static storage are initialised before main() runs, so their initializers must be constant expressions. A compound literal (type){…} creates an unnamed object on the spot.
#include <stdio.h>
struct point { int x, y; };
static int counter; /* static storage: starts at 0 */
int main(void) {
int *p = (int[]){10, 20, 30}; /* compound literal: an unnamed array */
struct point origin = {0}; /* {0} zeroes every member */
printf("%d %d %d\n", counter, p[2], origin.x + origin.y);
return 0;
}
0 30 0
Declaring user-defined types
struct, union and enum declarations create new types; typedef gives an existing type another name. Built-in types such as int or long double are not user-defined.
struct student { char name[20]; int roll; }; /* a new structure type */
union value { int i; double d; }; /* overlapping members */
enum level { LOW, MEDIUM, HIGH }; /* named constants */
typedef unsigned long long u64; /* an alias, not a new type */
What C23 adds
C23 brings the empty initializer = {}, constexpr objects, type inference with auto, typeof, and nullptr – a null pointer constant with its own type, nullptr_t. It also makes int f() mean "no parameters".
#include <stdio.h>
int main(void) {
constexpr int N = 3; /* C23: compile-time constant */
int a[N] = {}; /* C23: empty initializer, all zero */
auto x = 2.5f; /* C23: type deduced – float */
typeof(x) y = x * 2; /* C23: same type as x */
int *p = nullptr; /* C23: typed null pointer constant */
printf("%d %zu %.1f %d\n", a[0], sizeof y, y, p == nullptr);
return 0;
}
0 4 5.0 1
Key points to remember
- Declare names before use; define each variable and function exactly once.
- Initialise locals; globals and statics start at zero.
- Partial brace initializers zero everything not listed.
- 3.14 is a double, 'A' is an int, "hi" is char[3].
constmust be initialised;volatileforbids caching of the value.
Frequently asked questions
What is the difference between declaration and initialization?
A declaration introduces the name and type; initialization gives the first value as part of that declaration. int x; declares, int x = 5; declares and initialises, and x = 5; later is an assignment.
Is extern needed for function declarations?
No. Functions have external linkage by default, so extern on a function declaration changes nothing. It matters for variables, where it turns a definition into a declaration.
Why is my variable's value random?
It is a local variable that was never initialised. Give it a value in its declaration.
Where can I learn more?
Read Data Types in C, Constants in C and Scope and Storage Classes, then practise with the exercises below.