Introduction to C language Chapter 1 (three in total)

Learning objectives:

**Master the basic knowledge of C language in one month** #Learning content: Basic syntax of C language, such as: 1. Setting of C language environment; 2. Mastering computer hexadecimal conversion and sorting algorithm. 3. Master data types, variables and constants, storage classes, operators and judgment. 4. Loop, function, scope rule, array, enum (enumeration), pointer and function pointer and callback function. 5. String, structure, common body, bit field, typeder, input & output, file reading and writing. 6. Preprocessing, header file, forced type conversion, error handling, recursion, variable parameters, memory management. Command line parameters,

Study time:

1. Monday to Friday 7:00 pm - 9:00 PM 2, Saturday 9:00 am - 11:00 am 3, Sunday 3:00 pm - 6:00 pm

Learning output:

For example: 1. Two C language cases; 2. Three basic notes of C language; 3. Video Explanation of C language learning; 4. Summarize at all times and type more codes

Learn to install C compiler

1. Installation on UNIX / Linux if you are using Linux or UNIX, please use the following command on the command line to check whether GCC is installed on your system:
$ gcc -v

If the GNU compiler is already installed on your computer, the following message is displayed:

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr .......
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

If GCC is not installed, follow http://gcc.gnu.org/install/ Install GCC as detailed on.

This tutorial is written based on Linux, and all given instances have been compiled on CentOS Linux system.

2.. Installation on Mac OS
If you are using Mac OS X, the quickest way to get GCC is to download the Xcode development environment from Apple's website and install it according to the installation instructions. Once Xcode is installed, you can use the GNU compiler.
Xcode is currently available from Download
Dev-C++ compiler
Installation on Windows
mingw-w64

Basic knowledge of C language
C constant
Constants are fixed values and do not change during program execution. These fixed values are also called literal values.

Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string literals, as well as enumeration constants.

Constants are like regular variables, but their values cannot be modified after definition.

integer constant
Integer constants can be decimal, octal, or hexadecimal constants. Prefix specifies the base: 0X or 0X indicates hexadecimal, 0 indicates octal, and without prefix, it indicates decimal by default.

Integer constants can also have a suffix. The suffix is a combination of U and L. U represents unsigned integer and l represents long integer. The suffix can be uppercase or lowercase, and the order of U and l is arbitrary.

Here are some examples of integer constants:

212         /* lawful */
215u        /* lawful */
0xFeeL      /* lawful */
078         /* Illegal: 8 is not an octal number */
032UU       /* Illegal: cannot duplicate suffix */

The following are examples of various types of integer constants:

85         /* decimal system */
0213       /* octal number system */
0x4b       /* hexadecimal */
30         /* integer */
30u        /* Unsigned integer */
30l        /* long integer */
30ul       /* Unsigned long integer */

Floating-Point Literals
Floating point constants are composed of integer part, decimal point, decimal part and exponential part. You can use decimal or exponential forms to represent floating-point constants.

When expressed in decimal form, it must include integer part, decimal part, or both. When expressed in the form of index, it must include decimal point, index, or both. The signed exponent is introduced by E or E.

Here are some examples of floating-point constants:

3.14159       /* lawful */
314159E-5L    /* lawful */
510E          /* Illegal: incomplete index */
210f          /* Illegal: no decimals or exponents */
.e55          /* Illegal: missing integer or fraction */

character constants
Character constants are enclosed in single quotes. For example, 'x' can be stored in a simple variable of type char.

A character constant can be an ordinary character (such as' x '), an escape sequence (such as' \ t'), or a general character (such as' \ u02C0 ').

In C, there are some specific characters. When they are preceded by backslashes, they have special meanings and are used to represent, such as line feed (\ n) or tab (\ t). The following table lists some such escape sequence codes:

Escape sequencemeaning
\\\Character
\''character
\""Character
\?? character
\aAlarm bell
\bBackspace key
\fPage feed
\nLine feed
\renter
\tHorizontal tab
\vvertical tab
\oooOne to three digit octal number
\xhh...One or more hexadecimal numbers

The following example shows some escape sequence characters:

example

#include <stdio.h>
 
int main()
{
   printf("Hello\tWorld\n\n");
 
   return 0;
}
When the above code is compiled and executed, it will produce the following results:

Hello   World

string constant
String literals or constants are enclosed in double quotes' '. A string contains characters similar to character constants: ordinary characters, escape sequences, and common characters.

You can use spaces as separators to break a long string constant.

The following example shows some string constants. The strings displayed in the following three forms are the same.

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Define constants
In C, there are two simple ways to define constants:

Use #define preprocessor.
Use the const keyword.
#define preprocessor
The following is the form of defining constants using #define preprocessor:

#define identifier value
See the following examples for details:

example

#include <stdio.h>
 
#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'
 
int main()
{
 
   int area;  
  
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

value of area : 50
const keyword
You can declare constants of the specified type using const prefix, as follows:

const type variable = value;

const declaration constants should be completed in one statement:

See the following examples for details:

example

#include <stdio.h>
 
int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

value of area : 50
Note that it is a good programming habit to define constants in uppercase letters.
C storage class
Storage classes define the scope (visibility) and life cycle of variables / functions in C programs. These specifiers are placed before the type they modify. The storage classes available in the C program are listed below:

auto
register
static
extern
auto Storage class
auto Storage class is the default storage class for all local variables.

{
   int mount;
   auto int month;
}

The above example defines two variables with the same storage class. Auto can only be used in functions, that is, auto can only modify local variables.

register storage class
The register storage class is used to define local variables stored in registers rather than RAM. This means that the maximum size of the variable is equal to the size of the register (usually a word), and the unary '&' operator cannot be applied to it (because it has no memory location).

{
   register int  miles;
}

Registers are only used for variables that require fast access, such as counters. It should also be noted that defining 'register' does not mean that variables will be stored in registers, it means that variables may be stored in registers, depending on hardware and implementation limitations.

static storage class
The static storage class instructs the compiler to maintain the existence of local variables throughout the life cycle of the program without creating and destroying them every time it enters and leaves the scope. Therefore, using static to modify local variables can maintain the value of local variables between function calls.

The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of the variable to the file in which it is declared.

A globally declared static variable or method can be called by any function or method as long as these methods appear in the same file as the static variable or method.

The following example demonstrates the application of static to modify global and local variables:

example

#include <stdio.h>
 
/* Function declaration */
void func1(void);
 
static int count=10;        /* Global variable - static is the default */
 
int main()
{
  while (count--) {
      func1();
  }
  return 0;
}
 
void func1(void)
{
/* 'thingy' Is a local variable of 'func1' - initialized only once
 * The 'thingy' value of 'func1' will not be reset every time the function is called.
 */                
  static int thingy=5;
  thingy++;
  printf(" thingy by %d , count by %d\n", thingy, count);
}

In the instance, count can be used as a global variable in the function. After thingy is decorated with static, it will not be reset every time it is called.

You may not understand this example yet, because I have used functions and global variables, which have not been explained so far. Even if you can't fully understand it now, it doesn't matter. We will explain it in detail in the subsequent chapters. When the above code is compiled and executed, it will produce the following results:

thingy Is 6, count For 9
 thingy Is 7, count For 8
 thingy 8, count For 7
 thingy 9, count For 6
 thingy 10, count For 5
 thingy Is 11, count For 4
 thingy Is 12, count For 3
 thingy Is 13, count For 2
 thingy 14, count Is 1
 thingy 15, count Is 0

extern storage class
The extern storage class is used to provide a reference to a global variable, which is visible to all program files. When you use extern, for variables that cannot be initialized, the variable name will point to a previously defined storage location.

When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get the reference of the defined variable or function. It can be understood that extern is used to declare a global variable or function in another file.

The extern modifier is usually used when two or more files share the same global variable or function, as follows:

First file: main c

example

#include <stdio.h>
 
int count ;
extern void write_extern();
 
int main()
{
   count = 5;
   write_extern();
}
Second file: support.c

example

#include <stdio.h>
 
extern int count;
 
void write_extern(void)
{
   printf("count is %d\n", count);
}

Here, the extern keyword in the second file is used to declare that it has been in the first file main count defined in C. Now compile the two files as follows:

$ gcc main.c support.c
This produces an a.out executable program, which produces the following results when the program is executed:

count is 5

C operator
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C language has built-in rich operators and provides the following types of operators:

Arithmetic operator
Relational operator
Logical operator
Bitwise Operators
Assignment Operators
Miscellaneous operator
This chapter will introduce arithmetic operators, relational operators, logical operators, bit operators, assignment operators and other operators one by one.

Arithmetic operator
The following table shows all arithmetic operators supported by the C language. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

operatordescribe
+Add the two operands
-Subtracts the second operand from the first operand
*Multiply two operands
/Numerator divided by denominator
%Take the module operator and divide the remainder
++Self increasing operator, the integer value is increased by 1
- -Self subtraction operator, the integer value is reduced by 1

example
Please take a look at the following examples to understand all the arithmetic operators available in C language:

example

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c ;
 
   c = a + b;
   printf("Line 1 - c The value of is %d\n", c );
   c = a - b;
   printf("Line 2 - c The value of is %d\n", c );
   c = a * b;
   printf("Line 3 - c The value of is %d\n", c );
   c = a / b;
   printf("Line 4 - c The value of is %d\n", c );
   c = a % b;
   printf("Line 5 - c The value of is %d\n", c );
   c = a++;  // Add 1 after assignment, c is 21 and a is 22
   printf("Line 6 - c The value of is %d\n", c );
   c = a--;  // Minus 1 after assignment, c is 22 and a is 21
   printf("Line 7 - c The value of is %d\n", c );
 
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - c The value of is 31
Line 2 - c The value of is 11
Line 3 - c The value of is 210
Line 4 - c The value of is 2
Line 5 - c The value of is 1
Line 6 - c The value of is 21
Line 7 - c The value of is 22

The following example demonstrates the difference between a + + and + + A:

example

#include <stdio.h>
 
int main()
{
   int c;
   int a = 10;
   c = a++; 
   printf("Assignment before operation:\n");
   printf("Line 1 - c The value of is %d\n", c );
   printf("Line 2 - a The value of is %d\n", a );
   a = 10;
   c = a--; 
   printf("Line 3 - c The value of is %d\n", c );
   printf("Line 4 - a The value of is %d\n", a );
 
   printf("Operation before assignment:\n");
   a = 10;
   c = ++a; 
   printf("Line 5 - c The value of is %d\n", c );
   printf("Line 6 - a The value of is %d\n", a );
   a = 10;
   c = --a; 
   printf("Line 7 - c The value of is %d\n", c );
   printf("Line 8 - a The value of is %d\n", a );
 
}

The output result of the above program execution is:

Assignment before operation:
Line 1 - c The value of is 10
Line 2 - a The value of is 11
Line 3 - c The value of is 10
Line 4 - a The value of is 9
 Operation before assignment:
Line 5 - c The value of is 11
Line 6 - a The value of is 11
Line 7 - c The value of is 9
Line 8 - a The value of is 9

Relational operator
The following table shows all the relational operators supported by the C language. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

Operator description instance
==Checks whether the values of the two operands are equal. If they are equal, the condition is true. (A == B) is false.
!= Checks whether the values of the two operands are equal. If they are not equal, the condition is true. (a! = b) is true.

Check whether the value of the left operand is greater than the value of the right operand. If so, the condition is true. (a > b) is false.
< check whether the value of the left operand is less than the value of the right operand. If so, the condition is true. (a < b) is true.
=Check whether the value of the left operand is greater than or equal to the value of the right operand. If so, the condition is true. (a > = b) is false.
< = check whether the value of the left operand is less than or equal to the value of the right operand. If so, the condition is true. (a < = b) is true.
example
Take a look at the following examples to understand all the relational operators available in C language:

example

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c ;
 
   if( a == b )
   {
      printf("Line 1 - a be equal to b\n" );
   }
   else
   {
      printf("Line 1 - a Not equal to b\n" );
   }
   if ( a < b )
   {
      printf("Line 2 - a less than b\n" );
   }
   else
   {
      printf("Line 2 - a Not less than b\n" );
   }
   if ( a > b )
   {
      printf("Line 3 - a greater than b\n" );
   }
   else
   {
      printf("Line 3 - a Not greater than b\n" );
   }
   /* Change the values of a and b */
   a = 5;
   b = 20;
   if ( a <= b )
   {
      printf("Line 4 - a Less than or equal to b\n" );
   }
   if ( b >= a )
   {
      printf("Line 5 - b Greater than or equal to a\n" );
   }
}

