About space character and "NULL" ascii codes

This is a discussion on About space character and "NULL" ascii codes within the C Programming forums, part of the General Programming Boards category; Hello guys!, I have a question about ascii codes. In C programming when i get input from user and search ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Question About space character and "NULL" ascii codes

    Hello guys!,

    I have a question about ascii codes.
    In C programming when i get input from user and search this input's inside for space characters i use

    Code:
    #include <stdio.h>
    void main(){
       char amountOfBill[15], chrOfInput;
       int loop = 0
       printf("Enter Value = ");
       scanf("%s", amountOfBill);
    
       chrOfInput = amountOfBill[0];
    
       while(chrOfInput != 0){
          /*some codes*/
          loop++;
           chrOfInput = amountOfBill[loop];
       }
    }
    this code.
    i use
    Code:
    while(chrOfInput != 0)
    because i check this character is last character of string or not but space and null ascii codes are same, how can we solve this problem

    for example if user give "1234.5" that's correct, but "1234 .5" wrong, because after 4, program suppose this is end of the string

    Hopefully, i was explained my problem also sorry about my english

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    but space and null ascii codes are same
    The space character is not the same as a '\0'. The problem is not with your string but with your input method. The scan() family of functions stop processing the string when they encounter a white space character. Any remaining characters are left in the input buffer for the next extraction operation. If you want to allow the user to enter spaces then you should use something like fgets() which doesn't stop processing until it encounters a end of string character. Also you should never use scanf() to retrieve a string unless you specify the maximum size of the string.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Welcome to the forum

    It is
    Code:
    int main(void)
    and not
    Code:
    void main()
    Every time you write it like this, you can see the picture of Salem to see what happens.

    Run this code and you will understand everything
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            /* Create an array which has 1, a space,2 and the
            * null terminating character as contents */
            char array[] = { '1' , ' ' , '2' , '\0'};
    
            int i;
    
            for( i = 0 ; i < 4 ; i++)
            {
                    /* Space */
                    if(array[i] == ' ')
                            printf("A space!\n");
                    if(array[i] == '1')
                            printf("A number! Which number? %c\n",array[i]);
                    if(array[i] == '2')
                            printf("A number! Which number? %c\n",array[i]);
                    if(array[i] == '\0')
                            printf("null terminating character found!\n");
            }
    
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,624
    Quote Originally Posted by std10093 View Post
    It is
    Code:
    int main(void)
    and not
    Code:
    void main()
    Every time you write it like this, you can see the picture of Salem to see what happens.
    Mhm, well, Salem is venting his frustration rather than making a statement about what's valid: freestanding implementations are allowed to define their own entry point.
    Quote Originally Posted by C99
    5.1.2.2 Hosted environment

    #1

    A hosted environment need not be provided, but shall conform to the following specifications if present.
    5.1.2.2.1 Program startup

    #1

    The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    Code:
    int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    Code:
    int main(int argc, char *argv[]) { /* ... */ }
    or equivalent;8) or in some other implementation-defined manner.
    The standard only applies much of itself to hosted environments. "void main(void)" may be perfectly valid for freestanding implementations, but most people aren't aware of what a freestanding implementation is, while they are almost certainly not working on one.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    The space character is not the same as a '\0'. The problem is not with your string but with your input method. The scan() family of functions stop processing the string when they encounter a white space character. Any remaining characters are left in the input buffer for the next extraction operation. If you want to allow the user to enter spaces then you should use something like fgets() which doesn't stop processing until it encounters a end of string character. Also you should never use scanf() to retrieve a string unless you specify the maximum size of the string.

    Jim
    Thank you Jim, i got point but when i make debug, inside of string like this for example "145 .45"; 145\0\0\04645, 46 is equal to point, whether, i try diffrent input methods

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by std10093 View Post
    Welcome to the forum

    It is
    Code:
    int main(void)
    and not
    Code:
    void main()
    Every time you write it like this, you can see the picture of Salem to see what happens.

    Run this code and you will understand everything
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            /* Create an array which has 1, a space,2 and the
            * null terminating character as contents */
            char array[] = { '1' , ' ' , '2' , '\0'};
    
            int i;
    
            for( i = 0 ; i < 4 ; i++)
            {
                    /* Space */
                    if(array[i] == ' ')
                            printf("A space!\n");
                    if(array[i] == '1')
                            printf("A number! Which number? %c\n",array[i]);
                    if(array[i] == '2')
                            printf("A number! Which number? %c\n",array[i]);
                    if(array[i] == '\0')
                            printf("null terminating character found!\n");
            }
    
            return 0;
    }
    Thank you std10093 , i learnt like
    Code:
     void main(){}
    but agani thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with ignoring "space" character in a string.
    By Grigoris Savvas in forum C Programming
    Replies: 2
    Last Post: 10-20-2012, 10:07 AM
  2. Replies: 4
    Last Post: 07-18-2011, 05:30 PM
  3. Replies: 46
    Last Post: 08-24-2007, 04:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21