Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

A brief introduction to strings

A string is a sequence of one or more characters

Double quotes only tell the compiler that it is enclosing a string, and single quotes are used to identify it

A single character

1.1 Char array and NULL characters

C has no special variable type for storing strings, which are stored in arrays of type char. Arrays consist of contiguous storage units, with characters in a string stored in adjacent storage units, each containing one character

1.2 What is an Array

An array can be thought of as a row of contiguous storage units

An array is an ordered sequence of data elements of the same type

char name[40]

This is an array, and 40 after the square brackets indicates the number of elements in the array

1.3 Characters and Strings

The string constant “x” is different from the character constant ‘x’. One difference is that **’ x ‘is a basic type (char) while’ x ‘is a derived type (char array); The second difference is that “x” actually consists of two characters: ‘x’ and the null character \0**

1.4 Strlen () and sizeof()

The sizeof operator, mentioned in the previous chapter, gives the sizeof an object in bytes. The strlen () function gives the length of the characters in the string

#include <stdio.h>

#include <string.h> // Provide the strlen () function prototype

#define PRAISE "Hello World!" // Define constants



int main(void)

{

  char name[40];

  // The array in Java is int[] arr; The parentheses are in a different place



  printf("Name:");

  scanf("%s",name);

  printf("%zd %zd".strlen(name),sizeof(name));



  return 0; } The result is: Name:pyy3 40
PS D:\Code\C>
Copy the code

The sizeof operator reports that the name array has 40 storage units. However, only the first 11 cells are used to store the Serendipi so strlen () yields 1L. The 12th cell of the name array stores null characters, which strlen() does not count

Constants and C preprocessors

Sometimes constants are used in programs. For example, the circumference of a circle can be calculated in circumference = 3.14159 * diameter;

Here 3.1415926 represents the constant PI (π).

This is best done with a symbolic constant, which is automatically replaced by the following computer

circumference = pi * diameter;

Constants convey more information than numbers

Owed = 0.015 * housevalue;

owed = taxrate * housevalue; The second statement is clearer if you read a long program.

How do I create symbolic constants?

float taxrate;

Taxrate = 0.015;

This provides a symbolic name, but TaxRate is a variable whose value a program may inadvertently change.

C also provides a way to preprocess by adding a line at the top

# define TAXRATE is 0.015

When the program is compiled, all ta.xrates in the program are replaced with 0.015. This process is called compile-time stitution. When the program is run, all substitutions in the program are complete

2.1 Const qualifier

The C90 standard adds the const keyword, **, to qualify a variable as read-only. ** Const int MONTHS = 12; Ll MONTHS cannot be changed in the program and the value is 12

This makes MONTHS a read-only value. That is, you can use MONTHS in a calculation, you can print MONTHS, but you can’t change the value of MONTHS. Const is more flexible than #define

Const modifies variables and is read-only

Printf () and scanf ()

While printf () is the output function, scanf() is the input function. It works almost the same way

3.1 the printf ()

The instruction that asks the printf() function to print data matches the type of data to be printed. For example, use %d for printing integers and %c for printing characters. These symbols are called conversion Specifications, and they specify how to convert data to displayable

Conversion instructions and printed output

The conversion instructions in the format string must match each subsequent item!

3.1.1 Conversion description modifier for printf ()

Inserting a modifier between % and the conversion character modifies the basic conversion specification

Common:

Number: refers to precision

5.2f means that the field width is 5 and two digits after the decimal point

Number: represents field widths such as %4d

3.2 the scanf ()

If scanf () is used to read the value of the base variable type, prefix the variable name with an ampersand;

If you use scanf() to read a string into a character array, do not use &.

3.2.1 Conversion description modifier of scanf ()

Just to recap…

This book is so damn hard to learn!!

Key concepts

A string is a series of characters that are treated as a processing unit.

char name [ 30]; Make sure there are enough elements to store the entire string (including null characters)

The strlen() function (declared in the string.h header) can be used to get the length of the string (trailing null characters are not counted). When the conversion specification in scanf () is %s, a word can be read.

