c - Using an if/else statment to check that enum arguments exist -



c - Using an if/else statment to check that enum arguments exist -

i'm having slight issue current piece of code , trying check arguments have been passed enum before trying go on program;

enum arg {argname, sinearg, samplearg, argc}eargs; int main(int argc, char *argv[]){ long samplingrate = atol(argv[samplearg]); float sinefreq = atof(argv[sinearg]); if (argc < eargs ){ printf("usage: sinefreq\tsamplingrate\n"); }else{} }

the code compiles fine, although when run without arguments programme returns "segmentation fault: 11" instead of usage message want print console.

your usage check against eargs has nil number of command line arguments passed. no matter how many arguments pass, eargs going 0.

also should check before using of command line arguments. place status @ start of main():

#include<stdio.h> #include <stdlib.h> enum arg {argname, sinearg, samplearg, argc}eargs; int main(int argc, char *argv[]) { /* if ( argc < 3) explicit , clear here */ if (argc < argc ) { /* expect @ to the lowest degree 2 arguments */ printf("usage: sinefreq\tsamplingrate\n"); homecoming 1; // homecoming main on failure } long samplingrate = atol(argv[samplearg]); float sinefreq = atof(argv[sinearg]); .... }

i have included standard headers using printf() , atol()

c enums arguments

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -