-2

data types are many and are used for declaring variables of different types. the type of variable tells us how much space it occupies. this is all good but I didn't exactly understand what are they.

1
1

All variables have a type. Different types (and thus variables) are different sizes, so dictate the range of values that can be stored in it.

For instance the data type int on an 8-bit system like the Arduino UNO is 2 bytes, or 16 bits, in size. 15 of those bits are used to store the value and one bit is used to store the "sign" (whether it is positive or negative). So it can hold between -32768 and +32767.

If you make the datatype unsigned it removes the need to store the sign bit, which frees up an extra bit for storing the value, so it can store between 0 and 65535.

A byte (aka unsigned char) is only 8 bits (one byte, as the name suggests), and is unsigned. So it takes 1 byte of storage and can hold between 0 and 255.

There are more complex data types, such as char *, which is a pointer to an address in memory, the contents of which is to be (normally) interpreted as text.

Types can also be formed into an array of that type with the use of the [x] suffix (x is the number of elements):

char name[20]; 

Types can be combined together in the form of a struct or union to create a complex data type. For instance:

struct person {
    char forename[10];
    char surname[10];
    byte age;
};

You now have the complex data type of struct person.

struct person fred;

That complex type will take 21 bytes of memory - 10 for the forename array, 10 for the surname array, and one for the age.

1

Each data-type is the name of a specification of how to store and compute on data. It includes the amount of storage assigned for one such datum, how the bits in that storage are used to represent the datum, and how calculations or other operations are to be performed on such a datum. Another way of thinking about it is the data type tells the compiler that a given datum is to be treated according to that specification.

The data-types are specified in the language standard, and compilers are written to implement the standard. But a given compiler might not implement the full standard, might implement additional features (extensions) not in the language-standard, or both.

A good C++ reference manual will describe the standard data types and how they are to be supported. A given compiler's documentation should detail any departures it makes from the language standard. See, for instance, the gcc-wiki section for avr-gcc, the compiler supplied with the Arduino IDE. This wiki is where support by this compiler for standard data types is described. Also note the places where non-standard support is shown. 'double', for instance, is implemented as 4 bytes, the same as float, where the standard calls for 8 bytes.

tl;dr: The data type informs the compiler that the associated data are to be stored in memory and operated on according to a particular one of a number of defined specifications.

-1

what are data types in c

they are specifications about the storage and access to variables.

1
  • 1
    Just a heads-up, this answer has been flagged as low quality. You might want to do something about it and expand upon your answer, before it gets deleted. It is rather terse, as it stands, tbh. – sa_leinad Jul 9 '17 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.