C - Understanding printf, putchar, scanf, and getchar

  • Thread starter Thread starter Bipolarity
  • Start date Start date
Click For Summary
C is recognized for its complexity, particularly regarding static typing and I/O operations. Key distinctions between functions like printf and putchar, as well as scanf and getchar, are crucial for understanding C's I/O model. A "stream" is a broader concept that encompasses various data sources, not just files, and functions like putchar and getchar are optimized for speed and simplicity, handling single characters efficiently. In contrast, printf and scanf are more complex, capable of processing various data types but at the cost of performance.Regarding arrays, they cannot be resized after initialization unless dynamic memory functions like malloc and realloc are used. All elements in a standard C array must be of the same type, although workarounds exist, such as using unions or void pointers. While multi-dimensional arrays are possible, they are limited in flexibility. An alternative approach is to create an array of pointers to other arrays, which offers more versatility. Overall, array management in C presents challenges, particularly for anything beyond one-dimensional arrays.
Bipolarity
Messages
773
Reaction score
2
C is a pretty tough language to grasp for me due to its static typing and rather confusing I/O.
I was hoping someone could answer the following questions for me, keeping in mind that I am fairly experienced in Java and Python but have almost no experience in C/C++ or any assembly language.

1) What is exactly the difference between printf and putchar? Printf seems to print everything you need it to print. So why on Earth would you need to use putchar? I hear that purchar send a single character to the output stream. What exactly is an output stream and how does it differ from what printf does?

2) Same for the above, except between scanf and getchar.

3) Can array sizes be changed after the array has been initialized? Or do you have to reinitialize the array?

4) Can arrays store values of different data types?

5) Can arrays store arrays?

Thanks!

BiP
 
Technology news on Phys.org
1 and 2. Think of a "stream" as being a generalisation of a "file". it doesn't ahve to be a file stored on your hard disk, it can also be characters displayed on a screen, or typed from a keyboard, or transmitted over a network to another computer or a device like a robot, or whatever.

Putchar and getchar are simple and run fast, because they only do one thing. Printf and scanf are big and complicated and relatively slow, because they can do a lot of work converting characters to and from other data type (integers, floating point numbers, pointers, etc).

If you just want to read or write one character, use putchar or getchar. (If nothing else, it's less to type and there are fewer ways to do it wrong). If you want to USE the more complicated things that printf and scanf can do, then use them instead.

The terminology gets a bit scrambled here, because when you open a stream you get a data structure called a FILE, not a stream, and the versions of putchar etc that can access arbitrary files are called fputchar, etc, i.e. f for file not s for stream. But there are routines called sprintf and sscanf, which "read" and "write" data to and from a string (array) of characters in memory.

3. Yes, if you use the functions malloc and realloc. No, if you just declare array variables in your code. (C++ has features equivalent to "variable size arrays" that resize themselves automatically, but not C).

4. The simple-minded answer is no, because all the elements of an array are the same type. But there are ways to cheat. For example the "type" can be a "union" of different data types, or it can be pointers to data of type "void" which can point to anything. (But you have to keep track of what type each individual item of data is yourself). This is completely different (and much more powerful) in C++.

5. You can declare multi-dimensional arrays, but they are quite restrictive. Alternatively you can have an array of pointers which point to other arrays (same as 4.) Array handling is one of the bad design features of C (and C++ isn't much better), for anything beyond one-dimensional arrays.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K