Programming language C (II). Basic data

When we write programs using the C language, the data must be defined. Being constant or variable, the corresponding type must be indicated for the compiler to assign the appropriate part and bit sequence to the memo.

When writing programs using C language as in other languages, data should be defined. Being constant or variable, the corresponding type must be indicated for the compiler to assign the appropriate part and bit sequence to the memo.

Data types

C knows the following basic data: characters, integers, real numbers and double precision real numbers. (Table 1 shows the characteristics of each type.)

Table . Characteristics of basic data.

Integers with different characteristics can be defined depending on the use of the sign and the number of bits. Thus, numbers without signs, short numbers or long numbers can be written taking into account the characteristics analyzed in table 2.

The difference between real types lies in accuracy, while float have 7 decimal figures, doubles, have a precision of 15 figures.

Other types can also be used, but always based on the types mentioned. Thus, character strings will be character tables, integers or characters that only use two Boolean values, etc.

Table . Subtypes of integers.

Constants

The constants should not be made before using them, since according to their writing invents the compiler type that corresponds to them. In table 3 you can see some examples in which you can observe that the decimal is the characteristic of the real ones, that of the characters ‘, that of the strings “, the suffix L, that of the long characters. The expression of the constants also uses prefixes to determine the writing of the value we want to memorize. The prefix Ø, for example, indicates that the value is formed by ocular digits, the prefix Øx with hexadecimal digits and E k with mantis and exponent between the two numbers

...

Table . Definition of constants.

There are other constants (mainly used to write results) to indicate special characters. For example,\n is a line-end character,\t tabulation,\b backspace and\Ø is the last in the character string.

Once we see the writing of the constants, we want to remember a criterion that advises structured programming, the use of parameterized constants. Instead of using the constants directly according to this criterion (giving an initial name to the constants), this name will be used in all utilities. In this way, the program will gain in readability and will only be modified in the inscription if value is changed. In C programs of enrollment to constants, the #define pseudo-order is used at the beginning of the programs, with the name of the constants in uppercase (see table 4).

# defines PI 3.14159
main (){

float r, area, perimeter; printf (“press the size of the radius\n”); scanf (“%f”, r); /* Æcon reference*/ perimeter


= 2 * PI * r;
surface = PI * r * r; printf
(“surface: %f\n”;
surface; printf (“perimeter: %f\n”, perimeter);

Table . Use of parameterized constants.

Development and initialization of variables

As can be seen in the program in table 4, all variables must be scorned before being used, using the keywords seen in table 2 the corresponding type will be determined beforehand. In this way, the compiler will know how many bits to save to the variable in the executable program.

When entering the variables an initial value can be assigned, otherwise the compiler will assign them the Ø value. To carry out this initialization in the issue statement you will note after the variable name = character and the value you want to set. For example, when initializing a variable type character, the value a will be assigned with the following sentence:

char = ‘a’

Compatibility and conversions

Unlike languages such as pascal, in C programs operands should not be compatible with type.

If in the assignments and/or operations the operands are of different type, an implicit data conversion occurs, being the type of intermediate variables the longest and most complex among the participants according to the following classification:

        Â

Therefore, to convert a character to capital letters (assuming that it is lower case) can be done through the program in Table 5.

main ({‰ kar–min,
kar–maj;
printf (“press a
character in lowercase\n”) scanf
(“%c”, kar–min); kar–maj
= kar–maj – min–’a’ + ‘A’ printf
(“%c”,
kar–maj);}
Table . Convert the lowercase to uppercase.

As seen in the program, the sum and subtraction of characters is produced, but what will actually be executed is to convert the characters into integers, the addition and subtraction of integers and finally convert the result into characters.

However, it is advisable to specify conversions to avoid errors and gain in readability, since if some C compilers do not explicitly generate warning messages.

To express the explicit conversion, use the element called cast (title) that defines in parentheses the keyword of the data type. For example, to convert a character to an integer (int) will be placed in front of the character variable reference.

Babesleak
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila