![](/img/trans.png)
[英]Using command line in C to detect arguments, and then print out the first or second char of the arguments
[英]Print using command line arguments
我正在嘗試使用命令行中的環境變量讓程序打印特定的 output,但該程序似乎卡在我的第一個輸入上。
這是我的代碼
#include <stdio.h>
#include <stdlib.h>
void print_data(char*select);
int main (int argc, char * argv[]){
int ret;
char *ch = NULL;
if( argc == 2)
{
ch = argv[1];
}
else
{
ch = getenv("V1");
}
void print_data(ch);
return 0;
}
void print_data(char* select){
if(select, "k")
{
printf("Value set to key\n");
}
else if(select, "l")
{
printf("Value set to last name\n");
}
else if (select, " ")
{
printf("Value set to %s\n", select);
}
}
我的問題是程序要么只打印“值設置為鍵”或“值設置為姓氏”。 如何使用 Print_data 中的指針來檢查命令行中輸入了什么命令?
if(select, "k")
幾乎可以肯定沒有做你想做的事,但並不完全清楚你想要什么。 該表達式等效於if(1)
,這就是您得到所見行為的原因。 也許您打算:
if( select[0] == 'k' ) ...
else if( select[0] == 'l' ) ...
else if( select[0] == ' ' ) ...
寫得更好:
switch( select[0] ) {
case 'k': ...;
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.