Basic knowledge points of C language

Size of types in C language

#include<stdio.h>
int main()
{
	int a = 10;                           //give the result as follows
	printf("%d\n",sizeof(char));          //1 byte, - 128, + 127, 0-255
	printf("%d\n", sizeof(short));        //2 bytes, - 32768, + 32767 0-65535
	printf("%d\n", sizeof(int));          //4 bytes, - 2.1 billion, + 2.1 billion 0-4.29 billion
	printf("%d\n", sizeof(long));         //4 bytes
	printf("%d\n", sizeof(long long));    //8 bytes
	printf("%d\n", sizeof(float));        //4 bytes
	printf("%d\n", sizeof(double));       //8 bytes
	printf("%d\n", sizeof(long double));  //8 bytes
	system("pause");
	return 0;
}

constant

Definition: also known as literal quantity, it is a fixed value and will not change during program execution. The value of a constant cannot be modified after it is defined.

  1. Literal constant
  2. const keyword modified constant
  3. #Identifier constant defined by define
  4. enumeration constant
#include <stdio.h>
//Examples
//Create constant

//Macro definition
#define MAX 100             //#Identifier constant for define
//enumeration
enum Sex  //Listed
{
MALE,
FEMALE,
SECRET
};
//Mal, female and secret in parentheses are enumeration constants
int main()
{  
  //1. Literal constant
  3.14;
  1000;
  //10 = 20; // An error is reported and cannot be assigned
  //2. Use const keyword to represent
  const float pai = 3.14f;  //const modified constant
  //3. Enumeration
  //4. Macro
  printf("%d",MAX);
  return 0;
}

variable

A variable is the name of the storage area that the program can operate on. Each variable in C has a specific type, which determines the size and layout of variable storage. The values within this range can be stored in memory, and operators can be applied to variables.
The name of a variable can consist of letters, numbers, and underscore characters. It must start with a letter or underscore. Uppercase and lowercase letters are different because C is case sensitive.

Common ways to name multiple words:

  1. Hump nomenclature:
    studentCount, between words, the first letter is capitalized, and the first letter of the first word can be capitalized or lowercase
  2. Snake nomenclature:
    student_count, between words, separated by underscores
  3. Spine nomenclature:
    Student count. This method c is not supported, but it is common in other languages, such as CSS

Global and local variables

First, we need to understand two terms: scope and lifecycle
scope of variable: the code in which the variable can be used (the available range of the variable).
Variable life cycle: the variable life cycle refers to the time period from the creation of a variable to the destruction of a variable.
For local variables:
Scope: current code block
Lifecycle: current code block
For global variables:
Scope: current entire project
Life cycle: follow the whole program

Common keywords

auto break case char const continue default do double else enum
extern float for goto if int long register return short signed
sizeof static struct switch typedef union unsigned void volatile while

typedef

typedef, as the name suggests, is a type definition, which should be understood here as type renaming.

//Rename unsigned int to uint, so uint is also a type name
typedef unsigned int uint;
int main()
{
  //Look at num1 and num2. These two variables are of the same type
  unsigned int num1 = 0;
  uint num2 = 0;
  printf("num1 = %d num2 = %d",num1,num2);
  return 0;
}

*static

Detailed usage of static keyword

character string

Common string:
Note: the end flag of the string is an escape character of \ 0. When calculating the string length, \ 0 is the end flag and is not counted as the string content.

  1. strcpy(s1, s2);
    Copy string s2 to string s1.
  2. strcat(s1, s2);
    Connect string s2 to the end of string s1.
  3. strlen(s1);
    Returns the length of the string s1.
  4. strcmp(s1, s2);
    If s1 and s2 are the same, 0 is returned; If s1 < s2, it returns less than 0; If s1 > s2, it returns greater than 0.
  5. strchr(s1, ch);
    Returns a pointer to the first occurrence of the character ch in string s1.
  6. strstr(s1, s2);
    Returns a pointer to the first occurrence of string s2 in string s1.

Operator

1. arithmetic operator
    +Plus,-Minus,*By,/Except,%Surplus
2.  Shift operators 
    Shift right >> ,Shift left <<
3.  Bitwise operator (binary)
	&: Bitwise AND
	|: Bitwise OR
	^:
4.  unary operator 
	!      Logical reverse operation
	-      negative
	+      positive
	&      Get address
	sizeof    The type length of the operand in bytes
	~         Bitwise negation of a number
	--        Front and rear--
	++        Front and rear++
	*         Indirect access operator(dereference operator )
	(type)    Cast type
5. Relational operator
	>
	>=
	<
	<=
	!=    Used to test "inequality"
	==    Used to test equality
6. Logical operator
	&&    Logic and
	||    Logical or
7. Conditional Operator 
	exp1 ? exp2 : exp3
8. comma expression 
	exp1, exp2, exp3, ...expN

Tags: C

Posted on Fri, 05 Nov 2021 13:55:40 -0400 by billy2shoe