Like Tree1Likes
  • 1 Post By quzah

how to input a string of character?

This is a discussion on how to input a string of character? within the C Programming forums, part of the General Programming Boards category; -I know how to scanf a character but dont know how to scanf a string. This is correct? scantf("%f",lop); -How ...

  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.
    rags_to_riches likes this.
    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
    21,725
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:15 AM
  2. String/character input with getline
    By JM1082 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2011, 02: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, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21