Summary of data types, operators, and expressions

Posted by lajollabob on Fri, 31 Dec 2021 21:44:23 +0100

%c , character% d , integer% f decimal

2.1} cited examples

/* output: "programming is fun." */

#include<stdio.h>
int main(void)
{
 printf("programming is fun.");
return 0
}
 
Operation results:
programming is fun.

Example 2: ball volume and surface area

#include<stdio.h>
#define PI 3.14159
void main()
{    int r;
    float v,f;
    r=2;
    v=4.0*PI*r*r*r/3.0;
    f=4.0*PI*r*r;
    printf("The volume is:%f,The surface area is:%f\n",v,f);
 
}
 
Operation results:
Volume: 33.510292,Surface area: 50.265442


2.2 data types of c language

Data types include the following

Basic type: integer, character, real [floating point]

Construction type: array type, structure type, common body type and enumeration type

Pointer type

Empty type

2.3} c language constants

2.3. 1. Direct constant

1. Integer constant

Decimal: consists of numbers 0 to 9

Octal: prefixed with 0 and composed of numbers 0 to 7

Hexadecimal: dotted with ox or ox, it is composed of numbers 0 to 9 and letters A to F

be careful:

1. An integer constant followed by a letter U or u is considered to be unsigned int

2. Add a letter L or l after an integer constant, which is considered as long nt

2. Real constant

1. Decimal form: it consists of integer part, decimal point and decimal part. When the integer part and decimal part are 0, it can be omitted, but the decimal point cannot be omitted

2. Index form: it is composed of tail, letter E or E and index. The format is: positive and negative tail e index

be careful:

1. When expressed in decimal form, there must be a decimal, and there must be a number on at least one side before and after the decimal point

2. When expressing real numbers in exponential form, there must be numbers before and after the letter E, and the exponential part can only be an integer

3. Character constant

1. Character constants can only be enclosed in single quotation marks, not double quotation marks or other brackets

2. Character constants can only be single characters, not strings

3. Characters can be any character in ASCll character set

Escape character

The escape character is formally composed of multiple characters, but it is a character constant and represents only one character

Example 3: use examples of escape characters

#include<stdio.h>
int main()
{    char b,c,d,e;
    b='\43';
    c='\103';
    d='\x2a';
    e='\x41';
    printf("b=%c,c=%c,d=%c,e=%c\n",b,c,d,e);
    printf("\"good\40morning!\b\"!\n");
    prinf("\123456");
    return 0;
}


Double quotation marks represent strings and single quotation marks represent characters

4. String constant

String: characters such as space characters, escape characters, other characters and Chinese characters

2.3. 2. Symbolic Constant

#define is a compilation preprocessing command called macro definition command

Example 5: using symbolic constants to calculate prices

#include<stdio.h>
#define PRICE 20
int main()
{    int num,total;
    num=10;
    total=num*PRICE;
    printf("The total price is:%d\n",total);
    return 0;
}
 
Operation results:
Total result: 200


Advantages of symbolic constants

1. Increase program readability

2. Improve program maintainability

3. Simplified program code

4. Convenient array definition

2.4 c language variables

2.4. 1. Definition of variables

Variable names generally use lowercase letters

When using variables, they should be assigned first and then referenced

Example 6: example of variable usage

#include<stdio.h>
int main()
{    int a,b,c;
     char ch='A';
     a=10;
     b=a+ch;
     scanf("%d",&c);
     printf("%d,%d,%d,%d\n",a,b,c,ch);
     return 0;
}
 
Operation results:
10,75,10,A


 2.4. 2. Integer variable

Integer data keyword, number of bytes and value range

Example 7: definition and use examples of different types of integer variables

#include<stdio.h>
int main()
{    int a,b,c,d;
     unsigned long e;
     a=5;b=6;e=10;
     c=a+e;
     d=b+e;
     printf("c=%d,d=%d\n",c,d);
     return 0;
}
 
Operation results:
c=15,d=16


Example 8: overflow of short integer variables

#include<stdio.h>
int main()
{    short int a,b;
     a=327667;
     b=a+1;
     printf("a=%d,b=%d\n",a,b);
     return 0;
}
 
Operation results:
a=32767,b=-32768


 2.4. 3. Real variables

Real data keyword, number of bytes and value range

