Tuesday, July 14, 2009

How to print output in the "C" language programming?

I want to print the output of programme in "C" language





is it possible

How to print output in the "C" language programming?
Look up the printf() function. That is a good place to start.





Oh! wait if you want to take it directly to the printer then if you are on unix then you can just pipe your program to the printer device.





$ myprogram | lpd





On Windows, you could save your output to a file and then print that file with notepad.





c:\dev\myprogram %26gt;output.txt


c:\dev\notepad output.txt
Reply:Here's some examples


printf ("Hello");


cout%26lt;%26lt;"Hello";


int num = 3;


printf("Num : %d",num)


cout%26lt;%26lt;"Num : "%26lt;%26lt;num;
Reply:Print it how? Not to be mean but one of the fascinating things about these answers is how everyone interprets your question differently. I don't think you're irresponsible but it would be nice if you added in additional information whether you want to print it to the screen or to the printer.





I'm on Linux. I have been for a few years and haven't concerned myself with printing to recent iterations of Windows. Both Linux and MS-Dos (the ancestor of Windows) took from Unix the policy of treating everything as a file, even device drivers. Win32 versions like the last ones I did any programming on, continued this. PRN and LPT1 I believe are the names of the printing device in Windows. stdout is the name of the screen in C, period.





Cout is just wrong, if you're asking about C rather than C++. Cout is stdout in the iostream.h library, which is C++, not C. You send variables to it using the %26lt;%26lt; operator, which is also in iostream. What I have to say below applies to a C++ program, once it's compiled.





The C output is usually taken care of by one of 2 functions: printf("format string", variable1, variable2...); or fprintf(Outputdevice, "format string", variable1, variable2...);. You can see how similar they are. Printf is short for PRINT Format. Fprintf is short for File PRINT Format. Printf is the functional equivalent of fprintf(stdout, "format string", ...); Of course you should read the documentation which will tell you among other things, that if you don't have any variables in your format string, you don't need a comma and variable list after, and you only need as many variables in your list as you have indicated in your format string. Most C compilers don't have names for your printer, so if your documentation doesn't indicate one you probably can't make fprintf(PRN...) work. You have two choices. You can use printf or you can open up a file and fprintf your output to the file. Then of course you can send the output file to your printer. In Unix and in older MS/Dos/Windows you can send it a couple of ways. In Unix/Linux just type lpr filename or executable_file %26gt; lpr. In Windows, type executable_file %26gt; prn or print filename. In Windows you will be asked if you want to overwrite prn. Answer yes. Windows treats your prn as a file, but it isn't a file, it's a device, so instead of messing up your driver you will be sending all the characters to the device to print out.





I know this sounds heavy but I'm tired. I hope it helps.
Reply:Use the printf function, which is included in the stdio.h header file.





To include said header file, simply put #include %26lt;stdio.h%26gt; at the very top of your code (this is a preprocessor instruction).





Using printf is simple - it takes a single string argument/parameter, and you can use %d, %f etc to substitute with various variables which can also be passed to the function as extra arguments.
Reply:For Free C++ Lessons For Beginner


Enter Programming_Share Group





Omar Abdallah


No comments:

Post a Comment