Actions

EmSys

CCoding Style - Use else-ifs for multi-way decisions3

From EdWiki

Revision as of 06:51, 7 March 2012 by Jshankar (Talk | contribs) (1 revision)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Use else-ifs for multi-way decisions ...

    If( argc != 3 )
        printf( “Usage : cp inputfile outputfile\n” );
    else if( ( fin = fopen( argv[1], “r”) ) == NULL )
        printf( “Can’t open input file %s\n”, argv[1] );
    else if( ( fout = fopen( argv[2], “w”) ) == NULL ) {
        printf( “Can’t open output file %s\n”, argv[2] );
        fclose(fin);
    } else {
        while( ( c = getc(fin) ) != EOF )
            putc(c, fout);
        fclose(fin);
	fclose(fout);
    }