Asked  7 Months ago    Answers:  5   Viewed   80 times

If I don't know how long the word is, I cannot write char m[6];,
The length of the word is maybe ten or twenty long. How can I use scanf to get input from the keyboard?

#include <stdio.h>
int main(void)
    char  m[6];
    printf("please input a string with length=5n");
    printf("this is the string: %sn", m);
    return 0;

please input a string with lenght=5
this is the string: hello



Enter while securing an area dynamically


#include <stdio.h>
#include <stdlib.h>

char *inputString(FILE* fp, size_t size){
//The size is extended by the input with the value of the provisional
    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(*str)*size);//size is start size
    if(!str)return str;
    while(EOF!=(ch=fgetc(fp)) && ch != 'n'){
            str = realloc(str, sizeof(*str)*(size+=16));
            if(!str)return str;

    return realloc(str, sizeof(*str)*len);

int main(void){
    char *m;

    printf("input string : ");
    m = inputString(stdin, 10);
    printf("%sn", m);

    return 0;
Tuesday, June 1, 2021
answered 7 Months ago


The scanf() family of functions reads data from the console or from a FILE stream, parses it, and stores the results away in variables you provide in the argument list.

The format string is very similar to that in printf() in that you can tell it to read a "%d", for instance for an int. But it also has additional capabilities, most notably that it can eat up other characters in the input that you specify in the format string.

What's happening is scanf is pattern matching the format string (kind of like a regular expression). scanf keeps consumes text from standard input (e.g. the console) until the entire pattern is matched.

In your second example, scanf reads in a number and stores it in x. But it has not yet reached the end of the format string -- there is still a space character left. So scanf reads additional whitespace character(s) from standard input in order to (try to) match it.

Thursday, July 29, 2021
answered 5 Months ago

Use scanf("%[^n]",buffer);. It will accept white space.

Sample program-

int main()
    char buffer[2048];
    printf("Enter the stringn");

    printf("%sn", buffer);
    return 0;


root@sathish1:~/My Docs/Programs# ./a.out 
Enter the string
abc def ghi ijk
abc def ghi ijk
root@sathish1:~/My Docs/Programs# 
Saturday, August 7, 2021
answered 4 Months ago

What you need to do is read the line in smaller increments, and resize your buffer as you go.

As an example (not tested and not meaning to be particularly elegant, just an example):

char *readline(FILE *f)
   char *buf = NULL;
   size_t bufsz = 0, len = 0;
   int keep_going = 1;

   while (keep_going)
      int c = fgetc(f);
      if (c == EOF || c == 'n')
         c = 0;             // we'll add zero terminator
         keep_going = 0;    // and terminate the loop afterwards

      if (bufsz == len)
         // time to resize the buffer.
         void *newbuf = NULL;
         if (!buf)
            bufsz = 512; // some arbitrary starting size.
            newbuf = malloc(bufsz);
            bufsz *= 2; // issue - ideally you'd check for overflow here.
            newbuf = realloc(buf, bufsz);

         if (!newbuf)
            // Allocation failure.  Free old buffer (if any) and bail.
            buf = NULL;

         buf = newbuf;

      buf[len++] = c;

   return buf;
Thursday, August 26, 2021
answered 4 Months ago

It's in the Row. I fixed your xml a bit :)

var xml = "<item attr="some attribute"><description>anything</description></item>";
var ds = new DataSet();

ds.ReadXml( new StringReader( xml ), XmlReadMode.Auto );

var ia = ds.Tables[0].Rows[0].ItemArray;
var att = ia[1]; // att == "some attribute"

If you don't have a schema, you might have to check the column to determine what it is.

Per comment: You will see I am letting it infer the schema (XmlReadMode.Auto). It takes elements under the root node as Rows then adds the attributes in order and then the value in the element. So for example the following XML ...

var xml = "<items>
             <item attr1='attr1' attr2='attr2'>
             <item attr1='attr3' attr2='attr4'>

I will get two rows (one for each item) with Columns for attr1, attr2 and description. You can change the way it interprets the XML using a schema.

Saturday, August 28, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :