Command Line Argument in CodeBlocks

How to run a program in CodeBlocks explained with the example

Program:


Step1: Type the program in CodeBlocks and save with the name “even.c”


#include<stdio.h>

int main(int argc, char *argv[])
{
    if(argc!=2){
            printf("\nArgument not in correct format\n");
            exit(1);
    }

    if(atoi(argv[1])%2==0)

        printf("\nEVEN NUMBER\n");
    else
        printf("\nODD NUMBER\n");

return 0;

}





Step 2: Compile the program by clicking on Build





Step 3: Open Command Prompt 



                  
Now you need to open the Directory where you have saved your program 
( Shown in image STEP 1 )

To run just type program name 
( Shown in image STEP 2 )

 To pass Arguments in the program type :      <program_name> <space> <argument>
  Ex:     even 4 ↵ 

Output will be Displayed .

Post a Comment

0 Comments