CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Difference between printf( ) and fprintf( )

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
srikrishna



Joined: 06 Sep 2017
Posts: 67

View user's profile Send private message

Difference between printf( ) and fprintf( )
PostPosted: Mon Oct 09, 2017 6:21 am     Reply with quote

I have read the ccs c manual but can not understand when i use fprintf. The manual says 'If fprintf() is used then the specified stream is used where printf() defaults to STDOUT (the last USE RS232).' I can not understand the line. Can anybody explain this with example ?

Ttelmah



Joined: 11 Mar 2010
Posts: 13389

View user's profile Send private message

PostPosted: Mon Oct 09, 2017 7:00 am     Reply with quote

printf, is the original standard C syntax.
Code:

printf("FRED");

sends 'FRED' to the standard output device.

However if you are using streams (so have multiple serials setup), which you have given stream names, then fprintf, allows you to send things to these different streams. So if you have setup two serial streams UART1 & UART2 for two serial ports, then:
Code:

printf(UART1,"FRED");
printf(UART2,"HARRY");


sends 'FRED' to the stream called UART1, and 'HARRY' to the stream called UART2.

The same applies for fputc, and fgetc, which allow you to send/get individual characters to named streams.
Named streams in C, is also standard now.

There are a lot of examples in the 'examples' with the compiler.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: Difference between printf( ) and fprintf( )
PostPosted: Mon Oct 09, 2017 9:18 am     Reply with quote

srikrishna wrote:
The manual says 'If fprintf() is used then the specified stream is used where printf() defaults to STDOUT (the last USE RS232).' I can not understand the line.


Some background information:

Originally and for a long time, C had/still has three "standard" input and outputs:

Standard Output, STDOUT
Standard Input, STDIN
Standard Error, STDERR

These have to be connected to something, otherwise anything you send is simply lost and you will always read nulls ('\0'). This connection is done automatically by the compiler and in the case of something like a PC application, by the operating system. In C's original habitat, UNIX and related operating systems, devices, such as serial ports were simply virtual files provided by the operating system, whereas Microsoft OSes treats them as devices. CCS C is a bit different: all three are connected to the device set up in the last #use RS232 line.

printf and related routines like getc, putc, puts, all use these standard streams and all send or get their data from the device in the last #use RS232 line.

If you don't want to use the standard streams, or want to use more than the standard, say to have two output streams conected to two different serial devices, then you have to have some way of identifying which stream/device you want to use, and of connecting streams with devices. CCS uses a non-standard way of doing this. Fprintf and related routines, such as fputc and fgets, are not really meant to deal directly with devices as they do in CCS C. Instead they are intended for use with files on (mostly) external storage devices. In most Cs you have to open the file with something like:
Code:

FILE *file_handle;
file_handle = fopen("./blah");

fprintf(file_handle, "My value = %7.2f\r\n", My_float);

fclose(file_Handle);


However there is no operating system in CCS C and no file system, therefore there are no files to open, so CCS C doesn't have C-style files. Instead it has the idea of streams (named input/output serial channels), and uses fprintf and similar functions to name the streams and tell them apart. CCS C does not have the standard C file control functions such as fopen, fclose and so on.

So, if you name a device in the use statement, you have to use that name in fprintf, or similar, rather than use printf. If you use printf it CCS C will send the output to the device in the last #use RS232 line and that may not be where you want it to go.

Personally, I do not recommend using both printf and fprintf in the same program. Either have one serial device and use printf or if you have more than one, name them all and always use fprintf with the relevant name.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group