7-5 Experiment 1_ 5_ Data type (100 points)

Posted by aouriques on Fri, 10 Dec 2021 15:46:19 +0100

C language provides programmers with rich data types. The commonly used data types are character type (char type), short type (short type), integer type (int type), long type (long type), extended long type (long type), single precision floating point type (float type) and double precision floating point type (double type). Your task is to define all the above variables in the main program. The variable names are a, b, C, d, e, f and g in the above order. Read these different types of data in the above order and output them in the original order. Finally, output the number of bytes of these variables in your operating system. Here, you need to use the operator sizeof() to calculate the number of memory bytes occupied by variables.

Tips:

Definition of short integer: short b;

Input of short integer variable: scanf('% hd', & B);

Output of short integer variable: printf('% hd', b);

Definition of long integer variable: long d;

Input of long integer variable: scanf('% ld', & D);

Output of long integer variable: printf('% ld', d);

Definition of long long variable: long e;

Input of long long variable: scanf('% lld', & E);

Output of long long variable: printf("% lld", e);

sizeof() usage:

If we want to know the memory space occupied by an int variable in the operating system, use the following code:

int a=0;

printf("%zu\n",sizeof(a));

perhaps

printf("%zu\n",sizeof(int));.

be careful:

1. Under some non ANSI C standard compilers, it is likely that the long long data type is not supported, such as VC6 0, but VS2003 and above support it.

2. The example we give is the result of running under 64 bit linux (the operating system of the problem determination server), which may be different in other environments. Even if the operating system and the results are different, as long as your program is written correctly, it will pass.

3. The output text is ascii characters. For characters such as single quotation marks, pay special attention not to output Chinese characters.

Input format:

There are 7 rows, and each row corresponds to the input of one type of data.

Output format:

It is also 7 rows, and each row corresponds to a type of data and The number of bytes occupied in memory. For example, "The 'int' variable is 200000000, it takes 4 byte.".

Input example:

X
1234
2000000000
2000000000
500000000000000
1.2345
9.87654321

Output example:

The 'char' variable is X, it takes 1 byte.
The 'short' variable is 1234, it takes 2 bytes.
The 'int' variable is 2000000000, it takes 4 bytes.
The 'long' variable is 2000000000, it takes 8 bytes.
The 'long long' variable is 500000000000000, it takes 8 bytes.
The 'float' variable is 1.234500, it takes 4 bytes.
The 'double' variable is 9.876543, it takes 8 bytes.
#include<stdio.h>
int main()
{
    char a;
    short b;
    int c;
    long d;
    long long e;
    float f;
    double g;
    scanf("%c\n",&a);
    scanf("%hd\n",&b);
    scanf("%d\n",&c);
    scanf("%ld\n",&d);
    scanf("%lld\n",&e);
    scanf("%f\n",&f);
    scanf("%lf",&g);
    printf("The 'char' variable is %c, it takes %zu byte.\n",a,sizeof(a));
    printf("The 'short' variable is %hd, it takes %zu bytes.\n",b,sizeof(b));
    printf("The 'int' variable is %d, it takes %zu bytes.\n",c,sizeof(c));
    printf("The 'long' variable is %ld, it takes %zu bytes.\n",d,sizeof(d));
    printf("The 'long long' variable is %lld, it takes %zu bytes.\n",e,sizeof(e));
    printf("The 'float' variable is %f, it takes %zu bytes.\n",f,sizeof(f));
    printf("The 'double' variable is %lf, it takes %zu bytes.\n",g,sizeof(g));
    return 0;
}

Topics: C