scanf

Is getchar() equivalent to scanf("%c",&a)?

Is getchar() equivalent to scanf("%c",&a);? Is putchar(c) equivalent to printf("%c",a); where a is a char variable? ...

getc Vs getchar Vs Scanf for reading a character from stdin

Of the below three functions: getc getchar & scanf which is the best one for reading a character from stdin and why? Are there any known disadvantages or limitations for any of these functions which makes one better than the other? ...

Scanf with Signals

I have a signal that blocks SIGINT and basically says "Sorry, you can't quit.\n" The issue is this can occur during a scanf. When this occurs during a scanf, scanf takes in the printf as input. How can I do a printf that will cause scanf to basically hit the enter key automatically. I don't care that I am getting bad input. I just wan...

C, reading multiple numbers from single input line (scanf?)

Hi, I'm writing an app in pure C which expects two line at input: first one, which tells how big an array of int will be, and the second, which contains values, separated by space. So, for the following input 5 1 2 3 4 99 I need to create an array containing {1,2,3,4,99} Now, my question is - what is the fastest (yet easy ;)) way to do ...

scanf segfaults and various other anomalies inside while loop

while(1){ //Command prompt char *command; printf("%s>",current_working_directory); scanf("%s",command);<--seg faults after input has been received. printf("\ncommand:%s\n",command); } I am getting a few different errors and they don't really seem reproducible (except for the segfault at this point >.<). This code wo...

string holding text every \n

I tried to take input in form of string, specifically: int i=0; char c[50][500]; for(;i<50;++i) scanf("%s",A[i]); The input is x is a website that allows the easy[1] creation and editing of any number of interlinked web pages via a web browser using a simplified markup language or a WYSIWYG text editor. In my program, I wan...

Objective-C scanf spaces issue

I am learning objective-C and for the life of me can't figure out why this is happening. When the user inputs when the code is: scanf("%c %lf", &operator, &number); For some reason it messes with this code: doQuit = 0; [deskCalc setAccumulator: 0]; while (doQuit == 0) { NSLog(@"Please input an operation and then ...

How to prevent users from inputting letters or numbers ?

Hello, I have a simple problem; Here is the code : #include<stdio.h> main(){ int input; printf("Choose a numeric value"); scanf("%d",&input); } I want the user to only enter numbers ... So it has to be something like this : #include<stdio.h> main(){ int input; printf("Choose a numeric value"); do{ scanf("%d",&input); }whi...

Program repeats each time a character is scanned .. How to stop it ?

Hello there, I have a program that has this code : #include<stdio.h> main(){ int input; char g; do{ printf("Choose a numeric value"); printf(">"); scanf("\n%c",&input); g=input-'0'; }while((g>=-16 && g<=-1)||(g>=10 && g<=42)||(g>=43 && g<=79)); } It basically uses ASCII manipulation to allow the program to ...

Scan Numbers among letters in a Sentence

Hello, I have a pretty easy question. (using C) In a sentence such as In this document, there are 345 words and 6 figures How can I scan 345 and 6 while ignoring all that is in between ? I tried fscanf(FILE *pointer,"%d %d",&words,&figs); But it only gets the first value ... What am I doing wrong ? EDIT Im sorry I forgot to ment...

Simplest way to use isdigit and isalpha commands ?

Hello, Can anyone briefly explain how do these two commands work ? isdigit and isalpha .. Ofcourse I read online sources before asking the question, but unfortunately I tried them and didnt get them to work. What is the simplest way ? I know it gives back a value, so im assuming I can use it like this : if(isdigit(someinput)==1) r...

using scanf in C/C++

To read an int using scanf we use: scanf("%d",&i); What if i is long not int?? Note: when using %d with long it gives me an irritating warning.. Thanks! ...

reading a string with spaces with sscanf

For a project I'm trying to read an int and a string from a string. The only problem is sscanf appears to break reading an %s when it sees a space. Is there anyway to get around this limitation? Here's an example of what I'm trying to do: #include<stdio.h> #include<stdlib.h> int main(int argc, char** argv) { int age; char* buff...

fgets instructions gets skipped.Why?

Whenever I do a scanf before a fgets the fgets instruction gets skipped. I have come accross this issue in C++ and I remember I had to had some instrcution that would clear the stdin buffer or something like that. I suppose there's an equivalent for C. What is it? Thanks. ...

Assigning each digit of an int to an int array

Is it possible to retrieve an integer using scanf and assigning each digit to a int array? I'm trying to achieve it doing it this way: int numbers[]; puts("Enter number"); int x; scanf("%d",x); numbers = malloc(x); numbers = x; ...

How to read in a negative double with scanf() in C?

I'm learning basics of C and writing a simple first order equation solver. I want the input to be exactly ax+b=c or ax-b=c, where a, b, c are double type. I'm employing scanf() to read in user input and to check if it's of the correct form. However, if I enter a negative double, -4.6 say, as the "a" in the equation, scanf() won't read t...

Using scanf in a while loop

Probably an extremely simple answer to this extremely simple question: I'm reading "C Primer Plus" by Pratta and he keeps using the example while (scanf("%d", &num) == 1)... Is the == 1 really necessary? It seems like one could just write: while (scanf("%d", &num)) It seems like the equality test is unnecessary since scanf returns...

Magic of scanf() function?

Why run this code and print whole string? #include <stdio.h> void main() { int a; while(a!='q') { scanf("%c",&a); printf("%c",a); } } Enter string except q, and finally press enter key. Only now your string will print on the screen. Why? ...

why above function doesnot take below input

void get_formula_and_intervals() { int i=0; while(1) { if(scanf("%c",&(infix_formula[i]))!=1) { infix_formula[i]='\0'; break; } if(infix_formula[i]==' ') continue; ...

if one complains about gets(), why not do the same with scanf("%s",...)?

From man gets: Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fg...