This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
tutorial variables
Tianshu Huang edited this page Oct 7, 2018
·
7 revisions
In C, data is stored as variables.
Each variable is associated with a data type.
By default, the following types are available: (this list is not complete)
Name | Size (bytes) | Format Specifier | Description |
---|---|---|---|
char | 1 | %c | 8-bit number or single ASCII character |
int | 2 | %d or %i | 2-byte signed integer |
unsigned int | 2 | %u | 2-byte unsigned integer |
long | 4 | %d or %i | Larger signed integer |
unsigned long | 4 | %u | Larger unsigned integer |
float | 4 | %f or %F | 4-byte floating point number (decimal number) |
double | 8 | %e, %E, %g, or %G | Larger floating point number |
NOTE: long long
(64-bit integer) isn't fully supported by our compiler. Using a long long
is just asking for trouble, especially if you attempt to divide it.
More explicit types can be accessed by adding #include <stdint.h>
to the top of the program. This gives you access to the following types:
Name Format | Size | Format Specifier | Example | Description |
---|---|---|---|---|
uintX_t | X | %d or %i | uint16_t | X-bit unsigned integer |
intX_t | X | %u | int32_t | X-bit signed integer |
Declare a variable by putting the desired data type in front of the desired name:
#include <stdint.h>
int main() {
uint32_t x = 100000
x = x + 1
printf("%d\n", x); // Prints x; shows 100001
}
In C, assignment is denoted with =
, with the value to be assigned on the left.
x = 5; // 5 -> x
x = x + 1; // x + 1 -> x
WARNING: assignment is always =
. Comparison is done with ==
. For example,
if(x = 5) {
printf("%d\n", x);
}
will always print 5
, since x = 5
assigns x as 5.