scanf and entering characters [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 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.

Related

Error in accepting the value from console in c? [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.
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>

enter an arithmetic operation (ex. 5*4) in c [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.
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 /

How to print using scanf() function in C programming? [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.
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.

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.

in c printf() returns what [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.
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.

Resources