C - Understanding printf, putchar, scanf, and getchar

  • Thread starter Thread starter Bipolarity
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on the differences between C I/O functions: printf, putchar, scanf, and getchar. Putchar and getchar are optimized for single character operations, making them faster and simpler than their counterparts, printf and scanf, which handle complex data types and formatting. The conversation also addresses array management in C, clarifying that arrays cannot change size post-initialization without using malloc and realloc, and that arrays must contain elements of the same type, although workarounds exist using unions or void pointers.

PREREQUISITES
  • Understanding of C programming fundamentals
  • Familiarity with I/O operations in C
  • Knowledge of memory management functions like malloc and realloc
  • Basic concepts of data types and arrays in C
NEXT STEPS
  • Explore the differences between printf and fprintf for formatted output to files
  • Learn about dynamic memory allocation in C using malloc and free
  • Study the use of unions for storing different data types in a single array
  • Investigate multi-dimensional arrays and their implementation in C
USEFUL FOR

This discussion is beneficial for C programmers, software developers transitioning from languages like Java or Python, and anyone seeking to deepen their understanding of C I/O functions and array management.

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.
 

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
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K