The C preprocessor finds source code programs for preprocessor instructions (starting with the # symbol) and processes them before starting to compile the program.

The printf() and scanf () functions provide a variety of support for input and output. Both functions use format strings that contain conversion instructions indicating the number and type of data items to be read or printed. In addition, you can use conversion specifications to control the appearance of the output: field width, decimal places, and layout within the field.

5. Programming exercises

The code and results are in the code block!

1.c

#include <stdio.h>



int main(void)

{

  char fname[20], lname[20];



  printf("Please enter your first name: ");

  scanf("%19s", fname); //19 is the field length

  printf("Please enter your last name: ");

  scanf("%19s", lname);

  printf("Hello! %s, %s.\n", fname, lname);



  return 0;

}
Copy the code

2.c

#include <stdio.h>

#include <string.h>



int main(void)

{

  int len = 0;

  char name[20];



  printf("Please enter your name:");

  scanf("%19s", &name);

  len = strlen(name);

  printf("Print your name:\n");

  printf("a.\"%s\"\n", name);// Normal print

  printf("b.\"%20s\"\n", name); // Print the name at the right end of the field of width 20, including double quotes

  printf("c.\"%-20s\"\n", name); // Print the item from the left of the field

  printf("d.%*s\n", len + 3, name);



  return 0;

}





Please enter your name:pyy
Print your name:
a."pyy"
b." pyy"
c."pyy "
d.   pyy
Copy the code

3.c

#include <stdio.h>



int main(void)

{

  float num;



  printf("Please enter a float number:");

  scanf("%f", &num);

  printf("The input is %.1f or %.1e.\n", num, num);1f represents two decimal places
    //.1e represents the floating-point count



  return 0;

}


   
Please enter a float number:2.3
The input is 2.3 or 2.3 e+000. This is C language floating point constant count method2.3 e+02
    2.3*10The quadratic1.0 e+003
    1.0*10The third powerCopy the code

4.c

#include <stdio.h>
#define LEN 30

int main(void)
{
    float heigh;
    char name[LEN];

    printf("Please enter your name:");
    scanf("%29s", &name);/ / to accept the name
    printf("Hello! %s, how tall you are(inch):", name);
    scanf("%f", &heigh);//接受height
    printf("%s, you are %.3f feet tall.\n", name, heigh / 12.0);

    return 0;
}






Please enter your name:pyy
Hello! pyy, how tall you are(inch):178
pyy, you are 14.833 feet tall.
Copy the code

5.c

#include <stdio.h>

#define BIT 8



int main(void)

{

  float speed, size, time;



  printf("Please enter net speed(Mbit/s):");

  scanf("%f", &speed);

  printf("Please enter file size(MB):");

  scanf("%f", &size);

  time = size * BIT / speed; // The conversion unit between b and B is 8
    

  printf("At %.2f megabits per secnod, ", speed);

  printf("a file of %.2f megabytes ", size);

  printf("downloads in %.2f seconds.\n", time);



  return 0; } Simply not explained in detailPlease enter net speed(Mbit/s):10
Please enter file size(MB):20
At 10.00 megabits per secnod, a file of 20.00 megabytes downloads in 16.00 seconds.
Copy the code

6.c

#include <stdio.h>

#include <string.h>



int main(void)

{

  int x, y;

  char fname[20], lname[20];



  printf("Please enter your first name: ");

  scanf("%19s", &fname);

  printf("Please enter your last name: ");

  scanf("%19s", &lname);

  x = strlen(fname);

  y = strlen(lname);

  printf("%s %s\n", fname, lname);

  printf("%*d %*d\n", x, x, y, y);// The function of * is to align with the tail

  printf("%s %s\n", fname, lname);

  printf("%-*d %-*d\n", x, x, y, y);



  return 0;

}


Please enter your first name: pyy
Please enter your last name: caq
pyy caq
  3   3
pyy caq
3   3
Copy the code

7.c

#include <stdio.h>

#include <float.h>



int main(void)

{

  float f_value = 1.0 / 3.0;

  double d_value = 1.0 / 3.0;

//1, %lf double, also known as the format of the double, the default reservation of 6 decimal places.
//2, %.2lf same as above, but limited, the value of 2 decimal places.


  printf("1.0/3.0 display 6 Decimal Places: \n");

  printf("f_value = %.6f\nd_value = %.6lf\n", f_value, d_value);

  printf("\n1.0 / 3.0 display 12 decimal places:\n");

  printf("f_value = %.12f\nd_value = %.12lf\n", f_value, d_value);

  printf("\n1.0 / 3.0 display 16 decimal places:\n");

  printf("f_value = %.16f\nd_value = %.16lf\n", f_value, d_value);

  printf("\nfloat and double maximum significant digits:\n");

  printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);

  FLTDIG = float valid decimal number;

  //↑DBL_DIG stands for double valid decimal digits;



  return 0;

}





1.0 / 3.0 display 6 decimal places:
f_value = 0.333333
d_value = 0.333333

1.0 / 3.0 display 12 decimal places:
f_value = 0.333333343267
d_value = 0.333333333333

1.0 / 3.0 display 16 decimal places:
f_value = 0.3333333432674408
d_value = 0.3333333333333333

float and double maximum significant digits:
FLT_DIG = 6, DBL_DIG = 15
Copy the code