C primer plus (under update)

Posted by ace01 on Tue, 01 Feb 2022 23:47:09 +0100

C Language Overview summary

1. In int main(), int indicates that the main() function returns an integer, and viod indicates that main() does not take any parameters

2. / * * / add comments in the middle (multi line comments are allowed), "/ /" can also be commented.

3. For variable naming, lower case, upper case letters, numbers and underscores can be used. The first character of the name must be letters or underscores, not numbers. Operating systems and C libraries often start with one or two underscores, so it's best to avoid such naming

4. A function consists of a function header and a function body. Function header: the name of the function, the information type and the return type of the function. Function body: statement, declaration.

A simple C program has the following format:

#include <stdio.h>
int main(void)
{
    sentence
    return 0;
}

5. The program needs to be readable

6. How to call a function

#include <stdio.h>
void butler(void);
int main(void)
{
    printf("I will summon the bulter function.\n");
    butler();
    pritnf("Yes.Bring me some tea and writeable DVDs.\n");
    return 0;
}
void butler(void)
{
printf("You rang ,sir?\n");
}

Summary of data and c

1. The smallest storage unit is bit.

2 byte s is a common computer storage unit.

3. A word has 8 bits in length. The larger the word length, the faster the data transfer.

4.0X or 0x indicates hexadecimal value.

5. Display numbers in decimal, using% d. Display numbers in octal, using% o. Display numbers in hexadecimal, using% x.

To display the prefixes 0 and 0x of each hexadecimal number, you must use% #o and% #x respectively

#include <stdio.h>
int main(void)
{
    int x = 100;
    
    printf("dec = %d;octal = %o;hex = %x\n",x,x,x);
    printf("dec = %d; octal = %#o; hex = %#x\n",x,x,x);
    
    return 0;
}

Operation results: dec = 100;octal = 144;hex = 64
        dec = 100;octal = 0144;hex = 0x64

6. Print the value of unsigned int, using% u; Print a value of type long, using% ld.

The prefix l can be used before x and o,% lx means to print long type integers in hexadecimal format, and% lo means to print long type integers in octal format.

A single character enclosed in single quotation marks is called a character constant, while a character enclosed in double quotation marks is a string.

char bro;//Declare a variable of type char
bro = 'T';//Assign a value to it. The T is a character and has a corresponding value in ASCII
bro = T;//At this time, T is a variable
bro = T;//At this time, T is a string

The printf function prints characters using% c, floating-point numbers using% f, and floating-point numbers using exponential notation using e

Use sizeof to know the size of the specified type of the current system

#include <stdio.h>
int main(void)
{
    pritnf("Type int has a size of %zd bytes.\n",sizeof(int));
    pritnf("Type char has a size of %zd bytes.\n",sizeof(char));
    pritnf("Type long has a size of %zd bytes.\n",sizeof(long));
    return 0;
}
Operation results; Type int has a size of 4 bytes
    Type char has a size of 1 bytes
    Type long has a size of 8 bytes

When initializing a value of one type to different types, the compiler converts it to a matching value

int cost = 12.99;
float pi = 3.1415926;

The C language compiler will convert the floating-point value of cost into integer, and will directly cut off the part after the decimal point without rounding.

The second one will lose accuracy, because float can only guarantee the accuracy of the first six bits

printf() and scanf()

1.printf function

%c single character% d decimal integer% e floating point count% f floating point% p pointer

2. The conversion description in the format string must match each subsequent item

3.sizeof operator returns the size of a type or value in bytes

4. For example,% 2d is the width of two characters. Adding a number before the conversion description indicates the minimum width

5. For In terms of numbers, it means the accuracy of numbers.

6. The array is composed of continuous storage units. c language uses \ 0 to represent the end of the string.

include<stdio.h>
#define PRAISE "You are an extraordinary being"
    int main(void)
{
    char name[40];
    
    printf("What's your name?");
    scanf("%s",name);
    printf("Hello, %s. %s\n",name,PRAISE);
    
    return 0 ;
}

result What's your name?Angela Plains
    Hello,Angela. You are an extraordinary being

Scanf only reads Angela Plains. It stops reading after it encounters the first blank. According to the% s conversion instructions, scanf will only read one word in the string, not a whole sentence.

7. The strlen function gives the character length of the string

8.#define can define characters and string constants. Double quotation marks are used for strings and single quotation marks are used for characters

Basic operator

Assignment operator '='

1. = assign the right value to the left

2. Lvalue is used to identify modifiable objects. The right value refers to the amount that can be assigned to the modifiable left value, and itself is not an left value.

Addition operator

1. Addition operator is used for addition operation to add the values on both sides.

Subtraction operator

1. Subtraction operator is used for subtraction operation to subtract the number on the left from the number on the right

division operator

1. Integer division will truncate the decimal part of the calculation result and will not round the result. The result of mixed integer and floating-point calculation is floating-point number.

Other Operators

1. Modulus operator

The modulo operator is the remainder. For example, the remainder of 13% 5 is 3, q