Variables
Anonymous contributor
Anonymous contributor3071 total contributions
Anonymous contributor
Published Aug 24, 2021Updated Nov 19, 2022
Contribute to Docs
A variable is a location in computer memory used to store data, usually for use in a program.
Syntax
type name = value;
When declaring a variable in C, the type
of variable goes first, followed by the name, and then the value.
But if a variable does not yet need a value assigned to it, this can be omitted and assigned later.
type name;
name = value;
Variables in C are static and can only have one type (e.g., an int
variable can only hold an integer value).
Example
To display a variable in C, the printf()
function from the stdio.h header file can be used along with the %
character to format values, followed by the variable name:
#include <stdio.h>int main(void) {int number = 5;char letter = 'a';printf("%d\n", number);printf("%c\n", letter);return 0;}
The output would be:
5a
All contributors
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributorAnonymous contributor4 total contributions
- garanews222 total contributions
- Anonymous contributorAnonymous contributor24 total contributions
- christian.dinh2476 total contributions
- Anonymous contributor
- Anonymous contributor
- garanews
- Anonymous contributor
- christian.dinh
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.