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.
Related
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 example in C, the NULL ('\0') stands for "nothing". And in mit-scheme the nil or the '() stands for nothing. So does Ruby use nil.
So i just wonder what's the symbol or variable that stands for "nothing" in other programming languages. thx.
You are wrong about C language NULL and '\0' being the same. Usually in C, NULL is used with pointers and '\0' is used with strings.
For example, The following code fragment compiles without any warning.
int *p=NULL;
char ch='\0';
But the following code gives you the warning "initialization makes integer from pointer without a cast" on compilation.
int *p='\0';
char ch=NULL;
So we can conclude that they are not equal and interchangeable.
Javascript: null
Python: None
It all depends from language developers preferences.
Everything in Ruby is object. nil is an object and empty string "" is also an object etc... But "" means 0 length string and nil means object with no reasonable value - in some situations it really is "nothing".
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.
Is it any possible chance to print to IO device with the help of scanf() function?
main()
{
char str[30];
scanf("\n Name ?%s",&str);
printf("\n Name Entered is %s",str);
}
Try this and help me out.
The scanf function reads input from the console and parses it. It has no means of printing anything. That's what the aptly-named printf family of functions is for.
The first argument to scanf is not a prompt (as it looks like you're assuming), it's the format string to be used to scan the input.
That scanf will fail unless your input matches exactly what's expected including the literal string "Name ?" and so forth. It will also stop at the first whitespace so entering udhayar kumar would only get your first name. If you want a prompt simply output it beforehand such as with:
char str[30];
printf ("Name? ");
scanf ("%s", str);
printf ("Name Entered is %s\n", str);
However keep in mind that unbound %s format specifiers for scanf are an easy way to crash your program or give someone a security hole they can get through (see "buffer overflow"). That's because there's no bounds checking on the input.
If you want a decent input function, check out this one. It gets an entire line (with optional prompt) and handles error conditions well.
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 just started in C programming and I know that "%d" defines that it will be a number value, as in:
int x = 9;
printf("X = %d", x);
getchar();
return 0;
but what are the other variable specifiers for C? (to define strings, and do they change for float, double, long, etc.?)
There's a good summary of the specifiers available at http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders .
Your question as phrased is meaningless. The %d is a format specifier, and has nothing to do with variable declarations.
If you were to google 'printf c', you would find many, many sites that answer your question.
You may want to pick up a copy of Kernighan and Ritchie's The C Programming Language. It short but fully packed. Working through this book is well worth the time.
But as to your question, you're asking about Format specifiers for string literals. You'll probably also want to look at escape characters soon (e.g. \n for new lines). Fortunately, it's all in K&R (the above book), specifically on page 153 (2nd edition).
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 would like to extract file names and their corresponding MD5 sum from a check sum file in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.
You can read lines easily enough using fgets(). Don't even think of using gets().
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf() to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf() string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf() to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.
You should be able to implement this with fopen(), fgets() and strchr() - but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a ) character?)
I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?
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.