Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A Few questions about C

  1. May 5, 2013 #1
    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?


  2. jcsd
  3. May 5, 2013 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook