Thread: strlen problem

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    strlen problem

    Guys,

    Have a very simple programme for finding the string length. But whats going wrong here.
    [
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>

    main()
    {
    char * s="ritika";
    int l;
    l=strlen(char *s);
    printf('%d',l);
    getch();
    /]

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You don't need to write char* when using s.
    That is only for the declaration.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Have tried putting l=strlen(s);. Did not work out

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Note that the printf format string requires double quotes, not single quotes.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    plus you include all these libraries for nothing . you only need stdio.h and strings.h.
    also , never write something like "main()" or "void main (void)" for start always write "int main (void)" and "return 0;"
    as the last line in the main function .

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    if i don't err the function getch() has need the including the library <curses.h>

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by vittorio92 View Post
    if i don't err the function getch() has need the including the library <curses.h>
    getch() is non-standard, as is the entire curses library and conio.h. Instead, try getchar() which is found in stdio.h. get rid of #include <conio.h> and #include <stdlib.h> lines.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    getch() is non-standard, as is the entire curses library and conio.h. Instead, try getchar() which is found in stdio.h. get rid of #include <conio.h> and #include <stdlib.h> lines.
    hi anduril462,

    i have insert the command man getch in my shell end in the page man i have find
    the include <curses.h> .... why?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by vittorio92 View Post
    hi anduril462,

    i have insert the command man getch in my shell end in the page man i have find
    the include <curses.h> .... why?
    Because, getch() function is non-standard. It is not part of the standard C libraries, it is found in an "extra" library. That means that it may not exist on every system, and it may behave differently on different systems. That is why I suggest you do not use getch. Instead, use getchar, which is a standard function all C implementations have.

    On *nix (Linux, Unix, BSD, etc) systems, it is generally part of the curses library (a screen/terminal management library). To use the functions in this library, you must tell the compiler about them, i.e. inform the compiler of their prototypes so it can check that you use them correctly. Those prototypes (including the one for getch()) are in the curses.h header. Often, this library is installed by default, hence the header file, library file and man pages existing on your system. But it may not exist on all *nix systems since it is non-standard.

    On Windows systems, getch() is often found as part of the conio library (an old and outdated screen/terminal library). I *think* that it exists on all Windows systems, but am not sure since I am a Linux guy, not a Windows guy.

    The conio library does not typically exist in *nix, nor the curses library in Windows. There are ports of curses for Windows (pdcurses being a prime example) and I'm sure somebody has ported conio library to *nix.

    EDIT: By the way, learn to use code tags properly: [code]Your code here[/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen problem
    By jammysammy in forum C Programming
    Replies: 10
    Last Post: 05-19-2011, 01:13 AM
  2. strlen intermittent problem
    By JM1082 in forum C Programming
    Replies: 5
    Last Post: 05-10-2011, 10:27 AM
  3. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 04:13 AM
  4. Problem with strlen()
    By ilmarculin in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 09:10 AM
  5. strlen problem
    By niroopan in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 05:46 PM