Thread: isspace code problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    isspace code problem

    Hi,

    I have written a little piece of code, but it is not working well. I want to check if argv[1][0] is a whitespace character or not. Both the printf functions do not print output to sdtout. This is the code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(int argc, char *argv[])
    {
    
    
        if (isspace(argv[1][0]))
    {
        
            printf("The value is a whitespace character");
        
        } else {
           
            printf("The value is a non whitespace character");
            
        }
    
    return 0;
    
    }
    Can someone tell me why it is not working?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    I assume you are calling it without an argument, which would cause undefined behaviour. You need to supply an argument. To supply one that starts with a whitespace character you would need to use quotes: progname " arg"

    If you are just trying to tell if the argument exists then use argc (the argument count). If it is greater than 1 then there is at least one argument. If you want exactly one argument then argc should equal 2.
    Code:
    #include <stdio.h>
     
    int main(int argc, char **argv) {
        if (argc != 2) {
            printf("You must supply one argument.\n");
            return 1;
        }
     
        printf("Argument: %s\n", argv[1]);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    Thanks for the help. I am going to rewrite my code.

    Ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using isspace
    By allen. in forum C Programming
    Replies: 13
    Last Post: 09-01-2014, 06:53 AM
  2. for and isspace
    By izzy in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 06:18 PM
  3. isspace()
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2008, 02:02 PM
  4. isspace() is the way!
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 10:23 AM
  5. Isspace ??? Anyone Know?
    By justin69enoch in forum C Programming
    Replies: 12
    Last Post: 01-23-2003, 05:02 PM

Tags for this Thread