Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: If statement

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If statement

    Dear sir,

    Something wrong of my C coding as I can't run my program.

    #include <stdio.h>
    main()

    #define Blank_space " "
    char ch;
    printf ("Enter a character :");
    ch = getchar();
    if (ch == Blank_space)
    printf("It is a blank space");
    else
    if ((ch >= "A") && (ch <= "Z"))
    printf("It is a capital letter");
    else
    if ((ch >= "a") && (ch <= "z"))
    printf("It is a small letter");
    else
    if ((ch >= "0") && (ch <= "9"))
    printf("It is a digital");


    Best regards,

    Xiang

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    #include <stdio.h>

    // The ctype.h file contains useful functions for chars.
    #include <ctype.h>

    // Main function should start with
    int main(void)

    // You need a brace to start any block of code
    {

    char ch;

    printf ("Enter a character :");
    ch = getchar();

    // To see if a char is a space use isspace or isblank.
    if (isspace(ch))
    printf("It is a blank space");

    // Use isupper to check for capitals
    else if (isupper(ch))
    printf("It is a capital letter");

    // islower for lowercase
    else if (islower(ch))
    printf("It is a small letter");

    // isdigit for numbers
    else if (isdigit(ch))
    printf("It is a digital");

    // Returning 0 means the function had no probs
    return 0;

    // Closing brace after main function
    }

    If you want to check for capitals, digts, and so on your own way, remember to make sure that you enclose characters with single, not double quotes.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •