473,890 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getc() vs. fgetc()

I'm sure this has been asked before, and I have looked in the FAQ, but I'm
looking for an explanation for the following:

The functions pairs:

gets()/fgets()
puts()/fputs()
printf()/fprintf()
scanf()/fscanf()

differ primarily in that the first one assumes stdin/stdout while the second
one works with a stream passed by the programmer. This makes sense and makes
the functions easy to remember.

But then we have:

getc()/fgetc()
putc()/fputc()
getchar()/fgetchar()
putchar()/fputchar()

In each case the pairs of functions perform the same task. This makes it
hard for people that don't use these functions all the time because
everytime they use one they have to look up whether it assumes one of the
standard streams or not. Is there a reason that the standard did not adopt a
consistent (and quite useful) naming convention for these functions?


Nov 14 '05
13 15159
In <10************ *@corp.supernew s.com> "William L. Bahn" <wi*****@toomuc hspam.net> writes:
It still seems slopply to use the 'f' prefix for more than one thing in
functions that are in the same library and have so much surface similarity
to each otehr But I can see that the original developers could easily have
been so close to the material that they didn't see it that way - at least
not when it mattered.


The C's "standard" library was not the work of a restricted set of
developers, it grew more or less chaotically, which explains many of its
inconsistencies . There is no mention of fgetc/fputc functions in K&R1,
so they're probably a later addition to the library.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
Is there anyone that wants to take a crack at this.

I guess what I'm looking for is some way to have temporary, local storage
in a macro. A type-independent way would be nice, but even it is has to
be type-specific that would be fine.

"William L. Bahn" <wi*****@toomuc hspam.net> wrote in message
news:10******** *****@corp.supe rnews.com...
THANKS!

I was trying to think of a way a macro could evaluate an argument more than once and the obvious answer just didn't make itself obvious to me:

#define sq(x,y) ( (x)*(x) )

evaluates it more than once and hence would have problems with:

d = sq( x*=2 );

What's the general way of handling something like this where you need to use the value more than once? Using pow() is not the answer because that would
only work in a case similar to this one and I'm looking for a general way.

Can you do something like:

#define sq(x) {double u; u=(x); u*u}

That won't work because you can't do:

y = {3}; // Curly braces, not parens

But is there some trick that would let you do the same idea ?


Nov 14 '05 #12
In <10************ *@corp.supernew s.com> "William L. Bahn" <wi*****@toomuc hspam.net> writes:
Is there anyone that wants to take a crack at this.

I guess what I'm looking for is some way to have temporary, local storage
in a macro. A type-independent way would be nice, but even it is has to
be type-specific that would be fine.


The answer is no, if the macro has to return a value. GNU C provides the
required extensions for both type-generic local declarations and for
returning a value from a block.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
On Wed, 14 Jul 2004 22:26:52 -0600, "William L. Bahn"
<wi*****@toomuc hspam.net> wrote:
#define sq(x,y) ( (x)*(x) )

evaluates [its arg] more than once and hence would have problems with:

d = sq( x*=2 );
Or x++, which is usually preferred for demonstrating this problem.
Can you do something like:

#define sq(x) {double u; u=(x); u*u}

Not in standard C; you can do almost this as an extension in GNU C,
aka the language implemented by gcc; see "statement expressions".

The closest you can come in standard/portable C is
#define foo(x) (temp = x, temp*temp /* or whatever */)
/* or temp = (x) if you like to be consistent with other macros */
where a variable temp (or other fixed name) of correct or at least
suitable type must be in scope everyplace you want to use the macro.
It's simple to make the variable "global" or at least file scope,
using a more involved name to avoid any conflict; but not threadsafe,
though standard C doesn't have threads anyway, and not reentrant, if
you combine macros with functions to produce the recursion a macro
can't do directly. Alternatively require that the caller provide it,
preferably locally, which is a nuisance and clutter.

Or, not really a trick, just make it a function defined (by #include
if convenient) before any use in the/each source file, probably
static=internal and inline if C99 or gcc or some others; it is very
likely though not guaranteed you will get the same actual code as you
would have for the macro if you could write it.

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2942
by: Bill Cunningham | last post by:
Would getc and ungetc be the best and most simple what to parse expressions for a parser? Bill
6
2294
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's best to "think of it")? I've seen some explain it as if they call fgetc internally, then I've seen some people explain it as if they call some lower level OS system call like read(). I've even seen some older posts talk about a input functions...
8
17198
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
3
3358
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object.
7
3358
by: hzmonte | last post by:
My C program has the following: static int skip_space(FILE *fp, int *line, int c) { int i = 0; if(feof(fp)) { printf("in skip feof ...\n"); } printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c); while ((c == ' ' || c == '\t') && !feof(fp)) { printf("i=%d\n", i++); if(feof(fp)) { printf("in skip feof\n"); break;} c = getc(fp);
2
4413
by: Praesidium | last post by:
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears now: # include <stdio.h> # include <string.h> int main(void) { printf("Fine at line 6"); FILE *source; char string1;
32
2114
by: vippstar | last post by:
Assuming all the values of int are in the range of unsigned char, what happends if getc returns EOF? Is it possible that EOF was the value of the byte read? Does that mean that code aiming for maximum portability needs to check for both feof() and ferror()? (for example, if both feof() and ferror() return 0 for the stream when getc() returned EOF, consider EOF a valid byte read) To me, that seems to be the case, but maybe the standard...
3
4994
by: ANURAGBAJPAI | last post by:
The getc() is a macro while fgetc() is a function in the stdio.h library.Please tell me what is macro and explain the sentence.
3
9654
Parul Bagadia
by: Parul Bagadia | last post by:
getc gets the charachter from file stream and points to next charachter, fgetc gets the charachter from file stream and i don't know whether it points to next charachter or not; is that a significant change? In the help menu of Turbo C, it's given that its a function version of getc macro. what is a function version?
0
9829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10836
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4683
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4278
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.