moerjielovecookie

Sawen_Blog

一个普通工科牲的博客网站
x
github
follow
email

C Language Knowledge Review

C Language Tutorial | Runoob

Vscode Compile and Debug C Code#

Since vscode is just a text editor, it needs to configure task.json and launch.json to complete the compilation and debugging work. (The author's C environment is based on WSL 2 (Ubuntu-20.04))
task.json is as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build C Program",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}
  • version: Specifies the configuration format version, currently only supports "2.0.0".

  • tasks: Task array, each element corresponds to an executable task.

  • label: Task name, used for reference (e.g., preLaunchTask) and displayed in the UI list.

  • type: Task type, common values are "shell" (run in shell) or "process" (directly start an executable file).

  • command: The command or program name to execute.

  • args: List of parameters passed to the command, built-in variable interpolation can be used.

    • "-g": Generate debug symbols for source-level debugging with GDB and other debuggers.
    • "${file}": The full path of the file opened in the current active editor (e.g., /home/user/main.c).
    • "-o": GCC's output redirection option, specifies the path for the subsequent output file.
    • "${fileDirname}/${fileBasenameNoExtension}": Outputs the executable file to the same directory as the source file, with the same name as the source file but without an extension (e.g., outputs to /home/user/main).
  • group: Classifies tasks (e.g., build, test), and can mark as a default task within the group.

  • problemMatcher: Specifies how to parse command output, mapping compiler errors/warnings to the "problems" panel.

launch.json is as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "WSL Debug C",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "miDebuggerArgs": "--nx",
            "preLaunchTask": "Build C Program"
        }
    ]
}

C Language Scope#

Scope is the area in which a variable defined in a program exists; once outside this area, the variable cannot be accessed. There are three types of variables in C:

  1. Local variables inside functions
  2. Global variables outside all functions
  3. Formal parameters defined as function parameters

1 Local Variables#

Variables declared inside a function or block are called local variables. They can only be accessed by statements within that function or code block and are unknown outside the function or block.

2 Global Variables#

Global variables are defined outside of functions, usually at the top of the program. Global variables are valid throughout the entire program lifecycle and can be accessed from any function. In other words, global variables are available throughout the program after declaration.

If a local variable and a global variable have the same name, the local variable takes precedence within the function.

3 Formal Parameters#

Function parameters, or formal parameters, are treated as local variables within that function, and if they have the same name as a global variable, they will take precedence.

Pointers#

A pointer is a memory address, and a pointer variable is used to store a memory address, which must be declared before use.

type *var_name;

type is the base type of the pointer, and var_name is the name of the pointer variable. The following are valid pointer declarations:

int    *ip;    /* A pointer to an integer */
double *dp;    /* A pointer to a double */
float  *fp;    /* A pointer to a float */
char   *ch;    /* A pointer to a character */

The type of the value represented by different types of pointers is the same, all are hexadecimal numbers representing memory addresses. The difference between different types of pointers is the data type of the variable or constant they point to.

1 Pointer Usage#

Define a pointer variable, assign the address of a variable to the pointer, and access the address in the pointer variable.

#include <stdio.h>
 
int main ()
{
   int  var = 20;   /* Declaration of the actual variable */
   int  *ip;        /* Declaration of the pointer variable */
 
   ip = &var;  /* Store the address of var in the pointer variable */
 
   printf("Address of var variable: %p\n", &var  );
 
   /* Address stored in the pointer variable */
   printf("Address stored in ip variable: %p\n", ip );
 
   /* Access value using pointer */
   printf("*ip variable value: %d\n", *ip );
 
   return 0;
}
// Reference Runoob Tutorial

2 NULL Pointer#

When a pointer variable is declared, if there is no specific address value, a NULL can be assigned to the pointer variable, and at this point, the pointer is called a null pointer.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.