C Program को run करने के लिए Programmer अलग-अलग Compilers का
इस्तेमाल करते है जैसेकि, Turbo C/C++, CodeBlock, Cfree और Dev C/C++
इत्यादी .
Command Line के लिए main function में दो arguments होते है |
Command Prompt को Open करने के लिए Window+R Press करे | बाद में एक Run का Dialog Box खुल जायेगा उसमे 'cmd' नाम का Program डाले |
Command Prompt खुल जाएगा |
Source Code :
Output :
Compiler से program run कैसे होता है ?
Programmer पहले Source Code बनाता है, जिसका नाम Programmer कुछ भी रख सकता है, लेकिन .cpp extension/type लगाना अनिवार्य होता है | बाद में Preprocessor pre-process होता है | Pre-process होने के बाद Compiler के द्वारा Program को compile किया जाता है, Compile होने से जो IDE Programmer इस्तेमाल कर रहा है उसके द्वारा जिस file का नाम .cpp file type के साथ रखा है , उस file के नाम के साथ .obj extension की Object File तैयार होती है | बाद में Linker के साथ .lib library file तैयार होती है जो object file के साथ merge होती है | उसका बाद .exe executable file बनती है | जो हर Computer User अपने Computer में इस्तेमाल करता है |
Command Line Argument
Syntax for Command Line Arguments
int main( int argc, char *argv[])
Command Line के लिए main function में दो arguments होते है |
- argc
- argv[]
1. argc(Argument Counter)
- ये integer type का variable या argument है |
- इसमे arguments के total numbers main function को pass किये जाते है |
- Command line में file के नाम को भी वो argument लेता है
2. argv[](Argument Vector)
- ये character pointer type का argument होता है |
- ये actual arguments को main functionn को pass किये जाते है |
- ये सभी argument array बनाकर लेता है |
Example for Command Line Argument
Source Code :command.cpp
#include <iostream.h> using namespace std; int main(int argc, char *argv[]){ int i; cout<<"Number of Argument : "<<argc<<endl; for(i=1; i<argc; i++){ cout<<"Argument "<<i<<" = "<<argv[i]<<endl; } }
Command Line से Program Run कैसे किया जाता है ?
पहले Command Prompt को open करे |Command Prompt को Open करने के लिए Window+R Press करे | बाद में एक Run का Dialog Box खुल जायेगा उसमे 'cmd' नाम का Program डाले |
Command Prompt खुल जाएगा |
Drive को change करने के लिए जिस Drive पर जाना है उस Drive का नाम और उसके साथ 'colon' दे | for eg.E:, C:, D:
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\UD>E:
E:\>
Directory को change करने के लिए 'cd' type करे और जिस directory पर जाना है उसका path दे |
Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. All rights reserved. C:\Users\UD>E: E:\>cd E:\UD\Documents E:\UD\Documents>
Source Code :
command.cpp
#include <iostream.h>
using namespace std;
int main(int argc, char *argv[]){
int i;
cout<<"Number of Argument : "<<argc<<endl;
for(i=1; i<argc; i++){
cout<<"Argument "<<i<<" = "<<argv[i]<<endl;
}
}
Program Command line से run करने से पहले IDE से run करे, अगर बिना error से program चले तो फिर Command Line से चलाये |Output :
Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. All rights reserved. C:\Users\UD>E: E:\>cd E:\UD\Documents E:\UD\Documents>command Hi! Hello Friends Number of Argument 4 Argument 1 : Hi! Argument 2 : Hello Argument 3 : Friends E:\UD\Documents>
Example for Addition two Numbers using Command Line Arguments
Source Code :
commandline.cpp
#include <iostream.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char * argv[]) {
int i, sum = 0;
if (argc != 3) {
cout<<"Enter only Two Arguments.";
exit (1);
}
cout<<"Addition = "<<sum;
for (i = 1; i < argc; i++)
sum = atoi(argv[1]) + atoi(argv[2]); // atoi is used to convert string to integer
cout<<sum;
}
Output :
Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. All rights reserved. C:\Users\UD>E: E:\>cd E:\UD\Documents E:\UD\Documents>commandline 4 6 Addition = 010
E:\UD\Documents>







No comments: