簡體   English   中英

使用來自 argv 的值在 header 文件中設置一個變量

[英]set up a variable in header file using a value from argv

我需要多次運行我的代碼,更改 header 文件中定義的變量。

我想編寫一個編譯(通過makefile)並運行的bash腳本

./myprogram some_value

有沒有辦法在我包含在 myprogram.c 中的 header 中使用我從 atof(argv[1]) 恢復的內容? (在許多其他文件之間共享的 header 包含我在代碼中使用的參數的值)

在那個 header 我會有類似的東西

float the_parameter;

我想初始化為 atof(argv[1])

首先,在 header 文件中使用extern聲明變量。

extern float the_parameter;

然后,在您的一個源文件中,聲明不帶extern的變量並對其進行初始化。

#include <stdlib.h>

float the_parameter;

int main(int argc, char* argv[]) {
    if (argc > 1) the_parameter = atof(argv[1]);

    /* other code */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM