C - Understanding printf, putchar, scanf, and getchar

  • Thread starter Bipolarity
  • Start date
In summary, the conversation discusses the challenges of learning C for someone experienced in Java and Python, and seeks clarification on the differences between printf and putchar, and scanf and getchar. It also covers the topic of changing array sizes, storing values of different data types in arrays, and storing arrays within arrays. The conversation also touches on the concept of streams and the relative advantages of using putchar/getchar versus printf/scanf.
  • #1
Bipolarity
776
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
  • #2
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.
 

Related to C - Understanding printf, putchar, scanf, and getchar

What is C?

C is a high-level, general-purpose programming language that was originally developed in the early 1970s by Dennis Ritchie at Bell Labs. It is a powerful and versatile language that is widely used in the development of operating systems, application software, and embedded systems.

What are the benefits of using C?

C is a highly efficient and fast language, making it suitable for projects that require high performance. It also has a relatively small footprint, making it a popular choice for embedded systems. C is also a portable language, meaning it can be used on different platforms without needing to make major changes to the code.

How is C different from other programming languages?

C is often referred to as a "mid-level" language, as it combines elements of both high-level and low-level languages. Unlike high-level languages, C gives the programmer more control over how the computer's hardware is utilized. However, C is still considered a high-level language compared to low-level languages like assembly, which require more direct manipulation of hardware.

What are some common use cases for C?

C is commonly used in the development of operating systems, such as Unix and Linux. It is also widely used in the development of system and application software, as well as in embedded systems. C is also popular in the fields of scientific computing and game development.

Is C difficult to learn?

While C may have a steeper learning curve compared to some other programming languages, it is not considered overly difficult. It has a relatively small and simple syntax, making it easier to learn compared to other languages. With practice and dedication, anyone can learn to code in C.

Similar threads

  • Programming and Computer Science
Replies
10
Views
5K
  • Programming and Computer Science
Replies
1
Views
954
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
19
Views
4K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
15
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top