Thread: using strlen() i got a segmetation fault.

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    using strlen() i got a segmetation fault.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    	char buf[81];
    	int x;
    		scanf("%s", buf);
    		x = strlen( buf );
    		printf("length: %d", x) ;
    return 0;
    }
    in the above code i got this warning: incompatible implicit declaration of built-in function ‘strlen’
    does anyone know why?


    the next code is almost the same but it has a small change in the green marks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    	char buf[] = "I am a programer";
    	int x;
    		x = strlen(buf);
    		printf("length: %d", x);
    return 0;
    }
    My question is the following: when i use the 1st program typing "This is C book" returns lenght: 4 but in the second instead of "I am a programmer" if i write "This is C book" it will return lenght: 14
    . Why?
    i' ll be glad if anyone could help me! thanks in advance...
    Last edited by brack; 08-25-2010 at 02:31 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    i got this warning: incompatible implicit declaration of built-in function ‘strlen’
    does anyone know why?
    All the string functions are declared in <string.h>, so you need to include it.

    when i use the 1st program typing "This is C book" returns lenght: 4 but in the second instead of "I am a programmer" if i write "This is C book" it will return lenght: 14
    I believe that's because scanf is stopping at the first space. Putting a string literal in your code works, because the strings contains all the words and spaces. When you use scanf, if only really reads the first word, and is storing just that in the string. There are more advanced codes than %s that you can put in your format string to get scanf to behave differently (as a quick example: http://bytes.com/topic/c/answers/218...e-white-spaces)

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    oh yeah! stupid mistake...thanks again...

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You didn't mention anything about your segmentation fault.

    scanf does not read line, but a word (token) separated by spaces (or new line characters).

    It returns a correct length of 'This' which is 4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  3. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmetation Fault
    By nomes in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 01:28 AM