Types & Modifiers

Submitted by M E Senturk on Fri, 05/08/2009 - 00:07

Variables are in almost everywhere in our lives(i assume you have a social environment, at least you may go out once a week). We have decimal numbers , integers and chars to express our needs. In c, we have variables like we have in our lives, and these variables has various types.

Types of C

int

 integer
float

decimal, you can store decimal numbers up to E-16 but you can only show 6 decimal digits.

double decimal, you can store decimal number up to E-32 but you can show only 6 decimal digits.
char just a character like 'C' or 'z' or '_'
void a type that is used for functions that does not return a value. We will get to that subject in functions part.
enum

a type that can be used to create collections of named integer constants.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Of course, there will be an example code for how you can use these types with variables.

#include <stdio.h>

int main()
{
int laptops = 3;
float temperature = 25.6; //in Celsius
float weight_of_a_dust_particle = 16E-16; //in kg
double diameter_of_dust_particle = 16E-32; //in km
char grade_of_a_student = 'A';

return 0;
}

and another example for "enum"

#include <stdio.h>
int main()
{
enum numbers{zero,one,two,three,four,five,six,seven,eight,nine};
/*
By using enum, we set constants as
zero==0
one==1
two==2
three==3
.....
eight==8
nine==9
*/

/*here's another approach*/

enum nonsense{a,b,c,d=10,e,f,g=20,h,i};
/*
a == 0
b == 1
c == 2
d == 10
e == 11
f == 12
g == 20
h == 21
i == 22
*/
return 0;
}

Sometimes, you may not want to use "enum". You can use "#define" in order to declare constants.

#include <stdio.h>
#define PI 3.14
#define Kelvin 273.15
#define wonderful LOVE
#define bush IDIOT

int main()
{
/*
We set constants as
PI == 3.14
Kelvin == 273.15
wonderful == 'LOVE'
bush == 'IDIOT'
*/
return 0;
}

You may also use "const" to make constants.

#include <stdio.h>
int main()
{
const float pi=3.14;
const int coefficient = 2;
return 0;
}

If you put "const" before you declare type, variables will be read-only.

After you finished with variable types, you need to know about integer modifiers.

C Integer modifiers
short A short integer usually occupies half the space of a normal
integer and the normal integer depends on the machine word size.
long A four byte long integer will allow values from 2,147,483,647
to 2,147,483,648 (231 - 1 to -231)
unsigned An unsigned integer occupies the same space as an integer but
has no negative values, for example for a two-byte integer the
range of values would be 0 to 216 - 1 i.e. 65535.
A four byte integer would range from 0 to 232 - 1 i.e.
4294967295
ushort
ulong
The unsigned short and long modifiers can be abbreviated
#include <stdio.h>

int main()
{
short int a_number = 1;
long int another_number = 2147483647;
unsigned int = 254;

return 0;
}

You need to be careful when you name your variables. For example, you cannot but blank space character in a variable.

int number one /// wrong

int number_one /// right

on the other hand, if you don't start to name a variable with underscore '_', it will be a clever action, because there are lots of variables and some other stuff named with underscore started words.

Maximum variable name lengths varies with compilers, but you try not to exceed 30.