When the above code is compiled and executed, it will produce the following results:

```c
Line 1 - a Not equal to b
Line 2 - a Not less than b
Line 3 - a greater than b
Line 4 - a Less than or equal to b
Line 5 - b Greater than or equal to a

**```

Relational operator**
The following table shows all the relational operators supported by the C language. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

operatordescribe
==Check whether the values of the two operands are equal. If they are equal, the condition is true (A == B) and false.
!=Checks whether the values of the two operands are equal. If they are not equal, the condition is true. (a! = b) is true.
>Check whether the value of the left operand is greater than the value of the right operand. If so, the condition is true. (a > b) is false.
>=Check whether the value of the left operand is greater than or equal to the value of the right operand. If so, the condition is true. (a > = b) is false.
<=Check whether the value of the left operand is less than or equal to the value of the right operand. If so, the condition is true. (a < = b) true

example
Take a look at the following examples to understand all the relational operators available in C language:

#include <stdio.h>
 
int main()
{
   int a = 21;
   int b = 10;
   int c ;
 
   if( a == b )
   {
      printf("Line 1 - a be equal to b\n" );
   }
   else
   {
      printf("Line 1 - a Not equal to b\n" );
   }
   if ( a < b )
   {
      printf("Line 2 - a less than b\n" );
   }
   else
   {
      printf("Line 2 - a Not less than b\n" );
   }
   if ( a > b )
   {
      printf("Line 3 - a greater than b\n" );
   }
   else
   {
      printf("Line 3 - a Not greater than b\n" );
   }
   /* Change the values of a and b */
   a = 5;
   b = 20;
   if ( a <= b )
   {
      printf("Line 4 - a Less than or equal to b\n" );
   }
   if ( b >= a )
   {
      printf("Line 5 - b Greater than or equal to a\n" );
   }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - a Not equal to b
Line 2 - a Not less than b
Line 3 - a greater than b
Line 4 - a Less than or equal to b
Line 5 - b Greater than or equal to a

Logical operator
The following table shows all the relational logic operators supported by C language. Assuming that the value of variable A is 1 and the value of variable B is 0, then:

operatordescribe
&&They are called logical and operators. If both operands are non-zero, the condition is true. (A & & B) is false.
\\It is called a logical or operator. If either of the two operands is non-zero, the condition is true. (A
!It is called a logical non operator. Used to reverse the logical state of an operand. If the condition is true, the logical non operator will make it false! (A & & B) is true.

example
Please take a look at the following examples to understand all the logical operators available in C language:

#include <stdio.h>
 
int main()
{
   int a = 5;
   int b = 20;
   int c ;
 
   if ( a && b )
   {
      printf("Line 1 - Condition is true\n" );
   }
   if ( a || b )
   {
      printf("Line 2 - Condition is true\n" );
   }
   /* Change the values of a and b */
   a = 0;
   b = 10;
   if ( a && b )
   {
      printf("Line 3 - Condition is true\n" );
   }
   else
   {
      printf("Line 3 - Condition is false\n" );
   }
   if ( !(a && b) )
   {
      printf("Line 4 - Condition is true\n" );
   }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is false
Line 4 - Condition is true

Bitwise Operators
Bit operators act on bits and perform operations bit by bit| The truth table of and ^ is as follows:

Suppose that if A = 60 and B = 13 are now represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table shows the bitwise operators supported by the C language. Assuming that the value of variable A is 60 and the value of variable B is 13, then:


example
Take a look at the following examples to understand all the bitwise operators available in C language:

example

#include <stdio.h>
 
int main()
{
 
   unsigned int a = 60;    /* 60 = 0011 1100 */  
   unsigned int b = 13;    /* 13 = 0000 1101 */
   int c = 0;           
 
   c = a & b;       /* 12 = 0000 1100 */ 
   printf("Line 1 - c The value of is %d\n", c );
 
   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - c The value of is %d\n", c );
 
   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - c The value of is %d\n", c );
 
   c = ~a;          /*-61 = 1100 0011 */
   printf("Line 4 - c The value of is %d\n", c );
 
   c = a << 2;     /* 240 = 1111 0000 */
   printf("Line 5 - c The value of is %d\n", c );
 
   c = a >> 2;     /* 15 = 0000 1111 */
   printf("Line 6 - c The value of is %d\n", c );
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - c The value of is 12
Line 2 - c The value of is 61
Line 3 - c The value of is 49
Line 4 - c The value of is -61
Line 5 - c The value of is 240
Line 6 - c The value of is 15

Assignment Operators
The following table lists the assignment operators supported by C language:

example
Please refer to the following examples to understand all available assignment operators in C language:

example

#include <stdio.h>
 
int main()
{
   int a = 21;
   int c ;
 
   c =  a;
   printf("Line 1 - =  Operator instance, c Value of = %d\n", c );
 
   c +=  a;
   printf("Line 2 - += Operator instance, c Value of = %d\n", c );
 
   c -=  a;
   printf("Line 3 - -= Operator instance, c Value of = %d\n", c );
 
   c *=  a;
   printf("Line 4 - *= Operator instance, c Value of = %d\n", c );
 
   c /=  a;
   printf("Line 5 - /= Operator instance, c Value of = %d\n", c );
 
   c  = 200;
   c %=  a;
   printf("Line 6 - %%= Operator instance, c Value of = %d\n", c );
 
   c <<=  2;
   printf("Line 7 - <<= Operator instance, c Value of = %d\n", c );
 
   c >>=  2;
   printf("Line 8 - >>= Operator instance, c Value of = %d\n", c );
 
   c &=  2;
   printf("Line 9 - &= Operator instance, c Value of = %d\n", c );
 
   c ^=  2;
   printf("Line 10 - ^= Operator instance, c Value of = %d\n", c );
 
   c |=  2;
   printf("Line 11 - |= Operator instance, c Value of = %d\n", c );
 
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - =  Operator instance, c Value of = 21
Line 2 - += Operator instance, c Value of = 42
Line 3 - -= Operator instance, c Value of = 21
Line 4 - *= Operator instance, c Value of = 441
Line 5 - /= Operator instance, c Value of = 21
Line 6 - %= Operator instance, c Value of = 11
Line 7 - <<= Operator instance, c Value of = 44
Line 8 - >>= Operator instance, c Value of = 11
Line 9 - &= Operator instance, c Value of = 2
Line 10 - ^= Operator instance, c Value of = 0
Line 11 - |= Operator instance, c Value of = 2

Miscellaneous operator ↦ sizeof & ternary
The following table lists some other important operators supported by C language, including sizeof and?:

example
Take a look at the following examples to understand all the miscellaneous operators available in C language:

example

#include <stdio.h>
 
int main()
{
   int a = 4;
   short b;
   double c;
   int* ptr;
 
   /* sizeof Operator instance */
   printf("Line 1 - variable a Size of = %lu\n", sizeof(a) );
   printf("Line 2 - variable b Size of = %lu\n", sizeof(b) );
   printf("Line 3 - variable c Size of = %lu\n", sizeof(c) );
 
   /* & And * operator instances */
   ptr = &a;    /* 'ptr' The address that now contains' a ' */
   printf("a The value of is %d\n", a);
   printf("*ptr yes %d\n", *ptr);
 
   /* Ternary operator instance */
   a = 10;
   b = (a == 1) ? 20: 30;
   printf( "b The value of is %d\n", b );
 
   b = (a == 10) ? 20: 30;
   printf( "b The value of is %d\n", b );
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - variable a Size of = 4
Line 2 - variable b Size of = 2
Line 3 - variable c Size of = 8
a The value of is 4
*ptr It's 4
b The value of is 30
b The value of is 20

Operator priority in C
The priority of the operator determines the combination of items in the expression. This affects how an expression is evaluated. Some operators have higher priority than others. For example, multiplication and division operators have higher priority than addition and subtraction operators.

For example, x = 7 + 3 * 2. Here, X is assigned 13 instead of 20. Because the operator * has higher priority than +, multiply 3 * 2 first and then add 7.

The following table lists operators from high to low by operator priority. Operators with higher priority appear above the table and operators with lower priority appear below the table. In the expression, the operator with higher priority will be evaluated first.

example
Please see the following example to understand the priority of operators in C language:

example

#include <stdio.h>
 
main()
{
   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;
   int e;
 
   e = (a + b) * c / d;      // ( 30 * 15 ) / 5
   printf("(a + b) * c / d The value of is %d\n",  e );
 
   e = ((a + b) * c) / d;    // (30 * 15 ) / 5
   printf("((a + b) * c) / d The value of is %d\n" ,  e );
 
   e = (a + b) * (c / d);   // (30) * (15/5)
   printf("(a + b) * (c / d) The value of is %d\n",  e );
 
   e = a + (b * c) / d;     //  20 + (150/5)
   printf("a + (b * c) / d The value of is %d\n" ,  e );
  
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

(a + b) * c / d The value of is 90
((a + b) * c) / d The value of is 90
(a + b) * (c / d) The value of is 90
a + (b * c) / d The value of is 50

 - [ ] List item

The data is being repaired

Keywords: C

Added by jdubwelch on Mon, 31 Jan 2022 15:21:52 +0200