What are data types in c

Beginning back next

3. C data types


For the processing of information in a program, places (variables) must be reserved in the memory of the computer to receive this information. Before these variables can be used, they must first be declared. This happens at the beginning of a function before any executable statements. The declaration describes the properties of variables and consists of a type specification and a list of variables (names) which then have this type. e.g .:

int a, b; float x, y; char ch;

Here is a list of all elementary numeric variable types available in C:

characterchar1-128 .. 127
unsigned characterunsigned char10 .. 255
integrityint2-32 768 .. 32 767
short integershort2-32 768 .. 32 767
long integerlong4-2 147 483 648 .. 2 147 483 647
unsigned integerunsigned int20 .. 65 535
unsigned short integerunsigned short20 .. 65 535
unsigned long integerunsigned long40 .. 4 294 967 295
single-precision floating-point (7 digits)float41.17E-38 .. 3.4E38
double-precision floating-point (19 digits)double82.2E-308 .. 1.8E308

Variable names must obey the following rules:

  • The name can consist of letters, numbers and the underscore _. At least the first 31 characters of a name are significant. Variable names are usually written in lower case in C.
  • The first character must be a letter. The underscore is considered a letter, but it should not be used in the first place.
  • The reserved words of the C programming language cannot be used as variable names.
Table of reserved keywords:
automobile const double float int short struct unsigned
break continue else for long signed switch void
case default enum goto register sizeof typedef volatile
char do external if return static union while

Constant variables
In addition to the actual variables for which a change in value is permitted, "constant variables" can also be declared in C that no longer allow the value to be changed. The general formula looks like this:

const type name = value; Examples: const int apostle = 12; const double PI = 3.14159265;


In addition to variables, constant values ​​are often used in the program. A constant value is a fixed expression that cannot be changed. Constants are used to assign values ​​to variables or as parts of expressions. For example, the expression only consists of constants, while in a variable (x) and a constant occur.
One differentiates:

Integer constants
such as.:

68 / * signed short int * / -68 / * signed short int * / 68U / * unsigned short int * / 68L / * signed long int * / 1234567 / * signed long int * /

Floating point constants
Both the notation and can be used for floating point constants. Valid constants are:

0.00012 123.456 1.0e-12 12.3E23 1. 1E1

Character constants
Character constants are enclosed in apostrophes. Characters that cannot be printed or are not available on the keyboard are specified as escape sequences.

'a' '\ n' '\ t' '\ x41' Internally characters are managed as values, so that and actually represent the same thing: #include int main (void) {printf ("% c is in ASCII:% d \ n ", 'a', 'a'); printf ("% c is really in ASCII:% d \ n", 97, 97); return 0; }

Data type conversions

In contrast to other programming languages, data types in C can be converted into one another. In C, casts are either implicitly or explicitly performed using a special construct. In a mixed expression, a type conversion is carried out implicitly in such a way that an attempt is made to convert to the largest type of the expression. The type order is:

The rules for converting are:
  • Is an operator long double, the other will also be in long double transformed.
  • Is an operator double, the other will also be in double transformed.
  • Is an operator float, the other will also be in float transformed.
  • Otherwise it will be char and short in int changed.
  • If then one of the operands long is, the other is in long changed.
If the expression is assigned to a variable, a conversion may have to be made. The following list shows examples of conversions of corresponding variables: char c; int i; long l; float f; double d; c - i conversion int d = c - i conversion int, assignment to double l * 2.0 - i conversion double c = l * 2.0 - i conversion double, assignment to char f * d conversion double l = f * d conversion double, assignment an long 1 + 3.0 Conversion double 'a' + 1 Conversion int i = i / d Conversion double, assignment to int In any expression one can use a unary conversion operation, a so-called type-castthat enforce explicit casts. For (type-name) expression, the value of the expression is converted using the rules given above. In the program #include int main (void) {int n = 1; double d; d = n / 3; printf ("% lf \ n", d); return 0; } Although the assignment is made to a double variable, the output would result in zero. With a corresponding type-cast, the calculation is carried out in such a way that the output delivers as required. #include int main (void) {int n = 1; double d; d = (double) n / 3; printf ("% lf \ n", d); return 0; } When calculating the expression, since the constant is an integer constant, an additional implicit conversion is carried out.

Which operators are there in C?