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 requires configuration of task.json and launch.json to complete the compilation and debugging tasks. (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: An array of tasks, each element corresponds to an executable task.

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

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

  • command: The command or program name to be executed.

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

    • "-g": Generates debug symbols for easier source-level debugging with GDB and other debuggers.
    • "${file}": The full path of the file currently opened in the 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 /home/user/main).
  • group: Classifies the task (e.g., build, test), and can mark it as a default task within the group.

  • problemMatcher: Specifies how to parse the 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 language:

  1. Local Variables inside a function
  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 not known outside the function or block.

2 Global Variables#

Global variables are defined outside functions, usually at the top of the program. Global variables are valid throughout the program's 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#

The parameters of a function, 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 corresponding to different types of pointers is the same, which is a hexadecimal number representing a memory address. The difference between different types of pointers lies in 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 stored 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  );
 
   /* The address stored in the pointer variable */
   printf("Address stored in ip variable: %p\n", ip );
 
   /* Accessing value using pointer */
   printf("*ip variable's 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, at which 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.