C Reference - Data Types Keywords
Basic Types
* machine dependent
Derived Types
| Name |
Declaration Syntax |
Description |
| Pointer |
type *name; |
Contains address of another variable |
| Array |
type name[size]; |
Set of variables of the same type |
| Structure |
struct tag {...} name; |
Set of variables of different types |
| Union |
union tag {...} name; |
Set of variables of different types that are allocated
on the same memory |
Type Specifiers and Qualifiers
| Name |
Syntax |
Description |
| typedef |
typedef old_type
new_type; |
Type definition |
| extern |
extern type name; |
External variable declaration |
| static |
static type name; |
Static variable has a lifetime until the end
of program and a local scope |
| const |
const type name=value; |
Constant variable - can't be changed |
| auto |
auto type name; |
Declare local variable inside function.
Can be omitted from the code. |
| register |
register type name; |
Declare local variable inside function. Suggests compiler to keep variable in CPU
register. Used for optimization. |
| volatile |
volatile type name; |
Avoid compiler optimization, used when can
be changed externally.
i.e: I/O
addressed variable. |
void |
Syntax:
|
|
void
|
Size:
|
|
|
Description:
|
| Void type |
Example1:
|
|
void *p;
p = (void *)(&x); |
Example2:
|
|
void my_func(int x)
{
:
/* no return value */
} |
char |
Syntax:
|
|
char variable name;
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Signed character |
Examples:
|
|
char c;
char c=32;
char c='a';
char *msg="Hello there";
char msg[]="Hello there";
|
signed char |
Syntax:
|
|
signed char variable name;
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Signed character. Same as char. |
Examples:
|
|
signed char c;
signed char c=-100;
signed char c='a';
signed char *msg="Hello there";
signed char msg[]="Hello there";
|
unsigned char |
Syntax:
|
|
unsigned char variable name;
|
Size:
|
|
|
Range:
|
-
Dec: 0..255
-
Hex: 0..0xFF
-
Const: 0..UCHAR_MAX
|
Description:
|
| Unsigned character. |
Examples:
|
|
unsigned char c;
unsigned char c=200;
unsigned char c='a'; |
short |
Syntax:
|
|
short variable name;
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Signed short integer |
Examples:
|
|
short c;
short c=-20000;
short c=20000;
|
unsigned short |
Syntax:
|
|
unsigned short variable name;
|
Size:
|
|
|
Range:
|
-
Dec: 0..65535
-
Hex: 0..0xFFFF
-
Const: 0..USHRT_MAX
|
Description:
|
| Unsigned short integer. |
Examples:
|
|
unsigned short c;
unsigned short c=40000;
|
int |
Syntax:
|
|
int variable name;
|
Size:
|
- 32 bits (machine dependent)
- 4 bytes
|
Range:
|
|
|
Description:
|
| Signed integer. |
Examples:
|
|
int c;
int c=-200000;
int c=200000;
|
enum |
Syntax:
|
|
enum name {
name1 [= number1], name2 [=
number2], name3 [= number3],
: };
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Enumeration constant list of signed integers. |
Examples:
|
|
enum subject {
MATH
= 1,
PHYSICS
= 2,
CHEMISTRY = 3,
PROGRAMMING = 4,
ELECTRONICS = 9
};
|
unsigned int |
Syntax:
|
|
unsigned int variable name;
|
Size:
|
- 32 bits (machine dependent)
- 4 bytes
|
Range:
|
-
Dec: 0..4294967295
-
Hex: 0..0xFFFFFFFF
-
Const: 0..UINT_MAX
|
Description:
|
| Unsigned integer. |
Examples:
|
|
unsigned int c;
unsigned int c=200000U;
|
long |
Syntax:
|
|
long variable name;
|
Size:
|
|
|
Range:
|
-
Dec:
-2147483648..2147483647
-
Hex: 80000000..0x7FFFFFFF
-
Const: LONG_MIN..LONG_MAX
|
Description:
|
| Signed long integer. |
Examples:
|
|
long c;
long c=-200000L;
long c=200000L;
|
unsigned long |
Syntax:
|
|
unsigned long variable name;
|
Size:
|
|
|
Range:
|
-
Dec: 0..4294967295
-
Hex: 0..0xFFFFFFFF
-
Const: 0..ULONG_MAX
|
Description:
|
| Unsigned long integer. |
Examples:
|
|
unsigned long c;
unsigned long c=200000UL;
|
long long |
Syntax:
|
|
long long variable name;
|
Size:
|
|
|
Range:
|
-
Dec: 9223372036854775808..
9223372036854775807
-
Hex: 0x8000000000000000.. 0x7FFFFFFFFFFFFFFF
-
Const: LLONG_MIN..LLONG_MAX
|
Description:
|
| long long integer. |
Examples:
|
|
long long c;
long long c=200000LL;
|
unsigned long long |
Syntax:
|
|
unsigned long long variable name;
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Unsigned long long integer. |
Examples:
|
|
unsigned long long c;
unsigned long long c=200000ULL;
|
float |
Syntax:
|
|
float variable name;
|
Size:
|
|
|
Range:
|
-
Dec:
1.17549e-38..
3.40282e+38
-
Const: FLT_MIN..FLT_MAX
|
Description:
|
| Single precision floating point. |
Examples:
|
|
float c;
float c=200.8F;
float c=-2e-8F;
|
double |
Syntax:
|
|
double variable name;
|
Size:
|
|
|
Range:
|
-
Dec:
2.2250e-308.. 1.7976e+308
-
Const: DBL_MIN..DBL_MAX
|
Description:
|
| Double precision floating point. |
Examples:
|
|
unsigned char c;
unsigned char c=200.8;
unsigned char c=-2e-8;
|
long double |
Syntax:
|
|
long double variable name;
|
Size:
|
|
|
Range:
|
|
|
Description:
|
| Long double precision floating point. |
Examples:
|
|
unsigned char c;
unsigned char c=200.8L;
unsigned char c=-2e-8L;
|
Pointer |
Syntax:
|
|
type *name;
type (*func_ptr)(type1, type2,..);
|
Description:
|
| Contains the memory address of another variable |
Example1:
|
|
int x=10;
int *px;
/* px points to x -
px contains the address of
x */
px = (int *)(&x);
/* x now equal to 20 */ *px = 20;
|
Example2:
|
|
int square(int x)
{
return x*x;
}
int main(void)
{
/* function pointer declaration
*/
int (*pfunc)(int) = square;
/* call function -> y=100 */
int y = (*pfunc)(10);
}
|
Array |
Syntax:
|
|
type name[size]; type name[size1][size2];
|
Description:
|
| Contains a set of variables of the same type |
Example1:
|
|
int x[10];
int y[10][20];
int z[5]={10,20,30,40,50};
char s[]="Hello";
int i,j;
/* reset x array */
for(i=0; i<10; i++) {
x[i] = 0; for(j=0; j<10;
j++) {
y[i][j] = 0; }
} /*
*/ *px = 20;
|
Example2:
|
|
int square(int x)
{
return x*x;
}
int main(void)
{
/* function pointer declaration
*/
int (*pfunc)(int) = square;
/* call function -> y=100 */
int y = (*pfunc)(10);
}
|
Structure |
Syntax:
|
|
struct tag {
type1 member1;
type2 member2; ... } name;
|
Description:
|
| Contains a set of variables of different types |
Example1:
|
|
struct time {
int h;
int m;
};
struct time *pt;
struct time dt;
struct time t1 = {8,0};
struct time t2 = {16,0};
dt.h = t2.h - t1.h;
dt.m = t2.m - t1.m;
if(dt.m < 0) {
dt.m += 60; dt.h -= 1;
}
|
Example2 - bit fields:
|
|
struct reg {
unsigned int in
: 3;
unsigned int out
: 4;
unsigned int reserved : 9;
};
int input;
int output;
struct reg r=0; // sets all bits to 0
input = r.in;
output = 3;
r.out = output;
|
Union |
Syntax:
|
|
union tag {
type1 member1;
type2 member2; ... } name;
|
Description:
|
| Contains a set of variables, that are allocated
on the same memory |
Example:
|
|
union data {
unsigned long num;
char name[4];
} union data d1.name="abc";
union data d2.num =0x00434241;
/* now d1 is equal to d2 */
|
typedef |
Syntax:
|
|
typedef old_type
new_type;
|
Description:
|
| New type definition |
Example:
|
|
typedef unsigned char Byte
typedef unsigned int Word
typedef struct time {
char h;
char m;
} Time;
Byte c=255;
Word x;
Time t;
|
extern |
Syntax:
|
|
extern type name;
|
Description:
|
| External variable declaration, used in other files
to broaden the variable scope to other files. The external
declaration is usually written in header file. |
Example:
|
|
See Program Structure
|
static |
Syntax:
|
|
static type name; static type
function(type arg1,...)
|
Description:
|
| Static variables are freed only when
program ends, even when defined inside a function. Static variables
have local file scope for global variables, and local
function scope for local variables.
Static functions have local file scope and are not
visible outside the file. |
Example:
|
|
// external static variable
static int a;
void myfunc()
{
// local static variable
static int x=10;
x = x+1; /* =10,11,12,...
*/
/* for each function call*/
}
|
const |
Syntax:
|
|
const type name=value;
|
Description:
|
| Constant variable - can't be changed |
Example:
|
|
const int linesnum=100;
linenum = 0; /* error! */
|
auto |
Syntax:
|
|
auto type name;
|
Description:
|
| Declare local variable inside function. Can be omitted
from the code. |
Example:
|
|
void myfunc()
{
auto int x;
...
}
|
register |
Syntax:
|
|
register type name;
|
Description:
|
| Declare local variable inside function. Suggests compiler to keep variable in CPU
register. Used for optimization. |
Example:
|
|
void myfunc()
{
register int x;
...
}
|
volatile |
Syntax:
|
|
volatile type name;
|
Description:
|
| Avoid compiler optimization, used when can be changed
externally (i.e: I/O
addressed variable). |
Example:
|
|
volatile unsigned short data;
|
|