Syntax
type identifier1, identifier2, ..., identifiern;
char myFirstLetter;
int a, b, c;
float currentSpeed;
double sensorValue;
There are multiple ways to declare a variable:
Declaration on a single line
type name;
Declaration on a single line with an initial value
type name = initialValue;
Multiple declarations of the same type on a single line
type name1, name2, name3;
Multiple declarations of the same type on a single line with initial values
type name1 = value1, name2 = value2, name3;
Code example
unsigned int myVar;
signed int answer = 42;
char myFirstLetter = 'a', mySecondLetter, myLastLetter = 'z';
float hugeNumber = 5.12e+21
Syntax
typedef type typeName;
Code example
typedef unsigned int uint32_t;
uint32_t a, b; // alias for unsigned int a, b;
Header files are used to declare variables and other program elements in a separate file. Usually, header files have the filename extension .h. To use a header file in your C code, associate it with the #include directive.
The #include directive can be used in three different ways. You can:
#include <stdio.h>
#include "myHeader.h"
#include "..\..\myHeader2.h"
#include "C:\projects\myProject\inc\myHeader3.h"
Code example
main.h header file:
int n, m, p;
float myFloat;
main.c source file:
#include "main.h"
int main (void) {
n = 3;
m = 1;
p = n - m;
myFLoat = 1 / p;
}
Using the #include directive, the contents of the main.h header file are pasted into the main.c source file, starting at the #include directive’s line.
After the preprocessor runs, the compiler sees the main.c source file as follows:
int n, m, p;
float myFloat;
int main (void) {
n = 3;
m = 1;
p = n - m;
myFloat = 1 / p;
}