in c printf() returns what [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
in c printf() returns what?

printf returns:
On success, the total number of
characters written.
On failure, a negative number.

Point your browser to www.google.com;
Search "printf c";
Almost any result you'll get will tell you:
Return Value
On success, the total number of characters written is returned.
On failure, a negative number is returned.
Was that so difficult?

It returns the number of characters printed. See man fprintf for simple questions like this.
From the man page:
These functions return the number of characters printed (not including the trailing '\0' used to end output to strings) or a negative value if an output error occurs, except for snprintf() and vsnprintf(), which return the number of characters that would have been printed if the n were unlimited (again, not including the final '\0').

The result of "printf" is the number of characters written. If a write error occurs, "printf" returns a negative number. (ANSI standard)
Even wikipedia has a whole article about printf, where you can find the different return values for different languages and times.

Exactly what it says it returns in any decent library reference
On success, the total number of characters written is returned.
On failure, a negative number is returned.

Number of characters (not including the trailing \0) printed on success, negative value on failure. see man printf.

Related

Representation of a C binary file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For a homework assignment I created a simple compression/decompression program that makes use of a naive implementation of run-length encoding. I've gotten my program working; compressing and decompressing any text file with a pretty large number of characters (e.g. the program source) works flawlessly. As an experiment I tried to compress/decompress the binary of the compression program itself. This resulted in a file that was much smaller than the original binary, and is obviously un-runnable. What is causing this data-loss?
My assumption was that it's related to how binary files are represented, but I can't figure much out past that.
Possible issues:
Your program opens the binary file in the text mode, which damages the '\r' and '\n' bytes
Your program incorrectly handles zero bytes, treating them as ends of strings ('\0') and not as data of its own
Your program uses char (that is actually signed char) for the bytes of data and correctly works only with non-negative values, which ASCII chars of English text are, but fails to work with arbitrary char/byte values, which may be negative
Your program has an overflow somewhere which shows up only on big files
Your program has some other data-dependent bug
If the platform is linux (as the question is tagged), there's no difference between binary and text modes. So it shouldn't be that; but even so, the files should be opened as binary.
I suspect that your problem is the program treats '\0' characters as terminators (or otherwise specially) instead of as valid data.

scanf and entering characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I was wondering what happens if in my program I ask for an integer input using scanf("%d",&num) and the user enters a string of chars and ints for example at3stf0rfun.
What would be the value of num in that case? Does it take only the integers 30 or does it also convert the chars to its' ASCII decimal values?
Scanning stops on encountering invalid input. Therefore, in your code the value of num will remain unchanged.
To detect whether this is what happened you need to examine the return value of scanf. To quote man scanf:
RETURN VALUES
These functions return the number of input items assigned. This can
be fewer than provided for, or even zero, in the event of a matching
failure. Zero indicates that, although there was input available,
no conversions were assigned; typically this is due to an invalid
input character, such as an alphabetic character for a `%d'
conversion. The value EOF is returned if an input failure occurs
before any conversion such as an end-of-file occurs. If an error or
end-of-file occurs after conversion has begun, the number of
conversions which were successfully completed is returned.

What is the meaning of f in printf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the meaning of "f" in C's printf?
The f in printf stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f indicates formatted output (or formatted input for functions in the scanf family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s happens to contain % characters, printing it directly with printf can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf-like variadic functions.
If I'm not mistaken, printf stands for "Print formatted data to stdout".
printf allows for formatting, while print doesnt. Also, print doesn't exist in C. I don't even know what printg is.

When should I use fputs instead of fprintf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
What exactly is the difference between the two?
fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results.
fputs simply writes the string you supply it to the indicated output
stream.
fputs() doesn't have to parse the input string to figure out that all you want to do is print a string.fprintf() allows you to format at the time of outputting.
As have been pointed out by other commenters (and as it's obvious from the docs) the great difference is that printf allows formatting of arguments.
Perhaps you are asking if the functions are equivalent where no additional arguments are passed to printf()? Well, they are not.
char * str;
FILE * stream;
...
fputs(str,stream); // this is NOT the same as the following line
fprintf(stream,str); // this is probably wrong
The second is probably wrong, because the string argument to fprintf() is a still a formating string: if it has a '%' character it will be interpreted as a formatting specifier.
The functionally equivalent (but less direct/efficient/nice) form would be
fprintf(stream,"%s", str);
Uhm...
...puts() just writes a string, while printf() has a number of formatting facilities for several types of data.
fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
Documentation is useful! Learn to read it, and you'll have a powerful tool on your side.

memory error in c program [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have made a program in which I am handling files but I am getting this error
(I have run it through gdb)
Program received signal SIGSEGV, Segmentation fault.
0x0016e50b in vfprintf () from /lib/tls/i686/cmov/libc.so.6
I am not getting the exact line that contain error.
Can anybody tell me
what this error means
does it occur for any special reason
or is it general error occurring because of many reason?
What does this error mean?
It basically means that you're accessing memory that you're not supposed to be accessing.
Does it occur for any special reason?
A segmentation violation can occur for a huge number of reasons. However, since it's happening in vfprintf, it's likely to be limited to something like:
Invalid file pointer.
Not passing enough parameters for the format string.
Passing a NULL pointer for a C string.
Passing a non-null-terminated pointer for a C string.
Memory corruption from a totally different part of the program.
That's the most likely reasons.
Or is it a general error occurring because of many reasons?
As I said, it can occur for a vast number of reasons but it's probavly limited based on your circumstances.
Check all of the parameters before calling the printf call (not with printf of course, use some more robust debugging code such as printing each character of a string with flushing and fsyncing after each). And check that the file handle is valid and that the number of parameters passed to vprintf matches those specified in the format string.

Resources