scanf

C: scanf add to total

A quick question, is there a way to perform these 3 operations: while(...) { fscanf(input, "%lf %lf", &t, &y); tTotal += t; yTotal += y; } in one operation where t and y add themselves to tArray and yArray respectively, inside the scanf statement? Something of the form fscanf(input, "%lf %lf", ...code..., ...code...); Thanks, Ash....

c scanf question

Why do you require ampersand (&) in scanf. What will be the output or type of error (compile or runtime ) in the following c code? #include <stdio.h> void main() { int a; printf("enter integer"); scanf("%d",a); } ...

How does scanf() really work?

On Windows, char c; int i; scanf("%d", &i); scanf("%c", &c); The computer skips to retrieve character from console because '\n' is remaining on buffer. However, I found out that the code below works well. char str[10]; int i; scanf("%d", &i); scanf("%s", str); Just like the case above, '\n' is remaining on buffer but why scanf s...

How do I detect an operator vs. int in C using scanf?

How do I read in the following input in my RPN calculator so that it will find the operator no matter what order? 2 2+ 4 As of now my scanf only sees the first char in the string and I can only do this: 2 2 + 4 I'm also trying to add an option for integer vs floating point mode. (ex. when 'i' is entered, operate in floating point a...

what is scanf("%*s") and scanf("%*d") format identifiers ?

What is the practical use of the formats "%*" in scanf(). If this format exists, there has to be some purpose behind it. The following program gives weird output. #include<stdio.h> int main() { int i; char str[1024]; printf("Enter text: "); scanf("%*s", &str); printf("%s\n", str); printf...

scanf() resetting first result depending of variable declaration order

Why does this not work as expected? int main() { unsigned char louise, peter; printf("Age of Louise: "); scanf("%u", &louise); printf("Age of Peter: "); scanf("%u", &peter); printf("Louise: %u\n", louise); printf("Peter: %u\n", peter); return 0; } Outputs: Age of Louise: 12 Age of Peter: 13 Louise:...

Why scanf doesn't block the thread for input in this case? Objective-C

Hi, This must be very simple but since coming from Java world I feel a bit in the woods in this case. while (operator != 'E') { NSLog(@"Enter:"); scanf("%lf %c", &value2, &operator); switch (operator) { case 'S': [deskCalc setAccumulator:value2]; break; case 'E': break; case '+': [deskCalc add:value2]; ...

ANSI C (ISO C90): Can scanf read/accept an unsigned char?

Simple question: Can scanf read/accept a "small integer" into an unsigned char in ANSI C? example code un_char.c: #include <stdio.h> #include <stdlib.h> int main(void) { unsigned char character; scanf("%hhu", &character); return EXIT_SUCCESS; } Compiled as: $ gcc -Wall -ansi -pedantic -o un_char un_char.c un_char.c:...

anomalous behaviour of scanf

hello, wondering all about C, can you demystify this I am using turbo C I have this code scanf(“%d , %d”,&a,&b); printf(“%d,%d”,a,b); scanf(”%c”,&c); printf(“%d,%d”,a,b); then scanf for doesnt scan value of c output is : 1,2 if I use this scanf(“%d , %d”,&a,&b); printf(“%d,%d”,a,b); scanf(”%c ”,&c);//note a blank after %c prin...

can scanf be terminated on pressing some specific key other than enter

Hello, I have a situation here i am taking input from user using scanf can I terminate the scanf as soon as user presses the # key please enlighten me on this ...

Streaming structured text input

I'd like to parse formatted basic values and a few custom strings from a TextReader - essentially like scanf allows. My input might not have line-breaks, so ReadLine+Regex isn't an option. I could use some other way of chunking text input; but the problem is that I don't know the delimiter at compile time (so that's tricky), and that ...

getting rid of sscanf() warnings

Hi! I'm reading MAC addresses (in standard hex notation, e.g. 00:11:22:33:44:55) from stdin and converting them into a 6 byte variable hw_addr as decimals: u8 hw_addr[6]; scanf("%2x:%2x:%2x:%2x:%2x:%2x", &hw_addr[0], &hw_addr[1], &hw_addr[2], &hw_addr[3], &hw_addr[4], &hw_addr[5]); The only problem is that I'm getting 6 scanf warning...

scanf in while loop

#include<stdio.h> #include<unistd.h> int main() { int i = 1; if(!fork()) { while(i) { printf("Enter i"); scanf("%d",&i); fflush(stdin); fflush(stdout); } } ...

Dynamic String Input - using scanf()

I am trying to read input using scanf and storing into char * dynamically as specified by GCC manual. But it is giving compile time error. char *string; if (scanf ("%as",&string) != 1){ //some code } else{ printf("%s\n", *string); free(string); //some code } Edit: scanf("%ms") also works.(see answers) ...

Why doesn't scanf work when using the Win32 ConsoleInput function?

Hi! This is the example code, I'm using these functions for a program, but scanf doesn't work well: It doesn't display my input and accepts the input only after the enter key is pressed twice. this is the code: #include <stdio.h> #include <windows.h> char c[25]; void KeyEventProc(KEY_EVENT_RECORD); void KeyEventProc(KEY_EVENT_RECORD k...

Input in C. Scanf before gets. Problem.

Hi! I'm pretty new to C, and I have a problem with inputing data to the program. My code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); printf("Input your name: "); gets(b); printf("---------"); printf("Name: ...

Disadvantages of scanf

Hi all, I want to know what disadvantage of scanf() exist. In many of a sites I have read that using scanf will cause buffer overflow some times. What is the reason for that, and is there any other drawbacks with scanf? ...

Using an ampersand in scanf()

When I compile scanf("%s", &var);, gcc sends back a warning: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’ however when I compile scanf("%s", var);, no warning is applied. Both pieces of code work and the book I am reading specifically says to use the ampersand, but even it doesn't in some of the e...

Ignore carriage returns in scanf before data.... to keep layout of console based graphics with conio.h

I have the misfortune of having use conio.h in vc++ 6 for a college assignment, My problem is that my graphic setup is in the center of the screen... e.g. gotoxy( getcols()/2, getrows()/2); printf("Enter something"); scanf( "%d", &something ); now if someone accidentally hits enter before they enter the "something", then the cur...

C - equivalent of .NET Console.ReadLine

I need to accomplish the same behavior as .NET Console.ReadLine function provides. The program execution should continue when the user pushes enter key. The following code is not sufficient, as it requires additional input: printf ("Press Enter to continue"); scanf ("%s",str); Any suggestions? ...