Search code examples
cvisual-studio-2013printfcommand-line-argumentsargv

argc and argv in vstudio - to upper echo


i wrote a code that basically do the same thing of echo command in bash shell; but if read as argument -c uppercase all input argv. But, if i input hello -c, i've as only output argc = 3

#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
    int i,j;

    printf("argc = %d\n", argc);
    if (!strcmp(argv[argc-1], "-c"))
        for (i = 1; i < argc-1; ++i){
            for (j = 0; j != '\0'; ++j)
                putchar((char)toupper(argv[i][j]));
            putchar('\n');
        }
    else for (i = 0; i < argc; ++i)
        printf("argv[%d] = %s\n", i, argv[i]);
    return 0;
}

Solution

  • I think, in your code

      for (j = 0; j != '\0'; ++j)
    

    is problematic. You should be checking the array element against null. It should rather read

    for (j = 0; argv[i][j] != '\0'; ++j)
    

    That said, putchar() takes an int as argument, so you don't need to cast the return value of toupper().