Data in languages C/C++
Data types
The data type is determined by a set of values that can take data and a set of operations that can be performed above the data.
Data type | Description | Memory[bit] |
---|---|---|
char | Short integer data. Used to store characters | 8 |
short | Short integer data | 16 |
int | Integer data | 16 or 32 |
long | Long integer data | 32 |
float | single precision floating point | 32 |
double | double precision floating point | 64 |
- Variables:
- They can change the value during the program
- Memory must be reserved
- example: int a; // the memory for integer data is scaled
- Constants
- No memory is saved
- They are used in designs
- Example: Y = 2 * X; // 2 is a constant
- Symbolic constants
- Constants represented by an identifier
- example:
#define PI 3.14
P=r*r*PI;
- Constant data
- Memory is recovered
- Data must be initialized
- do not change the value during the program
- Example: const double e = 2.71828;
There are the following types of data:
- Primitive data types
- Derived data types
- The type of void
- Enumeration
Primitive data types
- short 16 bits short integer data
- int 16 or 32 bits integer data
- long 32 bits long integer data
- long long 64 bits long long integer data
Longer integer data may contain larger numbers because it uses more memory to store values. It is easy to calculate the maximum number that can be placed in eg long long. This would be in binary form a number containing 64 times 1. Eg. 111111 ....
so that would be a number 263+262 +...+20 =18446744073709551615
Example:
int a;
int b;
Floating point data:
- float 32byts single floating point
- double 64 bits double floating point
Example code using character:
char c;
c='A';
printf(“ASCII code of character %c iznosi %d\n”,c,c);
Derived data types
Arrays : If a series of numbers of the same type is to be defined, e.g. We then define the integers as follows:
int A[10];
Pointers type: If a pointer to a double data type is to be defined, then the pointer is defined and initialized as follows:
double a;
double *pA;;//Pokazivač na podatak tipa double
pA=&a;
Of the derived data types, structures, unions, and functions are also used.
The C programming language uses the type void code of functions, when the function does not return any value. Then the official word c of the void language is used for the return value type.
Signed i unsigned data
int a;
signed int b;
unsigned int c;
The integer signed data, if int occupies 32 bits of memory are in the range from -231 to 231 or from -32767 to 32767,
but
unmarked one are in the rank of 0-232 tj od 0-65535.
The first digit of the highlighted integers is used to specify the character (0-positive, 1-negative) while the remaining digits are used to show the value of the number. Therefore, the highest value of numbered numbers is 231 not 232. Other types, float, double, char can be signed and unsigned:
unsigned float x;
signed double y;
unsigned char z;
Variables
type_of_data data_name;
For example. if two integers are concerned then the official word int is used for the data type and the data is defined as follows:
int a,b;// or in two rows
int a;
int b;
a
b
For more about operators in language c, see:
https://izprogramiranja.weebly.com/operators_in_c_and_cpp.html
This does not mean equality and does not apply to the left of the "=" sign as to the right. Here on the left there must be a memory that needs to get some value and on the right is that value or some expression whose result will be stored in the memory on the left. For example. assign some values to variables a and b:
a=14;
b=22;
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 |
---|
a
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
---|
b
14 |
---|
a
14 |
---|
b
int c;
c = a + b;
36 |
---|
c
This is a printf function from the stdio.h header in c (see read and write data below) or in c ++, the cout function from the iostream header can be used.
In the previous example, the programmer commanded a = 14; and b = 22; programmatically determined the values of a and b and the program user would not be able to influence them. In most cases, it is necessary to ensure that the user enters the values as they wish, and to do so, a command is provided that the number typed on the keyboard is temporarily memorized by the user as a string of characters and then converted with the appropriate input binary conversion. See data reading below.
Constant data
const double e = 2.718281;
Constants
They are not reserved memory, but are simply used in expressions. However, constants also have their own data type. For example, 2 is an integer constant of type int, 2. or 2.0 are of type double, while 2f would be of float type.
Symbolic constants
#define PI 3.14;
P = r * r * PI;
Header files
We call these files header files. In C, they have the extension .h. For example. to be able to use the printf and scanf functions to write and read data to standard output, ie. input we need to include the stdio.h file:
#include <stdio.h>
Either you need to include the math.h file in c, or the cmath file in c ++ to use the math functions.
#include <math.h>
A useful site for viewing header files in c and c ++ is:http://www.cplusplus.com/reference/
Reading and writing data in C
The stdio.h file must be included among the preprocessor directives
#define<stdio.h>
Celestial input conversion
scanf(format, &podatak,&podatak,....,&podatak)
The format is text data. Individual conversions are expressed in sub-formats:
% nq, where q is the conversion code, and n is a supplementary conversion parameter.
Input Conversions: i, d,u, o, x,.
for short: hi,hd....
for long: li, ld,...
Celestial output conversions: i, d,u, o, x,.
for short: hi,hd....
for long: li, ld,...
Input Real-Number Conversions:
for float: f,e,g.
for double: lf, le,lg
for long double: Lf, Le,Lg
Output Real-Number Conversions:
for float: f,e,g.
for double: f, e,g
In the previous example of summation, it remains to show how data from memory is displayed on the screen.
The stdio.h file provides a printf whose syntax is:
printf(format,var1,var2,..);
In our example, this would be:
printf(“%d”,c);
After executing this command, the console would display:
If we would like to combine the output of the text and the number in the output, e.g. if we write "The sum of numbers 14 and 22 is 36", then the format parameter would actually be text with output formats for the output conversion for in this case integers "% d" or "% i". The parameters in the printf function, given after the format, are actually variables whose conversion is done, written in the order from left to right:
printf(“Sum of numbers %d and %d is %d”, a, b, c);
Values for assigning values to variables a and b.
a=14;
b=22;
should be replaced by:
scanf(“%d%d”, &a, &b);
The "&" operator provides the memory address of the data and is used for input and not for output conversion.
Before the scanf command, a printf command must be added to print a message, which tells the user what information to enter.
A complete task code would be:
- Enter the radius of the circle and calculate the area
- Load trapeze pages and calculate area.
- Load time in seconds and print as hh: mm: ss
Tasks Solutions
We solve the problem in 4 steps:
First, we need to define the information we need: In the program, the user needs to enter the radius, and the program should calculate the area of the circle. To define these data we will give them names and put the data type for real numbers (double). So the first command:
double r,P;
This is where we reserved the memory for two data of the double type we marked with r and with P
The second step is to enable the user to enter a semicolon. We use the method to enterscanf which is in the file stdio.h
We will include this file using the #include directive for inclusion of files, and this is written at the top of the document
#include < stdio.h >There is still a method in this file printf for printing data and text on the console.
Before typing, we print a message on the console so that the user knows what to enter. For printing, we use the printf method, which actually represents the output conversion. The input conversion is done using the scanf method. This second step is:
Figure 4: Circle area-steps
In step 4, a message with the surface value, which is read from the memory marked in this program with P., is displayed. The %f format for double data is used for the output conversion.
Example 3-solution
Here the user enters the time in seconds which should be an integer
int timeSek;
scanf(“%d”, &timeSek);
time in seconds and 3600, timeSek/ 3600, and to get how many seconds left, we use "%", ie timeSek% 3600.
Code that does this:
int hh, restSek;
hh = timeSek / 3600;
restSek= timeSek % 3600;
Similarly they would get minutes from the remaining seconds (restSek), but in this case they would take 60 for the divider because 1min = 60 sec;
int mm;
mm = restSek/ 60;
restSek= restSek% 60;
printf(“%02d : %02d : %02d”, hh, mm, restSek);
Next
Operators in C and C++>| |