Variables and data types in C language


The data processed by the computer (such as numbers, characters, symbols, graphics, audio, video, etc.) are stored in the memory in binary form; a component is 1Bit, which
has two results of 0 and 1; 8Bit is 1Byte, which is A byte; a byte is the smallest operable unit.

Variable

  • The computer first finds an area in the memory, stipulates that it is used to store integers, and gives it an easy-to-remember name to facilitate future search. Then we can put integers into this area.
  • Assignment refers to the process of putting data into memory.
#include <stdio.h>
int main()
{
    
    
  /* 定义变量, 初始化, 再次赋值 */
  int a;
  a = 1;
  printf("%d\n", a);
  a = 2;
  printf("%d\n", a);
  a = 3;
  printf("%d\n", a);

  /* 定义变量并初始化 */
  int b = 123;
  printf("%d\n", b);

  /* 连续定义多个变量 */
  int c, d, e, f = 456, g = 789;
  printf("%d\n", f);
  printf("%d\n", g);

  return 0;
}
  • Data is stored in memory, and three things need to be clarified when accessing data in memory: where the data is stored, the length of the data, and how the data is processed.
  • The data type is only specified when defining the variable, and it must be specified; it is not necessary to specify when using the variable, because the data type has been determined at this time.

type of data

  • In the C language, the number of bytes occupied by each data type is fixed. Knowing the data type (specifying the interpretation method of the data), you also know the length of the data.
  • The data type is only specified when defining the variable, and it must be specified; it is not necessary to specify when using the variable, because the data type has been determined at this time.
  • puts (output string) can only output strings,
  • printf (print format "formatted printing") is stronger than puts. It can output strings, integers, decimals, single characters, etc., and you can define the output format yourself, for example: output in decimal, octal, and hexadecimal
    ;
    require The output number occupies n character positions;
    controls the number of decimal places.

1. Integer (printf output)

%u 表示按unsigned int格式输入或输出数据
%hd用来输出 short int 类型,hd 是 short decimal 的简写;
%d用来输出 int 类型,d 是 decimal 的简写;
%ld用来输出 long int 类型,ld 是 long decimal 的简写。

Number of bytes: 2 ≤ short ≤ int ≤ long

#include <stdio.h>
int main()
{
    
    
  /*
    short === short int
    至少占2个字节(2Byte == 16Bit)
    Bit只有两种状态: 0或1
   */
  short a = 0;

  /*
    int大于等于short
   */
  int b = 0;

  /*
    long 大于等于int
   */
  long c = 0;
  
  /*
    short, int, long的长度关系:2 ≤ short ≤ int ≤ long
    short 并不一定真的”短“,long 也并不一定真的”长“,它们有可能和 int 占用相同的字节数。
    short至少占2个字节.
    short 的长度不能大于 int,long 的长度不能小于 int。
  */
  unsigned int d = 54321;
  short e = 12345;
  printf("%u and %hd\n", d, e);

  return 0;
}

2. sizeof gets the number of occupied bytes

sizeof 变量名
sizeof(数据类型)

sizeof is used to get the number of bytes occupied by a data type or variable

Two, eight, hexadecimal

#include <stdio.h>
int main()
{
    
    
  /* 二进制: 以0b或0B开头, 只含0和1 */
  int a = 0b1;
  int b = 0b10;
  int c = 0B11;
  int d = 0B0100;
  printf("二进制\n");
  printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

  /* 八进制: 以数字0开头, 只含0到7 */
  a = 01;
  b = 010;
  c = 0100;
  d = 001000;
  printf("八进制\n");
  printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

  /* 十六进制: 以0x或0X开头, 只含0到9再到f或0到F */
  a = 0xf;
  b = 0xf0;
  c = 0XF00;
  d = 0X0F000;
  printf("十六进制\n");
  printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);

  /* 进制的输出 */
  short e = 0b10;
  int f = 010;
  long g = 0x10;
  printf("e=%ho\n", e);
  printf("f=%o\n", f);
  printf("g=%lx\n", g);
  printf("g=%#lx\n", g); // ***

  return 0;
}

insert image description here

printf formatted output (including unsigned output format)

There are three types of commonly used integers: short, int, and long, and the corresponding output formats are as follows:
insert image description here

Sign bit (positive and negative) in C language

  • The C language stipulates that the highest bit of the memory is used as the sign bit.
  • The C language stipulates that in the sign bit, 0 is used to represent a positive number, and 1 is used to represent a negative number.
    insert image description here
  • The short, int, and long types are all signed bits by default, and the memory other than the sign bit is the value bit
  • If you don't want to set the sign bit, you can add the unsigned keyword in front of the data type
#include <stdio.h>
int main()
{
    
    
  int i = -1;
  printf("i = %d\n", i);
  // 无符号输出
  printf("i = %u\n", i); //符号位在第一位,负是1,正是0

  unsigned int j = -1;
  printf("j = %d\n", j);
  printf("j = %u\n", j); //符号位在第一位,负是1,正是0

  return 0;
}

insert image description here

addition of integers in memory

Addition and subtraction can also be combined into one operation, that is, addition operation, because subtracting a number is equivalent to adding the opposite number of this number. (5 - 3 is equivalent to 5 + (-3), 10 - (-9) Equivalent to 10+9.)
Signed numbers must be converted when storing and reading

  • The original code (the binary form of the integer), the inverse code and the complement code of the positive number are the same

  • The inverse code of a negative number is the inversion of the original code, and the complement code of a negative number is the inverse code of a negative number plus one

  • Calculation method in memory: complement code addition, the original code of the result is a specific value.

  • Computer memory values ​​are stored in complement code .


Characters and strings

#include <stdio.h>
int main(int argc, char **argv)
{
    
    
  // 字符
  char a = 'a';
  putchar(a);
  putchar('\n');
  printf("%c %d\n", a, a);

  // 字符串
  char str1[] = "I'm a string1. ";
  char *str2 = "I'm a string2. ";
  puts(str1);
  printf("%s\n", str2);

  return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_46372074/article/details/126658772