scanf

function takes only half of the inputs

/*-> struct sam set_of_data[4] -<*/ int main (void) { int k = 0; for(i = 0; i < 4; ++i) { char nm; double thelow, theupp; double numbers[200]; scanf("%c %lf %lf", &nm, &thelow, &theupp); for (k = 0; scanf("%lf", &numbers[k]) != 0; ++k) ; set_of_data[i] = co...

Scanf fails with bus error

I'm playing with C and I've run into this error: #include <stdio.h> int main () { char* foo; scanf("%s", foo); printf("entered %s", foo); return 0; } scanf takes pointer, foo is pointer, yet I get bus error. How can I make it work? ...

Newb Question: scanf() in C

So I started learning C today, and as an exercise i was told to write a program that asks the user for numbers until they type a 0, then adds the even ones and the odd ones together. Here is is (don't laugh at my bad style): #include <stdio.h>; int main() { int esum = 0, osum = 0; int n, mod; puts("Please enter some number...

scanf() (C Language ) confused me

Hello. When do I need to insert/don't insert & for scanf() in C? Thank you. int main() { char s1[81], s2[81], s3[81]; scanf("%s%s%s", s1, s2, s3); // If replace scanf() with the expression below, it works too. // scanf("%s%s%s", &s1, &s2, &s3); printf("\ns1 = %s\ns2 = %s\ns3 = %s", s1, s2, s3); return 0; } ...

SegFault after scanf?

#include <stdio.h> #define TimeConverter 60 #define TempFormula time * time * 4 / time + 2 - 20 double HoursMinToTime(int hour, int min); double Temperature(double time); int main() { int hour, min; double time, temperature; printf("Hours and minutes: "); scanf("%d %d", hour, min); //Segfault HERE time = HoursM...

Using scanf with NSStrings

I want the user to input a string and then assign the input to an NSString. Right now my code looks like this: NSString *word; scanf("%s", &word); ...

scanf / field lengths : using a variable / macro, C/C++

How can I use a variable to specify the field length when using scanf. For example: char word[20+1]; scanf(file, "%20s", word); Also, is it correct to use 20+1 (since it needs to add a \0 at the end?). Instead, I'd like to have something like: #define MAX_STRING_LENGTH 20 and then char word[MAX_STRING_LENGTH+1]; scanf(file, "%"MAX...

Width as a variable when using fscanf [C++]

I am trying to read in a certain portion of a file and that amount of data is different per line but I know how how many bytes of info I want. Like this: 5bytes.byte1byte2byte3byte4byte5CKSum //where # of bytes varies for each line (and there is no period only there for readability) Actual data: 05AABBCCDDEE11 03AABBCC22 04AABB...

fscanf multiple lines [c++]

I am reading in a file with multiple lines of data like this: :100093000202C4C0E0E57FB40005D0E0020C03B463 :1000A3000105D0E0022803B40205D0E0027C03027C :1000B30002E3C0E0E57FB40005D0E0020C0BB4011D I am reading in values byte by byte and storing them in an array. fscanf_s(in_file,"%c", &sc); // start code fscanf_s(in_file,"%...

Unable to find bug in this C program.

I'm unable to find bug in this C program. #include <stdio.h> int main() { struct book { char name ; float price ; int pages ; } ; struct book b[3] ; int i ; int k; for ( i = 0 ; i <= 2 ; i++ ) { printf ( "\nEnter name, price and pages: " ) ; k = scanf ( "%c %f...

Printf/scanf replacement methods: some feedback/advice needed.

I've recently been writing some basic command-line programs (I want to keep my skills sharp over the summer), but printf and scanf have been starting to annoy me. I'm not a wonderful C programmer, and having to get into printf/scanf and their instabilities (or even worse, fgets and their ilk) isn't exactly putting me in a comforting sett...

confusion in scanf() with & operator

why do we need to put a & operator in scanf() for storing values in an integer array and not while storing a string in a char array? i.e int a[5]; for(i=0;i<5;i++) scanf("%d",&a[i]); but char s[5]; scanf("%s",s); ? we need to pass in the address of the place we store the value,since array is a pointer to first element so in the case wi...

Problem using Scanf?

Why does scanf give a max value in case of "int" but crash the program in case of "char" when the limit is exceeded? #include<stdio.h> main(){ int a; char ch[10]; scanf("%d",&a); printf("%d",a); scanf("%s",ch); printf("%s",ch); } ...

Behaviour of scanf()

Possible Duplicate: confusion in scanf() with & operator Why we need a & in scanf for inputting integer and why not for characters. Do the & in scanf refers to merory location while getting input. Eg:- main() { int a; char c; scanf("%d",&a); scanf("%c"c); } ...

Input Puzzler in C

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc) int main() { int a=5,s; s=++a + ++a; printf("%d",a); printf("%d",s); } output is 7 and 14 BUT int main() { int a, s; printf("Enter value of a"); scanf ("%d",&a); s=++a + ++a; printf("%d",a); printf("%d",s); } input user give...

Problem with scanf() on mac

Hi! I compile a C library on mac os x. When I typed the input and after printing on the screen the data I don't see something. char *path = NULL; peerdisplay = "Bob"; printf("Insert the full 'To' path: "); scanf(" %a[^\n]", printf("A path: %s \n", When I replace the %a to %s, the printing ok, but after the running I have a segment...

C getchar vs scanf

I am confused by a piece of code found in a function I am studying: char GetCommand( void ) { char command; do { printf( "Enter command (q=quit, n=new, l=list): " ); scanf( "%c", &command ); Flush(); } while ( (command != 'q') && (command != 'n') && (command != 'l') ); printf( "\...

scanf not terminating

So, I've got the following code (C): char c = getc(in); if (c == '(') ... if (c == '%') ... if (isdigit(c)) { int n; ungetc(c, in); scanf("%i", &n); ... } Everything is all fine and dandy when I'm reading in input from stdin but, when reading in input from from a file, the call to scanf does not terminate. I added s...

Escaping "white space" escaping in C

I'm it really sure how to explain this question but i'll give it a try. I have this line of code in my program: scanf (" %c", &character); Notice the space before %c. This is supposed to keep the scanf from interpreting the last ENTER key as a character. This works, however after this line and its resulting printf output the program w...

Why is scanf returning 0.000000 when it is supplied with a double?

I have the following assembly code (written for NASM on Linux): ; This code has been generated by the 7Basic ; compiler <http://launchpad.net/7basic&gt; extern printf extern scanf SECTION .data printf_f: db "%f",10,0 scanf_f: db "%f",0 SECTION .bss v_0 resb 8 SECTION .text global main main: push ebp mov ebp,esp ...