C language program input and sequential programming - learning 6

Posted by michaelnorth on Mon, 28 Feb 2022 14:45:08 +0100

Format input

  • scanf function
  • General form: scanf (format control, address list)
    • The format control is consistent with the format control of printf function
    • The address list can be the address of a variable or the first address of a string

Tips

Using the scanf function directly in the vs new compiler will prompt a warning and cannot be operated, and scanf will be recommended_ s() , You can add a line #pragma warning(disable:4996) in front of the file header and continue to use the scanf function.

Their differences:

  • scanf() does not check the input boundary, which may cause data overflow_ S () will check the boundary.

For example: name[5]

  • Scanf ('% s', name). If abcdefgh is entered, fgh will overflow into other places and cause program errors.
  • scanf_s("%s",name), will only receive the first five inputs, and the subsequent ones are invalid, avoiding the vulnerability of the program.

Format declaration in scanf function

Similar to the format declaration in the printf function, it starts with% and ends with a format character. Additional characters can be inserted in the middle

  • For example: scanf ("a =% F, B =% D, C =% s", & A, & B, & C)
    • "&" is an address operator and & A is an expression whose function is to find the address of a variable. This address is the address of the storage space allocated by the compiling system to the a variable in memory.
    • The value of a variable and the address of a variable are two different concepts. The address of the variable is assigned by the C compilation system
    • The relationship between the address of the variable and the value of the variable is as follows: For example: a=123; Then a is the variable name, 123 is the value of the variable, and the starting address is 1001.

example

#include <stdio.h>


void main(){
    int a, b, c;
    scanf_s("%d%d%d", &a, &b, &c); 
    printf("%d,%d,%d\n", a, b, c);

}

Problems needing attention when using scanf function

1. There is no precision control in scanf function.

  • Error: scanf (% 7.2f ", & A);
  • Correct: scanf (% F ", & A);

2. It is required to give variable address instead of variable name in scanf culvert number

  • Error: scanf ('% d', a);
  • Correct: scanf ('% d', & A);

3. If there are no non format characters in the format control string as the interval between input data, space, TAB or carriage return can be used as the interval. If there are non format characters, these characters should be input at the corresponding position during input.

4. When inputting character data, if there are no non format characters in the format control string, all input characters are considered to be valid characters.

5. If there is an "*" additional specifier after% indicates that the number of columns specified by it is skipped.

example:

The input format characters are separated by commas. When entering on the console, you should also enter commas for separation

#include <stdio.h>


void main(){
    int a, b, c;
    scanf_s("%d,%d,%d", &a, &b, &c); 
    printf("%d,%d,%d\n", a, b, c);

}

If the character is a valid character, it can't be used here_ S is implemented because scanf_s mechanism for checking the input boundary, resulting in program error and unable to continue execution

#include <stdio.h>
#pragma warning(disable:4996)  

void main(){
    char a, b, c;
    scanf("%c%c%c", &a,&b,&c); 
    printf("%c,%c,%c\n",a,b,c);

}

%Followed by a "*" additional specifier

#include <stdio.h>

void main(){
    int a, b;
    scanf_s("%2d %*3d %d", &a, &b); 
    printf("%d,%d\n", a, b);

}

Sequential structure design

Example 1

Enter the length of three sides of the triangle and calculate the area of the triangle. Let the input three side lengths a, b and c form a triangle.

  • Square root of sqrt function
  • Calculate the area of a triangle using Helen's formula
#include <stdio.h>
#include <math.h>

void main(){
    float a, b, c, p, area;
    scanf_s("%f %f %f", &a, &b, &c); 
    p = (a + b + c) / 2;

    area = sqrt(p *(p - a)*(p - b)*(p - c));

    printf("a=%.2f b=%.2f c=%.2f p=%.2f area=%.2f\n",a,b,c,p,area);

}

Example 2

Input a capital letter from the keyboard and output it in lowercase letters.

  • Letters are stored in ASCII code in memory
  • The ASCII code of uppercase letters is 32 less than that of lowercase letters
#include <stdio.h>

void main()
{
    char c1, c2; 
    c1 = getchar(); 
    c2 = c1 + 32;

    printf("%c\n",c2);
}

Example 3

Seek ax ² The root of tbx+c=0 equation. a. b, c are input by keyboard and set b ²- 4ac>0.

Root formula:

  • The root formula can be divided into two parts

x1 = p + q; x2 = p - q

#include <stdio.h>
#include <math.h>

void main()
{
    float a,b,c,x1,x2,p,q; 
    scanf_s("%f %f %f", &a, &b, &c);

    p = -b / (2 * a);
    q = sqrt(b * b - 4 * (a*c)) / (2*a);

    x1 = p + q;
    x2 = p - q;

    printf("x1=%.2f,x2=%.2f\n",x1,x2);
}