1

I know beginner level C++. In C++ when I want to, for example. input the user's name, all I need to dos is:

getline(cin,str);

but I am trying to learn C.( I want to play with kernels and embedded systems). I went to geekforgeeks and yotube?. I was not able to find any straight forward way to input a string of undefined size? Why is that? I just don't want the code I want an explanation on why this is so difficult in c compared to C++.

Where ever I went on the internet I saw cstring inputs as:

char name[100];

scanf("%s",name);

Which I do know will only work for strings without white spaces.

10
  • 5
    Allocate an array of one character using malloc. Then in a loop read one character at a time, reallocating (with realloc) the array over and over again. Once done, do one last reallocation to add the terminating null character. Commented Jul 16 at 10:08
  • 2
    The homonym getline for C. Commented Jul 16 at 10:08
  • 1
    Or if you're on a POSIX system (like Linux or macOS) use its getline function. It's very likely that someone has ported it to Windows, if that's your target. Commented Jul 16 at 10:09
  • 1
    "stick to c standard" contradicts "play with kernels and embedded systems" that provide own C runtimes. Commented Jul 16 at 10:13
  • 1
    Note that %s will get you a word so it's not like C++'s std::getline Commented Jul 16 at 11:35

3 Answers 3

4

I just don't want the code I want an explanation on why this is so difficult in c compared to c++.

In C, strings (or any variable length data) oblige the programmer to perform much of the memory management. In C++, various standard libraries exist to do that already.

C++ has Strings which encode the text as well as its length and size - O(1) to report is length. C strings use a trailing null terminator to determine length - obliging O(n) to report the length.


I am trying to learn c.( I want to play with kernels and embedded systems).

For now, as a learner, consider not such play and stick to standard C and its standard library without resorting to a given implementation's extensions.


input a string of undefined size?

Use fgets() to read a line of user input into a string. Use an ample sized buffer and consider excessively long input as invalid rather than try to handle arbitrarily long input.

Sign up to request clarification or add additional context in comments.

2 Comments

@ZulqarnainBinAsif on embedded systems, it is often better not to use dynamic memory management but to plan the memory usage.
Thank you so much for you reply I needed this guidance bad.
3

I am trying to learn c.( I want to play with kernels and embedded systems)

You are brave... C is not that hard to learn, because its library and its possible expressions are only a subset of C++ (yes the C++ standard library contains the whole C standard library...). The downside is that you are now at a much lower level and will have to code by hand all the goodies (dynamic vectors, maps, etc.) or find third party libraries.

The rationale is that is allows/forces the programmer to use custom algorithms to solve common problems, which can lead to nice optimizations in a given context. Here, for example, if you are short in memory but execution time is not that sensitive, you should read one character at a time, and realloc often your buffer by short increments. If memory is not that sensitive, you would use larger buffers and read larger data chunks. If you do not know, do not try to invent an oval wheel when round ones are already available, and search the GNU libraries to see if you problem is not already solved.

Do not expect to be a genius: writing C code is (rather) easy, but writing robust yet efficient code is hard and requires extensive tests. So the rule is:

  • if it already exists and covers your requirements: do use it
  • only if the above conditions are not met: code it from scratch

Comments

0
  • there's a readline() in C just install it using your package manager, more about readline()

  • otherwise you could use a combination of read() and realloc() until you reach EOF

2 Comments

read() is a POSIX function, not standard C.
Absolutely, good to mention. but I think it would do fine for his use case, otherwise he might need platform specific APIs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.