C language learning notes

Posted by Steveo31 on Sun, 20 Feb 2022 04:18:37 +0100

C language learning notes

catalogue

  1. Chapter I

  2. Chapter II

Chapter I

Review questions:

The source code contains the code written by the programmer in any programming language.
The object file contains the machine language code
 An executable program is a completed machine language code
Define program objectives-->Design procedure-->Write code-->compile-->Run program-->Testing and commissioning procedures-->Maintenance and modification procedures
Compiler tasks:Convert the source code into equivalent machine language.
Linker tasks:Link the compiled object code, library code and startup code to generate executable files

5. Exercise questions (code_0):

#include <stdio.h>
int main()
{
    double a;
    printf("Please input a value whose unit is inch: ");
    scanf_s("%lf",&a);
    getchar();
    a = a*2.54;
    printf("\n%.3f",a);
    getchar();
}

Chapter II

Composition of C code:

​ C_Source_Code = {preprocessing instruction (e.g. #include) - >

int main(): {statement (content of function)} – >

function a(): {statement} – > function B (): {statement} – >...}

Statement = {label statement, compound statement, expression statement, selection statement, iteration statement, jump statement}

Data type: See next section

Code analysis: by This code example

a = a*2.54 is an assignment statement, in which the left a is called lvalue and the right value is called rvalue= The right is used to calculate and read the value, and then pass this value to the variable on the left of = as its new value.

The real parameter in printf() is "\ n%.3f", where \ n is the newline character,% 3 % in F is used to remind the program. Here it is used to print a variable, 3 F means to print a floating-point number with three digits after the decimal point. The effect here is similar to that in python

a = 2.3434343
print(f"\n{'{:.4}'.format(a)}")  # outputs: 2.343

And printf(); Is an expression statement.

Keywords of C

autoexternshortwhile
breakfloatsigned_Alignas
caseforsizeof_Alignof
chargotostatic_Atomic
constifstruct_Bool
continueinlineswitch_Complex
defaultinttypedef_Generic
dolongunion_Imaginary
doubleregisterunsigned_Noreturn
elserestrictvoid_Static_assert
enumreturnvolatile_Thread_local

Keywords cannot be used as variable names or function names. For example, if you define a function called printf(), it will override the original function of printf() when compiling.

eg:

#include "stdio.h"
void scanf(int num);

int main()
{
    int a = 1;
    scanf(a);
    return 0;
}

void scanf(int num)
{
    printf("The number a is : %d",num);
}
outputs:error: conflicting types for 'scanf'

The scanf() function will read the data in the buffer. If a blank character is encountered, it will skip the current and enter the next variable. Note that at the end of the input, the end of the data stream will be marked with a \ n, which will lead to errors when you repeatedly use scanf in the loop.

e.g.

#include<stdio.h>
#include <stdlib.h>

int main(void)
{
    char a,b;
    while(scanf("%c %c",&a,&b) != EOF)
    {
        printf("a = ");
        putchar(a);
        printf(" b = ");
        putchar(b);
    }
    return 0;
}
/*
a input
b input
a = a b = ba An a is entered here
a = 
 b = a
 */

Enter a first and then b. at the end, there are a and b in the buffer, \ n. Then output the first two a and b. Then enter a, and an a \ n is followed in the buffer. At this time, press enter, and there will be another a \ n after a in the buffer. Then scanf sends \ n to the address of a and 'a' to the address of b. So there is

a = 
b = a

Such output. (by default, your input matches the format it requires).

Review questions in this chapter:

  1. C The basic module of language is function
    
    

Chapter III

Floating point number: the storage method of floating point number in C is

Symbol (+ / -)Exponent: xMantissa: xxxxxx

If 3.1415 is recorded as + . 31415 ∗ ( 1 0 1 ) + .31415*(10^1) +.31415∗(101).

int type: range − 2 31 ∼ 2 31 − 1 -2^{31}{\sim}2^{31}-1 − 231 ∼ 231 − 1, accounting for 4 bytes, 32bit,

Use% u when printing values of unsigned int type,% Lu for used long int and% llu for used long long int

%x is printed as the corresponding hexadecimal integer and% o is printed as the corresponding octal integer.

#include <stdio.h>
int main(void)
{
    unsigned int un = 3000000000;
    short end = 200;
    long big = 65537;
    long long verybig = 12345678908642;

    printf("un = %u and not %d\n",un,un);
    printf("end = %hd and %d\n",end,end);
    printf("big = %ld not %hd\n",big,big);
    printf("verybig = %lld not %d\n",verybig,verybig);
    return 0;
}

//Results the compiler version is clang 13.0 system: macos 12.2
un = 3000000000 and not -1294967296
end = 200 and 200
big = 65537 not 1
verybig = 12345678908642 not 1942899938

The h modifier is an integer stage short type Big = 65537 = 00000000 00000001 00000000 00000001 (binary) is truncated into short type and printed according to 00000001, so the result is 1, which is the same as others.

char type

Note that if it is a single character

char a;

So there

a = 't' ;//True
a = "t" ;//False,because character is not string,the \"",means a string
a = t ;//False,because t is a variable here

The value of char is actually an ASCII character.

i.e.

char a = 65;
printf("%c",a); //outputs: A

Escape character

\a,\f,\n,\r,\t,\v,\\,\',\",\?,\0oo,\xhh

Integer formatted output with macro definition

#include <stdio.h>
#include <inttypes.h>
int main()
{
  int32_t me32; //me32 is a 32-bit signed integer variable
  me32 = 45933945; 
  printf("me32 = %" PRId32 "\n",me32); // PRID32 is a macro definition and will be replaced with d defined in inttypes. This advantage is that it must be output according to 32 bits and will not change due to the change of computer bits
  printf("me32 = %d\n",me32);//outputs:45933945 the same is true for the output of the above line. The test environment is the same as above
}

float,double and long double

float can guarantee at least 6 significant digits. Note that the above is reserved*** Floating point number ***The first six digits in the middle of the decimal in a section, such as 0.123456789, only ensure the accuracy of 123456.

For 233.343, only the accuracy of 233.343 is guaranteed.

double can guarantee at least 10 significant digits and 64 bit variables (8 bytes).

Declaration method of floating point number:
double a,b,c,d,e;
a = 3232.322; //Assignment method I
b = 2.32E+12;//Assignment method II
c = 2.99e-3;//Assignment method (representation method) III
d = .2; //d = 0.200000
e = 100. // outputs: 100.000000
//Remark: C = 2.99, E-3 is wrong, and there can be no spaces before e (E)

Remark: floating point constant is considered as double by the compiler by default. Adding suffix f or F after floating point constant can make it recognized as float type. Similarly, adding suffix L or l will be recognized as long double

e.g.

#include <stdio.h>
int main(void)
{
    float a = 6.f;   //6f is not allowed, because 6 is recognized as an int type constant, and the f suffix is invalid for int type constants. Similarly, L
    long double b = 0.323l;
    printf("%f %Lf\n",a,b);  //outputs: 6.000000 0.323000
    printf("%d",a); // 0, instead of truncating the decimal part after the decimal point, convert the memory information marked with float a in the way of converting the memory information of int, and then output it, so the result is 0
    return 0;
}

Floating point format output method:% f,%a,%e

e.g.

double c;
c = 5.32e-5;
printf("%f\n",a);
printf("%a\n",a);
printf("%e\n",a);
// outputs:
0.000053
0x1.be46214c80e21p-15
5.320000e-05

%a is to output floating-point numbers in hexadecimal, and the following p-15 means 0 x 1. b e 46214 c 80 e 21 ∗ 2 − 15 0x1.be46214c80e21*2^{-15} 0x1.be46214c80e21∗2−15

Range of floating point numbers

#include <stdio.h>
#include <float.h>
int main(void)
{
    printf("%e %e\n",FLT_MAX,FLT_MIN);
    printf("%e %e",DBL_MAX,DBL_MIN);
    return 0;
}
/*outputs:
3.402823e+38 1.175494e-38
1.797693e+308 2.225074e-308
*/

Where FLT_MAX,FLT_MIN,DBL_MAX,DBL_MIN is the header file float The macro definition in H represents the maximum value and minimum value of float type and the maximum (small) value of double type respectively.

Therefore, floating-point numbers have a range. If the value of a floating-point number exceeds its maximum value, it is called overflow. In C, the number will be assigned inf

#include <stdio.h>
#include <float.h>
int main(void)
{
    printf("%Le",LDBL_MAX+1.00e+308);
    return 0;
}
//outputs: inf

Suppose a is the smallest float variable that can be represented by a float, and then perform an operation on a to further reduce its exponent. At this time, the significant digits of a will decrease, that is, a loses the highest accuracy, which is called underflow, such as a = 0.1234E-10; a/10; Outputs: a = 0.0123E-10 (this is only an example and does not apply to actual C) The last digit of 4 is lost, because it has reached the minimum value of - 10 that can be displayed by the index of floating-point number (we assume it is - 10, but it is not, and the situation will be more complex). In order to achieve the effect of a / 10, we can only change the front 0.1234 of E to 0.0123 (move one digit to the right). At this time, the accuracy is lost.

Here is how the accuracy of a is lost when a = 1.175494e-38 (the minimum value that can be accurately described by float) is taken

#include <stdio.h>
int main(void)
{
    float a = (float)1.175494E-38;
    printf("%e",a);
    for(int ix = 1;ix <10;ix++)
    {
        a = a/10;
        printf("\n%e",a);
    }
    return 0;
}
outputs:     | Lost precision bits
1.175494e-38 | 0
1.175493e-39 | 1
1.175493e-40 | 1
1.175549e-41 | 3
1.175689e-42 | 3
1.177091e-43 | 4
1.121039e-44 | 6
1.401298e-45 | 7
0.000000e+00 | all
0.000000e+00 | all

Error of other floating point calculation

#include <stdio.h>
int main(void)
{
    float a ,b;
    double c;
    a = 2.0E20f;
    c = 2.0E20;
    b = (2.0E20f) +1.0f;
    printf("%f\n",c);
    printf("%f\n",a);
    a = b - (float)2.0E20;
    printf("%.26f",a);
    return 0;
}
outputs:
200000000000000000000.000000   c As double There is no problem with variables because c Higher accuracy
200000004008175468544.000000   There is an error in this step
0.00000000000000000000000000   There is no error in this step, and C Primer Plus The inconsistency may be due to the relatively new compiler version

complex

#include <stdio.h>
#include "complex.h"
int main(void)
{
    complex float a;
    a = 1.0+2.0I;
    printf("%f+%fi", crealf(a), cimagf(a));
    return 0;
}

i (capital i) is used in the complex − 1 \sqrt{-1} −1 ) i can also be used, but a = 1.0 + 2.0 * i cannot be used. In this way, i will be considered as a variable by the compiler and no longer a variable − 1 \sqrt{-1} −1 ​.

Boolean value

_ Bool type. In C, 1 represents true and 0 represents false So_ Bool is actually an integer.

Summary

There are 11 keywords in C:

intlongshortunsignedcharfloatdoublesigned_Bool_Complex_Imaginary

For the same integer, the range that the unsigned type can represent (in the positive integer field, it is greater than the signed type), because the first bit is no longer used to represent positive and negative, but also used to represent size.

The accuracy of long double is not necessarily higher than that of double. It depends on how many bits your computer is, but the maximum is no more than 8 bytes.

You can use the sizeof() function to see how many bytes the current variable type occupies, or to see limits H and float There is information about type restrictions in H.

In the case of declaration methods of complex and imaginary numbers (without introducing complex.h)

float _Complex
double _Complex
long double _Complex
float _Imaginary
double _Imaginary
long double _Imaginary

Tips:

When naming the variable name, add some representations before it. For example, the prefix of unsigned short variable means that it is us_ You can help yourself understand the type of this variable through the variable command and improve the readability of the code.

Formatted input / output of string

One way for C to store strings is to use the character array char a []

Note that each character occupies one bit, and in this character array, the end must be \ 0. 0 is a non printing character. C will stop printing when printing to \ 0

#include<stdio.h>

int main(void)
{
    char a[] = {'a','b','b','\0','v','d'};
    printf("%s",a);
    return 0;
}
/* outputs:
abb
*/

You can see from the example that the characters after \ 0 are no longer printed.

And scanf will stop when it reads the space '' when reading the string.

#include<stdio.h>

int main(void)
{
    char a[20];
    scanf("%s",a);
    printf("%s",a);
    return 0;
}
/*
input: hoiwdh nwdiw
output: hoiwdh
*/

C at limits H and floats There are some symbolic constants in H, which are used to tell the user the size limit of the integer and floating-point number (bits, bytes, etc.). See page 67 of c primer plus for details
At present, it is updated here first, and it is moistened 😇

·

Topics: C