简体   繁体   中英

"Too many arguments" error while running C program on VS Code

So here's the program that I want to run:

#include <stdio.h>

int main(){
    printf("Helloworld!");
    return 0;
}

And here is the error I get: 错误(参数过多)

I seriously don't know why this is happening. A month ago it was working just fine. Whenever I go to Run on VSCode and click "Start Debugging" this is what I get:

选择环境

Please help.

The issue is in the console command line, not the c-code. As suggested by @molbdnilo, the backslash is an escape character in bash. In particular, the final backslash in the file path is escaping the quote character, which means the cd command is not terminated. You can either use double-backslashes (to escape the backslash) and/or drop the last backslash in the path. In bash you can also use forward slash to separate elements in a path to avoid this issue.

As mentioned by Ezward, the issue is with your bash command. Suppose you have a file named "a.cpp" at you desktop. Open the terminal in same directory, ie desktop and try this command:

g++ -o objectFileName sourceFileName

For example in this case use:

g++ -o a a.cpp

This command would produce an object/executable file named "a" for your a.cpp file. Now to run the file enter this command:

./objectFileName

In this case that will be:

./a

This will run your code and you would see its output.

Note: I am assuming you're using g++ based compiler in linux/ubuntu. If not, google it on how to install g++ compiler. I hope this link helps:

https://askubuntu.com/questions/481807/how-to-install-g-in-ubuntu-14-04

As has been mentioned, the problem is in the command line, not in your code.

However, the problem is not with g++ or with && (which is good, not bad, since it stops when there is an error), but with your backslash at the end of the directory. Unlike cmd in Windows, bash interprets \" as a literal " , not as backslash followed by string delimiter.
This means that cd gets the two arguments

C:\Users\...\Desktop" && gcc hello.c -o hello &&

and

C:\Users\...\Desktop"hello

Setting the Default Profile ( Shell ) to CMD fixes the problem for me. >_<

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM