Some Missing Knowledge Points of C Programming Language for C++ Programmer

As a programmer with C++ as the first programming language, I have been unfamiliar with some important features and idioms of C programming language. By reading "The C Programming Language", some missing knowledge points are sorted out as follows.

1. extern declaration

The keyword extern is used to declare external variables. And there are differences between declaration and definition:

  • definition: It means creating variables or allocating memory units.
  • declaration: It just describes the nature of the variable, but does not allocate memory.

When the definition of an external variable appears before the function that uses it, the extern declaration can be omitted; otherwise, there must be an extern declaration (for example: involving multiple source files).

Example:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int n = 1;

int main(void)
{
extern int n; # extern declaration
printf("%d\n", n);
return 0;
}

Modifying an external variable or function with the static keyword can limit the scope of the object declared later to the rest of the compiled file, and other files cannot access the object.

The keyword static can also be used to declare internal variables: variables that can only be used within a function but always occupy memory space.

2. macro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// cancel the definition of a macro
#undef getchar


// the macro used to debug
#define dprint(expr) printf(#expr " = %g\n", expr)

dprint(x/y);

// the macro will expand to:
printf("x/y" " = %g\n", x/y);


// preprocessor operator ##
#define paste(front, end) front ## end

// the result of the macro call paste(name, 1) will create the symbol name1

conditional include

example 1:

1
2
3
4
5
6
#if !defined(HDR)
#define HDR

/* hdr.h */

#endif

which is equal to:

1
2
3
4
5
6
#ifndef HDR
#define HDR

/* code here */

#endif

example 2:

1
2
3
4
5
6
7
8
9
10
#if SYSTEM == SYSV
#define HDR "sysv.h"
#elif SYSTEM == BSD:
#define HDR "bsd.h"
#elif SYSTEM == MSDOS:
#define HDR "msdos.h"
#else:
#define HDR "default.h"
#endif
#include HDR

3. union

union: Variables that hold objects of different types and lengths at different times.

example:

1
2
3
4
5
union u_tag {
int ival;
float fval;
char *sval;
} u;

union is actually struct, all its members have an offset of 0 relative to the base address, this struct space should be large enough to accommodate the widest member, and its alignment should be suitable for members of all types in a union.

4. bit-field

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// define a bit field, the number after ':' indicates the width of the field
// bit-fields are similar to small integers and can appear in arithmetic expressions
struct {
unsigned int is_keyword : 1;
unsigned int is_extern : 1;
unsigned int is_static : 1;
} flags;

// set value of bit-field
flags.is_extern = flags.is_static = 1;
flags.is_extern = flags.is_static = 0;

// test value of bit-field
if (flags.is_extern == 0 && flags.is_static ==0)
...

Bit-field is not an array and has no address, the & operator cannot be used on it.

5. File Operation

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
FILE *fp = fopen(name, mode);	// open the file in the specified mode; failure returns NULL
fclose(fp); // close the file

// return the next character from the file; EOF if end-of-file or error occurs
int getc(FILE *fp);

// write character c to the file pointed to by fp and return the character written
int putc(int c, FILE *fp);

// formatted input and output to files
int fscanf(FILE *fp, char *format, ...);
int fprintf(FILE *fp, char *format, ...);

// judge errors
int ferror(FILE *fp); // 如果流 fp 中出现错误,则返回一个非 0 值
int feof(FILE *fp); // 如果文件到达文件结尾,返回一个非 0 值

// line input and line output
// Read the next input line (including '\n') from the file pointed to by fp and store it in line. Read at most maxline-1 characters. Normal end returns line; error returns NULL
char *fgets(char *line, int maxline, FILE *fp);
// Write line to the file pointed to by fp. Returns EOF on error; otherwise returns a non-negative value.
char *fputs(char *line, FILE *fp);

Some Missing Knowledge Points of C Programming Language for C++ Programmer
https://arcsin2.cloud/en/2023/05/01/Some-Missing-Knowledge-Points-of-C-Programming-Language-for-C-Programmer/
Author
arcsin2
Posted on
May 1, 2023
Licensed under