Example 9: use examples of real variables

#include<stoid.h>
int main()
{    float a=1234.56789;
     double b=1234.567895678;
     printf("a=%f,b=%f\n",a,b);
     return 0;
}
 
Operation results:
a=1234.567871,b=1234.567896


The number of significant digits of single precision floating-point type is 7, and the maximum number of digits after double precision decimal is 6

2.4. 4. Character variable

Example 10: use example of character variable

#include<stdio.h>
int main()
{    char c1,c2;
     c1='A';
     c2=65;
     printf("%c,%c\n",c1,c2);
     printf("%d,%d\n",c1,c2);
     return 0;
}
 
Operation results:
A,A
65,65


Example 11: transpose capital letters into lowercase letters

#iinclude<sttdio.h>
int main()
{    char c1,c2;
     c1='A';
     c2='B';
     C1=C1+32;
     C2=C2+32;
     printf("%c,%c\n",c1,c2);
     printf("%d,%d\n",c1,c2);
     return 0;
}
 
Operation results:
a,b
97,98


2.5 operators and expressions

2.5. 1. Assignment operator and assignment expression

Compound arithmetic assignment operator

2.5. 2. Arithmetic operators and arithmetic expressions

1. Basic arithmetic operators

Binocular operators: addition, subtraction, multiplication, division, and remainder

%: it is used to calculate the remainder. It can only be used for values between integers

2. Self increasing and self decreasing operators

++Self increasing operator is a monocular operator whose function is to increase the value of a variable by 1

Prefix method: used in front of variables, such as "int" a=++x; " It is' calculate first and then use '

Suffix method: used after variables, such as "int" a=x + +; " It is "use first, then calculate"

The minus sign is the opposite

Example 12: setting is defined as follows:

#define d 2
int x=5;
float y=3.83;
char c='d';

3. Precedence and associativity of arithmetic operators·

Monocular operators have higher priority than binocular operators, and *, /,% in binocular arithmetic symbols have higher priority than +, -. When the priority is the same, it shall be handled according to the specified "combination"

4. Arithmetic expression

Example 13: input a 3-bit positive integer and output its inverse number

#include<stdio.h>
int main()
{    int number,a,b,c;
     number=123;
     printf("The 3 digits entered are:%d\n",number);
     a=number/100;
     b=(number-a*100)/10;
     c=number%10
     printf("The inverse number is:%d%d%d\n",c,b,a);
     return 0;
}
 
Operation results:
The 3 digits entered are: 123
 Inverse number: 321


2.5. 3. Comma operator and comma expression

Example 14: application example of comma expression

#include<stdio.h>
int main()
{    int i,j=7;
     float k=5,
     i=(j=j+2,j/k);
     printf("i=%d\n",i);
     return 0;
}
 
Operation results:
i=1


 2.5. 4. Byte count operator

Example 15: number of bytes of different data types

#include<studio.h>
int main()
{    printf("char Type accounting%d byte\n",sizeof(char));
     printf("short int Type accounting%d byte\n",sizeof(short int));
     printf("int Type accounting%d byte\n",sizeof(int));
     printf("float Type accounting%d byte\n",sizeof(float));
     printf("double Type accounting%n",sizeof(double));
     return 0; 
}
 
Operation results:
char Type occupies 1 byte
short int Type takes up 2 bytes
int Type takes up 4 bytes
float Type takes up 4 bytes
double Type takes up 8 bytes


Example 16: example of byte count operator

#include<stdio.h>
int main()
{    int a,b;
     a=sizeof(3+5.0);
     b=sizeof 3+5.0;
     printf("%d%d%d\\",a,b,sizeof("china"));
     return 0;
}
 
 
Operation results:
8,9,6


2.6 data type conversion

2.6. 1. Automatic type conversion

The rule for self conversion is to convert low-level types that occupy memory space to high-level types that occupy more space

Note: no high to low [avoid data loss]

2.6. 2. Cast type

Type specifier function: cast the operation result of the expression to the type specified by the type specifier

Example 18: cast example

#include<stdio.h>
int main()
{    int a=3,b=4;
     float c;
     c=(float)(a+b)/2;
     printf("c=%f\n",c);
     return 0;
}
 
Operation results:
c=3.500000

Topics: C Network Protocol