This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

  1. Overview of C language
  2. C language identifier
  3. C data types
  4. constant

01 Part One C Overview

A preliminary examination outline

/* The first program */
#include <cstdio>
int main(a) { 
    printf("Hello World"); 
    return 0;
Copy the code

The code for

Type 1: Data structure definition

/* Sequential storage description of the stack */ 
#define MaxSize 50// Define the maximum number of elements in the stack
typedef struct{ / / structureElemtype data [MaxSize];// Store the stack elements
    intTop;// Top of the stack pointer
Copy the code

Type 2: Functional implementation of an algorithm

/* Stack logic */
Bool Pop(SqStack & S,ElemType & x){ 
    if(S.t op = = -1)
        return false; X = s.d. ata [S.t op -];// Exit the stack first and subtract 1 from the pointer
    return true; }Copy the code

For example,

  • 【2009年 computer 408 年 考】41.(13分) Let L=(A1, A2, A3… ,an-1,an) adopt single linked list with lead node

Save, the nodes in the linked list are defined as follows:

Typepedef struct node {int data; Struct node * next:} node;Copy the code

Please design an algorithm with space complexity O(1) and as efficient as possible in time, and rearrange all nodes in L to obtain the linear table L=(A1, A2, A3… , the an – 1, an). Requirements: (1) give the basic design idea of the algorithm (2) according to the design idea, use C or C++ language to describe the algorithm, the key point to give a note (3) explain the time complexity of the algorithm you designed.

  • 43. A group of n(n≥ 3) philosophers sat at a round table, each alternately eating and thinking. There are m(m≥ 1) bowls in the center of the national table, and l chopsticks between each two philosophers. Each philosopher had to get a bowl and chopsticks on either side before he could eat. When he had finished, he put the bowl and chopsticks back in their place and went on thinking. To allow as many philosophers as possible to eat at the same time and to prevent deadlocks, use the semaphore operations (wait (), signal ()) to describe the mutual exclusion and the step, and explain the semaphore and initial values used.

Matters needing attention

1. KaoFa

  • C language grammar knowledge alone
  • Data structures and algorithms are examined

2. Review strategy

  • Master the basic syntax of C language, including array, pointer, structure, function, etc.
  • C language to achieve common algorithms, such as sorting algorithm, search algorithm.

3. Algorithm suggestions

  • In the exam, you should write the words well, code well, and write clearly. Then you should check whether you have accomplished the goal of the question. Finally, you should consider optimizing the problem.

02Part Two IDENTIFIER of the C language


Identifiers are simply our own names, such as variable names, array names, structure names, and so on. These names should follow certain specifications:

  • Contains only letters, digits, and underscores (_).
  • Must begin with a letter or underscore;
  • Strictly distinguish between upper and lower case letters, such as A and A are not the same;
  • You cannot use keywords (such as int, double, etc.) as identifiers. Keywords:

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

  • The selection of identifiers can generally make people clear at a glance, such as age, height, etc.

The written form

1. All statements must start with a semicolon (;). To end, as:

printf(" hello world ");Copy the code
  1. Suggest writing comments to help with reading and understanding;
  2. Comment format:
  • One-line comment: // haha
  • Multi-line comment: /The comments are here/
  1. Note, for example
/* const double PI = 3.14; // PI is a constant comment ending here */
Copy the code

03 Part Three C data types

The data type

This is a general case, but it varies from platform to platform, such as 32-bit and 64-bit platforms, which can be tested with sizeof keyword.

04 Part Four Constants

The constants that do not change during the program are called constants. Constants are divided into integer constants, real constants, string constants, and character constants.

  • Integer constants:

It consists of numbers and can be expressed in ten, eight, or hexadecimal. Prefix: 0x indicates hexadecimal, 0 indicates octal, and without a prefix, it defaults to decimal. Suffixes: U for unsigned integer, L for long integer. Such as 123,0456, 0x123, 30L and so on.

  • A real type constant

It consists of integer part, decimal point, decimal part and exponential part. Decimal form: consists of a number and a decimal point, as in 3.14159. Exponential form: powers are separated by the letter e, as in 1.2e-5.

#include <cstdio>
#define3.14 PI?// Specify PI as 3.14 constant int main() {
    int r=2;double s=pi*r*r; 
    return 0; }Copy the code
  • Character constant

Put them in single quotes. Common characters: can be single letters, numbers, or punctuation marks. Such as: ‘A’, ‘2’, ‘*’, ‘$’, etc. Escape character: An ASCII character that cannot be displayed. For example: ‘\r’ (carriage return), ‘\n’ (newline), ‘\t’ (TAB), etc.

  • String constant

A sequence of strings enclosed by a pair of double quotes. Such as “hello, world!” , “123”