Thread: how to input a string of character?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    23

    how to input a string of character?

    -I know how to scanf a character but dont know how to scanf a string.
    This is correct?

    scantf("%f",lop);

    -How to define the variable to save the string in?
    string "lop";

    Thanks,

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's no such thing as a "string".

    Sure, some programming languages have a typedef for it, but it doesn't truly exist. What people refer to as a string is simply an area of consecutive memory bytes. You'll see it referred to as a "char array" (an array of characters in the stack), or a "char pointer" (points to the first byte in memory that the "string" is stored in the heap). The "string" type in any language is one of those two things, and high-level (read: less powerful) languages do all of the memory management for you.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by memcpy View Post
    There's no such thing as a "string".

    Sure, some programming languages have a typedef for it, but it doesn't truly exist. What people refer to as a string is simply an area of consecutive memory bytes. You'll see it referred to as a "char array" (an array of characters in the stack), or a "char pointer" (points to the first byte in memory that the "string" is stored in the heap). The "string" type in any language is one of those two things, and high-level (read: less powerful) languages do all of the memory management for you.
    This is quite possibly the dumbest thing I've ever read. You're telling me that there is no such thing as a string in any programming language? That's just dumb.

    As to C, read 7.1.1 for the definition of a string:
    A string is a contiguous sequence of characters terminated by and includeing the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to the initial (lowest addressed) character. The length of a string is the number of characters preceeding the null character and the value of a string is the sequence of values of the contained characters, in order.
    You may not like the definition, but there are strings in C (and in a multitude of other languages).


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    thanks memcpy , I will print it and learn it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tahmod
    I know how to scanf a character but dont know how to scanf a string.
    Personally, I suggest that you read up on fgets: the naive way of using scanf to read a string leaves you open to buffer overflow, and correcting it without hard-coding the buffer length is not so straightforward as just using fgets.

    Quote Originally Posted by memcpy
    There's no such thing as a "string".

    Sure, some programming languages have a typedef for it, but it doesn't truly exist.
    There is no built-in or standard library string type in C, but the concept of a string exists in C as quzah pointed out. In other programming languages, the concept of a string may also be expressed as a built-in or library string type. Stating that there is no such thing as a "string" in your view also means that there is also no such thing as an "integer", for indeed an "integer" in any programming language is ultimately "simply an area of consecutive memory bytes". This may be true, but it is not useful at all, both in practical computer programming and theoretical computer science.

    Quote Originally Posted by tahmod
    thanks memcpy , I will print it and learn it.
    It does not really matter, but I note that it is quzah who linked you to those references.
    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. Character input
    By johnymanos_arc in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2011, 12:15 PM
  2. String/character input with getline
    By JM1082 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2011, 03:28 PM
  3. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  4. Replies: 5
    Last Post: 04-12-2006, 06:30 PM
  5. how to exclude character input
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2002, 09:20 AM