Open a command line window
method one
Open the folder of the specified file, enter "cmd" in the path bar, and press Enter to enter the console. The default path is the path to the specified folder.
Method Two
Open the specified folder, hold down the shift key, right-click in the blank space, and select "Open Powershell Window Here" in the menu bar, sometimes it may be "Open Command Window Here".
Method three
Press the Win key (Windows logo key) + R key on the keyboard at the same time, or search for "cmd" in the search box, enter "cmd" in the pop-up run window, click "OK" or press the Enter key. Open a command prompt.
Note : Administrator startup mode is: Win key + R key, entercmd
, then Ctrl + Shift + Enter.
Enter "cd" and a space after the default path, and then enter the specified folder path. After pressing Enter, the original default path remains for the time being. Then enter the disk where the specified folder is located, for example, enter "D:" here, and press Enter to get the specified path.
Method four
Enter cd+space to open folders one by one.
Command prompt to compile & execute cpp files
Step 1: Open Command Prompt
As shown before, press win+r at the same time and enter "cmd".
Step 2: Find the location of the .c file program you want to run:
Take the following program as an example: C:\Users\11\OneDrive\Desktop\hello.c
Step 3: Compile the code
Go to the directory where the file is saved and type: gcc hello.c and press Enter. If it shows that gcc is not an internal instruction, you need to install gcc.
If there are no errors in the code, the command prompt will jump to the next line and generate a.exe executable file.
Step 4: Execute the program
Now, type a.exe to execute the program.
Note : The command prompt runs the exe file generated after running the cpp file! .
You can see "Hello, World!" displayed on the screen.
Pass a string to a function
Command line parameters
When executing a program, values can be passed to the C program from the command line. These values are called command line parameters, and they are important to the program, especially when controlling the program from the outside rather than hard-coding these values within the code.
The command line parameter is to pass a string to the function. Two formal parameters need to be added to the main function. Among them, argc refers to the number of parameters passed in. It includes the file name, so the initial value is 1; argv[] is a Array of pointers to each parameter passed to the program.
When passing a string to a function, just add it directly at the end, separated by spaces.
The following is a simple example that checks whether the command line provides parameters and performs corresponding actions based on the parameters:
#include<stdio.h>
int main(int argc,char*argv[])
{
int i;
printf("The program name is:%s\n ",argv[0]);
printf("%d\n",argc);
if(argc > 1)
{
printf("The other arguments are following:\n");
for(i=1;i<argc;i++)
{
printf("%s\n",argv[i]);
}
}
return 0;
}
The result is as follows:
Some common command operations
d: | Switch to D drive |
cd space path |
Enter the current path directory, eg: cd C:\Users\Public |
cd. | Current directory |
cd.. | Return to the previous directory |
cd\ | Return to the root directory of the current drive letter |
dir | View the contents of folders and files under the current path |
dir/s/b d:\file | Find files. You only need to enter the path, no need to cd to return the path |
date | Date setting command |
time | Time setting command |
cls | clear screen |
exit | Exit the command prompt window |
F7 | View the command history and execute the used command after selecting it. |
ctrl+c | Terminate command |
cd.>file.txt | Create a new empty file, eg: cd.>file.docx |
type nul> newtest.txt | Create new empty file |
type filename.suffix | View Files |
ren source file.suffix new file.suffix | Modify file name |
echo The contents of the file>new.txt | Create a new non-empty file |
md d:\typora\file | new folder |
mkdir newtest | new folder |
start file name | Open a folder or file |
command-help | Use the help command to view the first form of help |
Order /? | Use the help command to view the second form of help. |
cd /? or cd/? | View the specific usage of the cd command ( / indicating the function switch of the command) |
md /? or cd/? | View the specific usage of the md command |
tree | Generate a directory tree and press ctrl+c to stop it. |
md drive letter:path name folder name | Create folder |
rd drive letter: path name folder name | delete folder |
del a.txt /p | Delete the a.txt file in the a folder (other folders in the folder will not be modified) |
del drive letter: path file. suffix name parameter | Delete the specified file under the specified path |
del drive letter: path *.txt /p | Delete all files with extension txt in the specified path |
del *.txt | Delete the file with the specified suffix, eg: del *.docx |
copy con file.suffix | Create a file |
copy source directory/file destination directory/file | Copy files, eg: copy E:\music\*Eason Chan*.* E:\test |
move path\filename path\filename | Move files |
netstat | Check network connection status |
ping www.csdn.net | Query IP address |
ipconfig | View ip address |
netstat -ano | Check the network connection, status and corresponding process ID |
shutdown | -s shutdown |
shutdown -p | Shut down the local computer without timeout or warning |
shutdown -f | Force closing of running applications without warning the user in advance |
shutdown -s -t 60 | Scheduled shutdown, timer 60s, time customized |
shutdown -r | Shut down and restart |
shutdown -l | Log out current user |
shutdown -h | To hibernate, you can use -f, shut down -h -f. Cannot be used with -t |
shutdown -a | Lift order |
explorer.exe | To enter the desktop through the command line, you can use the following command |