First knowledge of C language

Posted by Ludo Lambrechts on Fri, 11 Feb 2022 20:08:38 +0100

catalogue

Constant and Variable

variable

Location of variables

Scope

life cycle

constant

① Literal constant

② const modified constant

③ #define defined identifier constant

④ Enumeration constant

String type

ASCII code table

Escape character

Constant and Variable

variable

Variables can be divided into local variables and global variables, which are introduced by the following program.

Location of variables

#include <stdio.h>
int num1 = 10;//Global variable, defined outside the code block ({})
int main()
{
  int num2 = 20;//Local variables, defined within the code block ({})
  return 0;
}

Note: when the names of local variables and global variables are the same, local variables take precedence.

Scope

The scope of the local variable is the local scope of the variable, and the scope of the global variable is the whole project.

life cycle

The life cycle of local variables is: enter the scope and the life cycle begins; Out of scope lifecycle end (destroy). The life cycle of global variables is: the life cycle of the whole program.

constant

① Literal constant

Written directly

② const modified constant

#include <stdio.h>
int main()
{
  const int num = 4;
  printf("%d\n",num);
  num = 8;
  printf("%d\n",num);
  return 0;
}
//The program failed to run because once the variable num is modified by const, it has a constant attribute and its value will not change

In the above program, although num is modified by const and has constant attribute, it is still a variable in essence.

For example, the following way of defining an array is wrong.

const int n = 10;
int arr[n] = {0};

③ #define defined identifier constant

# include <stdio.h>
# define MAX 10 / / all MAX below represent 10 and are called identifier constants
int main()
{
  int arr[MAX] = {0};
  printf("%d\n",MAX);
  return 0;
}

④ Enumeration constant

The definition form of enumeration type is:

enum typeName{ valueName1, valueName2, valueName3, ...... };

enum is a new keyword, which is specially used to define enumeration types, which is its only use in C language. typeName is the name of the enumeration type. valueName1, valueName2, valueName3, ...... Is a list of names corresponding to each value. Pay attention to the last; No less.

enum week{Mon,Tues,Wed,Thurs,Fri,Sat,Sun};

We only give the name, but we don't give the value corresponding to the name. This is because the enumeration value starts from 0 by default, and then increases by 1 (increment) one by one; In other words, Mon and Tues in week The corresponding values of sun are 0, 1 6. We can also assign a value to each name. A simpler way is to assign a value to only the first name.

enum week{Mon = 1,Tues,Wed,Thurs,Fri,Sat,Sun};

In this way, the enumeration value is incremented from 1.

String type

A string of characters caused by double quotation marks. Note: the sign at the end of the string is the escape character \ 0, which is not included in the string length calculation.

//Print string
# include <stdio.h>
# include <string.h>
int main()
{
  char arr1[] = "abc";//"abc":'a','b','c','0'
  char arr2[] = {'a','b','c'};
  printf("%s\n",arr1);//Print correctly
  printf("%d\n",strlen(arr1));//Print the length of string arr1, which is 3
  printf("%s\n",arr2);//Printing error, 'a','b','c' is a single character, and another '\ 0' should be added
  printf("%d\n",strlen(arr2));//Print the length of string arr2, which is a random value until \ 0 is encountered
  return 0;
}

ASCII code table

Data is stored in a computer in binary form.

For example, the decimal system of character a is 97, the decimal system of character a is 165, and the decimal system of character \ 0 is 0

Escape character

As the name suggests, change the original meaning, such as \ 0 is no longer 0, but as the end sign of the string; \N is not n, but a new line.

\?Use when writing consecutive question marks to prevent them from being interpreted as three letter words
\''means'
\"Express“
\\Represents a single backslash\
\nLine feed
\tHorizontal tab
\dddddd represents 1-3 octal digits
\xdddd represents two hexadecimal digits
//Print path C: \ test \ 32 \ test c
printf("c:\\test\\32\\test.c");
//Print character a
printf("%s\n",'a');
//'print character'
printf("%s\n",'\'');
//Print string a
printf("%s\n","a");
//Print string“
printf("%s\n","\"");
calculation c:\test\32\test.c Length of
printf("%d\n",strlen("c:\test\32\test.c"));//The result is 13
//\32 represents the character represented by the decimal number corresponding to 32 (8) as the ASCII code value
//32 (8) = 26 (10), so \ 32 means - >

Topics: C