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.
while working on the c project I found the following problem.
I declared a float variable and performed scanf operation on it. The sample code is -
<#include stdio.h>
<#include conio.h>
void main()
{
clrscr();
float foo;
scanf(" %f ",&foo);
//remaining code goes here
getch();
}
I found that the error is due to the spaces given after and before %f in scanf statement.
But I don't know what is the reason behind it ?
thanks.
I think this may help you a little.
the function scanf will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace).
A single whitespace in the format string validates any quantity of
whitespace characters extracted from the stream (including none).
Also it should be like #include<stdio.h> not <#include stdio.h>
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 10 years ago.
I am trying to make a program that if the user gives a whole arithmetic operation from the keyboard ex 5*4 or 5/7 it will interpret the operation ex *, /, +, or - and print the result. How can I read the whole operation without the user pressing enter everytime he puts a number or a symbol ex * or / and put them in 3 variables?
I tried using:
printf("give an operation")
scanf("%d%c%d",&num_1,&c,&num_2)
but I want to do with:
c=getchar()
If I understand your question and comments correct, you want to read a line of input from the user, where the user enters an expression and uses the Enter key to end the expression?
Then using scanf should work fine. You could also use fgets to get the line, and the use sscanf for the parsing.
scanf("%d %[*+-/] %d", &numA, op, &numB);
op should be defined as char array: char op[2];
Explanation
%[+-*/] : this means that you expect for %op the character + or - or * or /
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 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.
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.
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.