Thread: If someone could explain.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    19

    If someone could explain.

    Why does this print 1, I thought it should print 0 since there is nothing in it and strlen dosn't count the \0 at the end of the string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main () {
        char a[50];
        int b = strlen(a);
        printf("\nlength:%d\n",b);
        
       return(0);
    }
    Now here

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char a[50];
        int b = strlen(a);
        printf("\nlength:%d\n",b);
           
        a[0]=fgetc(stdin);
        printf("\n--%c\n",a[0]);
        b = strlen(a);
        printf("\nlength:%d\n",b);
        
        a[0]=0;
        printf("\n--%c\n",a[0]);
        b = strlen(a);
        printf("\nlength:%d\n",b);
        
    }
    but not looking at this part

    Code:
    int main()
    {
        char a[50];
        int b = strlen(a);
        printf("\nlength:%d\n",b);
    I give the char a[0] a character, and then I say that the value of a[0] is [NULL].

    And then it dosnt see anything there.

    So for example if I had a string and use fgets and stdin to write in it, and then for every character of the char string I say it equals to 0, that is [NULL].

    So then it would show me strlen 0, and it wold look as if the string is empty.
    Why is that so btw?


    Oh and another question my friend told me if I do this

    char text[500] = {0};

    That then basicly every char should be = 0;
    Is that really how it works?
    Last edited by JohnnyC; 6 Days Ago at 07:29 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,689
    Because your strings are uninitialised, so strlen() could be anything.

    > Is that really how it works?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    C does not remember the length of a.

    strlen counts bytes until it reads a NULL, where it stops counting and returns its result.

    A string with a NULL as its first byte is considered empty. In your program, you did not put a value into a at first. That does not mean that a is empty.

    Maybe it has tonight's winning lottery numbers in it. Who knows? Standard C library functions, including strlen, will understand the first NULL in the string to mark its end. That could be the zeroth byte, or the 200th (C does not remember the length of a.)

    fgetc does not mark the end of your string with a NULL. Maybe you want to use fgets.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,290
    strlen counts bytes until it reads a NULL,
    Please quit saying NULL when at best you mean '\0' or 0 or nil or null. NULL has special meaning in C.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    19
    Sometimes I use a decimal 0 or a char [NULL]

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,290
    Again NULL has a special meaning in C and should only be used when talking about pointers. And note in many circumstances NULL is defined as ((void *)0) which doesn't always evaluate to zero.

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    19
    Ok I'm gonna inform myself a bit more about when NULL should be used.

    Thanks to everyone for helping me

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Quote Originally Posted by jimblumberg
    And note in many circumstances NULL is defined as ((void *)0) which doesn't always evaluate to zero.
    I think that is false: ((void *)0) always evaluates to zero, even if the internal representation might not actually be a representation for zero. This must be so because in the condition of an if statement, the decision as to whether to execute the body of the if statement depends on whether the expression compares unequal to 0. If ((void *)0) does not evaluate to zero, then we have a case where a null pointer as the expression of an if statement's condition causes the if statement's body to be executed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this..??
    By seemaxie in forum C Programming
    Replies: 11
    Last Post: 06-27-2012, 09:44 AM
  2. Someone explain this to me
    By bigmac(rexdale) in forum C Programming
    Replies: 5
    Last Post: 10-12-2008, 03:29 PM
  3. can anyone explain this?
    By anntenna333 in forum C Programming
    Replies: 6
    Last Post: 04-26-2006, 09:38 AM
  4. Can someone explain to me
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 06-22-2005, 11:09 AM
  5. Please explain this to me
    By kermit in forum C Programming
    Replies: 5
    Last Post: 10-01-2003, 05:07 PM

Tags for this Thread