Getting to know C language exercises and knowledge points

knowledge points

1.  Local variables: Generally, variables defined in functions are called local variables, which can only be used inside the function.

  Global variables: Variables defined in the global scope, that is, variables outside the function, are called global variables. The life cycle of global variables is born when the program starts, and dies when the program ends, and can be used in any function.

Note: Global variables are convenient to use, but in order to prevent conflicts and security, try to avoid defining global variables.

2. It is not allowed to define multiple variables with the same name in the same scope.

3. Multiple variables with the same name are allowed to be defined in different scopes.

4. Variables defined in different scopes adopt the proximity principle when accessing them.

5. The data types of C language include built-in data types and custom data types; built-in data types include: char, short, int, etc.; custom data types include: struct, enum, etc.

6. EOF end of fire, the end mark of the file

7. strlen calculates the size of the string, and ends when '\0' is encountered

8. \b escape character, backspace

9. Keywords cannot be defined by themselves, nor can they be used as variables, because keywords have their own special meanings.

10. define is not a keyword, define in #define is a preprocessing instruction

11. For the larger value of the two functions, use the function, x>y return x, this way of writing is simpler than using the third variable as an intermediate value;

If you use a function, you can also directly return the ternary operator. If the function is not used, if (x>y) max=x; the ternary operator is also available.

12. When performing addition, subtraction, multiplication and division, if the types are different, remember to perform type conversion and automatically discard problems after the decimal point.

programming questions

(1) Input the grades (integers) of 5 students from the keyboard, and find their average grades (floating point numbers, keep one decimal place). Input description: One line, enter 5 integers (range 0~100) continuously, separated by spaces. Output description: one line, output the average of 5 numbers (reserve one decimal place)

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    double f = 0;
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
    f = (double)(a + b + c + d + e) / 5;
    printf("%.1lf", f);
    return 0;
}
#include <stdio.h>

int main()

{

    int i = 0;

    int sum = 0;

    int input = 0;

    for(i=0; i<5; i++)

    {

        scanf("%d", &input);

        sum += input;

    }

    printf("%.1f\n", sum/5.0);

    return 0;

(2) Reversely output four digits

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    scanf("%d", &a);
    b = a % 10;
    c = (a / 10) % 10;
    d = (a / 100) %10;
    e = a / 1000;
    printf("%d%d%d%d", b, c, d, e);
    return 0;
}

 Common ideas for reverse input, octal and binary numbers, can be %2/2, %8/8



#include <stdio.h>

int main()

{

    int n = 0;

    scanf("%d", &n);//1234

    while(n)

    {

        printf("%d", n%10);

        n = n/10;

    }

    return 0;

}

Guess you like

Origin blog.csdn.net/m0_57388581/article/details/125566430