Thread: strlen not displaying size as an int

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    2

    strlen not displaying size as an int

    I'm having a problem displaying the size of a string as an int.

    It works fine using my IDE but doesn't compile using clang on Ubuntu.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() {
        char s1[ ] = "The grey fox";
        char s2[ ] = "jumped";
    
    
        strcat(s1, s2);
    
    
        printf("%s\n", s1);
        printf("Length of s1 is %d\n", strlen(s1));
    
    
        strcpy(s1, s2);
    
    
        printf("s1 is now %s\n", s1);
    
    
        return 0;
    }
    Last edited by code9dan; 09-11-2018 at 04:35 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is a serious mistake:
    Code:
    char s1[ ] = "The grey fox";
    char s2[ ] = "jumped";
    
    
    strcat(s1, s2);
    You declared s1 to be an array of char with size enough to store the string "The grey fox", and initialised it with that string literal. That's great, except that that means that there's no space to append anything else to it, but that's what you're trying to do with the strcat call.

    One way to fix this is to explicitly declare s1 to be large enough to store the final string you have in mind.
    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

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    2
    @laserlight Thanks for response. I think a lot of the issues I face is because the tutorial app i'm using on my iPhone is out of date. I've had problems with put, get, strlen and others. I want to use linux and maybe a lot of this programs are made for visual basic c#.

    I use a tutorial app called Sololearn.

    I've looked through the help on this site and the books are so expensive. Can anyone can point me in the right direction for a cheap book or website or even another app that's better.

    P.S. With a extensive search I worked it out.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() {
    	char s1[ ] = "The grey fox";
    	char s2[ ] = " jumped.";
    	int len;
    
    
    	strcat(s1, s2);
    
    
    	printf("%s\n", s1);
    
    
    	len = strlen(s1);
    	printf("Length of s1 is %d\n", len);
    
    
    	strcpy(s1, s2);
    
    
    	printf("s1 is now %s\n", s1);
    
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by code9dan View Post
    Code:
        printf("Length of s1 is %d\n", strlen(s1));
    Code:
        printf("Length of s1 is %lu\n", strlen(s1));

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
         char s1[21] = "The grey fox";

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by code9dan View Post
    the books are so expensive. Can anyone can point me in the right direction for a cheap book or website
    For a reference, there is this wiki, which is available in Ubuntu as devhelp ( apt install cppreference-doc-en-html ) or QT help ( apt install cppreference-doc-en-qch ). For a tutorial, Steve Summit's notes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen help please
    By LuizCPFL in forum C Programming
    Replies: 7
    Last Post: 12-10-2008, 04:04 AM
  2. strlen()
    By BoneXXX in forum C Programming
    Replies: 6
    Last Post: 06-17-2007, 01:32 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. strlen...int?
    By Extol in forum C++ Programming
    Replies: 9
    Last Post: 04-17-2003, 04:35 PM
  5. strlen
    By XSquared in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2002, 07:05 PM

Tags for this Thread