Menu
  • HOME
  • TAGS

Why passing string to scanf() compiles fine in C?

c,scanf,undefined-behavior

The code is behaving correctly. Indeed, scanf is declared int scanf(const char *format, ...); Luckily, your format does not contain any % for which there would be no correspondence in ..., which would invoke UB. Further, format is a string literal which allows the compiler to go through it ensuring...

How to introduce string by keyboard in child after fork

c,concurrency,process,signals,scanf

Let me see if I got it right, your problem is that child process is failing to read stdin, right? You may use fscanf(stdin, stringa); I've tested with a simple example and worked (just changing your scanf by fscanf). Let me know if that was your problem....

How do you type something on the same line of text in C [closed]

c,printf,scanf

From your tags, I'm guessing you're using printf(). Simply leave out the "\n" character, which means "newline.". In other words. printf("> "); printf("text\n"); This will print: > text ...

Why isn't scanf( ) waiting for input from keyboard?

c,printf,structure,scanf,fflush

The C11 standard explains how %c works: §7.21.6.2/8 Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. As such, the linefeed produced by the enter key is consumed by %c. You can fix this by adding a space...

getchar() not working in c

c,while-loop,char,scanf,getchar

That's because scanf() left the trailing newline in input. I suggest replacing this: ch = getchar(); With: scanf(" %c", &ch); Note the leading space in the format string. It is needed to force scanf() to ignore every whitespace character until a non-whitespace is read. This is generally more robust than...

How to break a string, that was read using fgets(), using scanf(“%s”,..)

c,buffer,scanf,fgets

You can use sscanf() to advance through a buffer read by fgets() %n will receive the number of characters used by the scan. Adding used to the offset will advance the scan through the buffer. int offset = 0; int used = 0; char line[1000]; char item[200]; fgets ( line,...

How to scanf to just one dimension of a multidimensional array in C?

c,arrays,loops,multidimensional-array,scanf

You want something like this. int i=0; do{ scanf("%d", &mat[i][1]); i++; }while (getchar() != '\n'); ...

Printing an int value obtained from user

c,printing,int,scanf,value

Here is your problem. char gender; scanf(" %s",&gender); gender is a char. That is, it only has memory for a 1 byte character. But you are using it as a string. You probably have the same problem for name1 since you are using & for that as well but can't...

Replacing gets with scanf in simple equation crashes the program

c,scanf,atoi

Why spend time converting the answer when scanf() can do it for you? #include <stdio.h> int main(void) { float height_in_inches; printf("Enter your height in inches: "); if (scanf("%f", &height_in_inches) == 1) { float height_in_cm = height_in_inches * 2.54; printf("You are %.2f inches or %.2f centimetres tall.\n", height_in_inches, height_in_cm); } else...

scanf in a while loop reads on first iteration only

c,scanf

You need to use int c; while((c=getchar()) != '\n' && c != EOF); at the end of the while loop in order to clear/flush the standard input stream(stdin). Why? The answer can be seen below: The scanf with the format string(" (%d,%d)\n") you have requires the user to type An...

End while loop with ctrl+d, scanf?

c,while-loop,printf,scanf,eof

The biggest problem with your program is that scanf will not read an EOF into a variable. However, fixing just this problem is not going to make your program entirely correct, because there are other issues in your code: Your code repeats itself - when possible, you should unify the...

Simple Unit Conversion Program - scanf issues with do-while loop (C programming)

c,while-loop,scanf

Two advices when using scanf (especially for numbers): 1) use fflush(stdin); before each call of scanf; 2) check the result of unputs - is number received or not? For example, if you cannot work without correct input: do{ printf("Please, enter number: "); fflush(stdin); } while( 1 == scanf("%d", &number)); ...

Specifing the maximum string length to scanf dynamically in C (like “%*s” in printf)

c,io,size,buffer,scanf

Basic answer There isn't an analog to the printf() format specifier * in scanf(). In The Practice of Programming, Kernighan and Pike recommend using snprintf() to create the format string: size_t sz = 64; char format[32]; snprintf(format, sizeof(format), "%%%zus", sz); if (scanf(format, buffer) != 1) { …oops… } Extra information...

scanf issue when reading double

c,gcc,double,user-input,scanf

In your code, change scanf("%lf \n", &radius); to scanf("%lf", &radius); Otherwise, with a format string having whitespace, scanf() will behave as below (quoted from C11, chapter §7.21.6.2, paragraph 5) A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or...

Checking input types with scanf() in a while loop [duplicate]

c,scanf

int check = scanf("%d", &x); does not consume the "input is a character not a number", leaving that input in stdin for the next input function. Since the next input function is check = scanf("%d", &x);, it does not consume the offending data and so the cycle repeats. Code needs...

Shortening strings in C

c,string,scanf

Code has input and output issues; char home[15]="HOME"; ... printf("Do you want to enter teams' names?\n1-Yes\n2-No\n:>>"); scanf("%d",&choice1); ... printf("\nPlease enter HOME team's name(max 15 characters including space)\n:>>"); scanf(" %[^\n]s",&home); ... Input1: The 's' in " %[^\n]s" is certainly not needed. 's' is not part of the "%[^\n]" format specifier. Input2:...

What does a # sign after a % sign in a scanf() function mean?

c,input,printf,scanf,format-specifiers

TL;DR; - A # after a % sign in the format string of scanf() function is wrong code. Explanation: The # here is a flag character, which is allowed in fprintf() and family, not in fscanf() and family. In case of your code, the presence of # after % is...

Why does inputting an unexpected value type via scanf() cause this program to enter an infinite loop

c,types,buffer,scanf

Because the scanf function will only extract the input if it's correct. If the input is not correct, the input will still be in the input buffer when the loop iterates, and the next call to scanf will read the exact same input. You might want to either use the...

Strange C++ behavior. Value gets overriden

c++,scanf,undefined-behavior

Your scanf to read in op is invalid. op is a single char, so you should read with scanf("%c",&op); Not scanf("%s",&op); ...

What does %[^<] (and friends) mean in the formatted string family?

c,string-formatting,scanf

From the C11 standard document, chapter §7.21.6.2, Paragraph 12, conversion specifiers, (emphasis mine) [ Matches a nonempty sequence of characters from a set of expected characters (the scanset). .... The conversion specifier includes all subsequent characters in the format string, up to and including the matching right bracket (]). The...

In C, Why does my float print out as 0 even though I input 13.5 for it using scanf?

c,arrays,floating-point,printf,scanf

Just change this: float price[10000]; to this: float price; Because you use it as a single variable and not as an array...

Simple scanf does not set variable value

c,scanf

For var4, you declare it as unsigned char, but read it using a %d format specifier, which invokes undefined behavior. I would recommend using %hhu as the format specifier for that field, if your compiler supports it. Or you could declare var4 as an int, and then assign it to...

Why can't I input values?

c,visual-studio-2013,scanf

Try this if (scanf_s("%99s", name, _countof(name)) == 1) printf("Your Name is: %s", name); Two things scanf_s() is a buffer overflow safe function, and it expects a length argument for "%s" specifier. You should only proceed to printf() if you actually succeeded scannig the value, for which the check (scanf(...) ==...

Why does my program accept one integer too many and input one too few?

c,arrays,scanf

Your loops should be for (count=0;count<SIZE;count++) Array indexing is 0 based in C. Since you have a whitespace chatacter (\n) in scanf() call, it waits for you input a non-whitespace character to complete each call. Remove the \n: for (count=0;count<SIZE;count++){ scanf("%d",&myArray[count]); } ...

How to use scanf for a char array input?

c,arrays,scanf

scanf() with format specifier %s scans the input until a space is encountered and in your case what you are seeing is undefined behavior your array can hold 10 char's but you are writing into array out of bounds and still getting desired answer which is not guaranteed and might...

scanf doesn't work (integer)

c,scanf

Your scanf is perfect. I think that your mistake is the loop for from esPrimo. Actually you have an infinite loop, because sqrt(n) has always the same value and it isn't a boolean expression. Change your loop: for(divisor = 2; sqrt(n); divisor++) { if(n <= 0) { return falso; }...

skipped trouble with getchar and scanf

c,ubuntu,scanf,getchar

The scanf command does not consume the Enter key that you pressed after entering y. So the getchar() happily consumes it. One solution is to consume the rest of the input line after reading y; the code for that looks like: int ch; while ( (ch = getchar()) != '\n'...

Trouble with delimiting colon with scanf

c,scanf

It seems the formatting is wrong, you want to read the colon and there are no commas. Try this: scanf("%m[^':']:%m[^':']", &str, &i); Hope this helps....

Why value of second variable is irrelevant after adding space or new line?

c,scanf

Because 215- 15 can only match one number: 215. As soon as scanf() reads a -, it stops processing the first match since - can't possibly be a digit of the current number, so num1 is matched with 215. Then, no more numbers can match, because you are left with...

scanf get multiple values at once

c,char,segmentation-fault,user-input,scanf

I'm not saying that it cannot be done using scanf(), but IMHO, that's not the best way to do it. Instead, use fgets() to read the whole like, use strtok() to tokenize the input and then, based on the first token value, iterate over the input string as required. A...

Parse string separated with semicolon using a regex

c,scanf

scanf doesn't support regular expressions. It supports strings of a given set of characters. When your format contains %[^';'] it matches any sequence of one or more characters except ' and ;. When your format contains a comma (,) it matches a comma. So when you say: sscanf(multiple, "%[^';'], %[^';'],...

Add and print multiple char in C [closed]

c,char,int,add,scanf

Judging by this statment: where the user rolls 3 dice and gets some random outputs (up to integer 6). My next step is to add those 3 values obtained and get its sum I assume you want this: first initialize sumDice to 0: int sumDice=0; then, in every for loop...

Print each word in a separate line from an input string

c,string,char,printf,scanf

In your code, printf("%s", s[i]); is wrong. Change it to printf("%c", s[i]); as, you're trying to print a char value. The conversion specifier for a char is %c. Note: Always remember, using wrong conversion specifier will lead to undefined behaviour. Also, while scan()-ing with %s, you cannot read the whole...

Storing a string of inputs into a struct in C

c,string,struct,user-input,scanf

You can specify complex formats using scanf, like: scanf("%79[^,],%c,%d,%d", apple.item, &apple.letter, &apple.x, &apple.y); %79[^,] means scan anything that is not a comma character, up to 79 characters. Note that this does no error handling if the user enters a poorly formatted string, like "aaa;b;1;2". For that, you'll need to write...

Program immediately terminates with segfault when using scanf

c,pointers,segmentation-fault,scanf

In your code, stack* st; st->top=-1; you're using st uninitialized which in turn invokes undefined behaviour. You need to allocate memory to st before using it. Try something like stack* st = malloc(sizeof*st); //also, check for malloc success That said, Please see why not to cast the return value of...

How to read input in C

c,scanf

Let us look at this step by step: "... read a line with scanf("%[^\n]");". scanf("%[^\n]", buf) does not read a line. It almost does - sometimes. "%[^\n]" directs scanf() to read any number of non-'\n' char until one is encountered (that '\n' is then put back into stdin) or EOF...

C Programming: How to check if user has entered numeric values only in the following expression?

c,validation,date,exception,scanf

There are a couple of issues. If using scanf check its return. In addition to the numeric validity checks, you also have the issue of needing to flush the input buffer in the event something other than an integer is entered by the user. While there are several ways to...

Segmentation Fault after 5th scanf

c,scanf,sigsegv

assumes sizeof (int) and sizeof (int*) are the same m=(int**)malloc(n*sizeof(int)); Try this m = malloc(n * sizeof *m); ...

Specifying the ']' character in a C/C++ scanf() scanset

c++,c,format,scanf

The trick is to put ']' as the first character in the pattern: scanf(" %80[^]]", &buf); ...

scanf a big hexadecimal value

c,hex,scanf

You must use "%llx" for both scanf format and printf. See the manual page for additional details.

C - Using file as database

c,database,file,scanf

When the format string in scanf_s contains %s or %S, the function expects another parameter that indicates the size of the string. See https://msdn.microsoft.com/en-us/library/w40768et.aspx. Instead of scanf_s("%[^\n]",std.studentNumber); you'll need to use: scanf_s("%[^\n]",std.studentNumber, sizeof(std.studentNumber)); Similar changes need to be to the other calls to the function....

C++ — error C2664: 'int scanf(const char *,…)' : cannot convert argument 1 from 'int' to 'const char *'

c++,scanf

Change scanf('%d', &row); to scanf("%d", &row); '%d' is a multicharacter literal which is of type int. "%d" on the other hand is a string literal, which is compatible with the const char * as expected by scanf first argument. If you pass single quoted %d, compiler would try to do...

Why does command prompt show numbers before I begin?

c,file-io,printf,scanf

Substitute printf("%d",&n); with scanf("%d",&n); printf Writes the C string pointed by format to the standard output (stdout) scanf Reads data from stdin In your code you are printing n, that is not initialized, that a random number is printed out after "Enter numbers to be stored in file" string....

C Wrong Answer when squaring

c,printf,scanf

To add to the above answers ... look at this example: int number = 5; printf(" %d\n", number); // Prints value of 'number'. printf(" %p\n", &number); // Prints address of 'number'. printf(" %d\n", *(&number)); // Prints value at address of 'number' ... ...

Difference between fgets and gets

c,scanf,fgets,gets

Drop gets() and scanf(). Create a helper function to handle and qualify user input. // Helper function that strips off _potential_ \n char *read1line(const char * prompt, char *dest, sizeof size) { fputs(prompt, stdout); char buf[100]; *dest = '\0'; if (fgets(buf, sizeof buf, stdin) == NULL) { return NULL; //...

if statement for string comparison is not executing properly

c,string,input,printf,scanf

Point 1: Use strcmp() for string comparison, not == operator. Point 2: Anyways, for an array of char yourchoice[40]="";, using yourchoice[40] is out of bounds which in turn invokes undefined behaviour. Array index in C starts from 0. Point 3: printf() does not need to have a pointer to data...

Reading from txt file in C using scanf

c,file,scanf,sscanf

sscanf takes first argument of type const char *.You are passing the first argument as FILE *. You can use: fscanf OR fgets with sscanf ...

Why doesn't isdigit() work?

c,debugging,scanf

isdigit() takes the ascii value of a character and returns 0 if it's not a digit and non-0 if it is. You are passing to it an integer value which is not necessarily an ascii value, you don't need to check if it's a digit since you read it with...

Scanning values in C till hit a new-line char, '\n'

c,arrays,algorithm,scanf,getchar

You are trying to read integers as characters so once read you need to convert it to integers. Read the line to a buffer using fgets() then parse the input buffer to get integers. Store the integers to the array. The code looks like char buf[300]; int a[5],i=0; fgets(buf,sizeof(buf),stdin);...

C: Format %s expects argument of type char* in funny strings program

c,string,scanf

Apparently you have a char * and you are passing it's address, which is wrong, scanf() wants a char pointer for each "%s" specifier, and the fix for your code is to use char string[10]; scanf("%s", string); the array automatically becomes a char pointer when passed to scanf() like above,...

Why does scanf ask for input twice, but just in the first loop iteration only?

c,scanf

In your case, scanf("%d ", &a[i]); after scanning an integer, scanf() needs to match the white space, and any number of white space until a non-white space character is required to complete the match. So, for the first time, the second input is the non-white space, which terminates the scan,...

program using the rand () function

c,random,scanf,modulo

Some issues with your code: Point 1: scanf("%d\n",&x); should be scanf("%d",&x); Point 2: for(i=0;i<1;i++) this for loop is practically useless. It only iterates one. either use a longer counter, or get rid of the loop. Point 3: It's better to provide a unique seed to your PRNG. You may want...

scanf takes extra parameter why?

c++,c,scanf

You have a space in your format after the second %i. scanf will read extra data to match the space. Remove the space and it should work as you expected.

Storing scanf values into a 2d string array

c,scanf

scanf returns the number of items scanned. In this case it would be 2 instead of 1. Here a return of 1 would indicate a problem during the scan. The %m specifier allocates memory to the pointers. Using a single pair of pointers, they should be freed in eadh iteration...

Is there any way in C to terminate scanf() inputting in array without ctrl+d?

c,arrays,input,scanf

You can do something like that: for(i=0;a[i]<10000;i++) { // chack that input correct if((scanf("%d",&a[i])==1) { count++; } else // if input is incorrect { // read first letter int c = getchar(); if( c == 'q' ) { break; // stop the loop } // clean the input buffer while(...

Find the lonely integer in an array

c,arrays,pointers,scanf

You'd want to use: #include <stdlib.h> /* ... */ int *arr; scanf("%d", &n); arr = malloc(sizeof(int) * n); This way, arr gets dynamically allocated at runtime, so it can be of any size depending on the input n. What you were originally doing (i.e. declaring arr[n] after recieving n via...

why after looping there's a newline

c,newline,counter,scanf

On the 2nd time through the loop, scanf("%c",&penal1); is scanning in the '\n' from the previous user input. Add a preceding space like code used in other places to consume all preceding white-space. scanf(" %c",&penal1); // added space. Code should check not only for 'O' and 'o', but 'X' and...

Why does scanf returns control back to the program on pressing Enter key?

c,scanf

Most OSes buffer keyboard input so that they can process backspaces properly -- the OS keeps input in a buffer and only gives it to the program when Enter is hit. Most OSes also provide ways to control this, but the way is different for different OSes. On POSIX systems,...

Scanf() working differently with \n and '\n' supplied in format specifier

c,scanf

So you had a scanf("%f'\n'%f'\n'%s",&lati,&longi,info) in your code. When you break this code up: %f - expect a float ' - expect a literal ' in the input \n - expect a line break ' - expect a literal ' in the input ... and so on. \n is a...

scanf() to get in the string on the second time

c,scanf

fflush(stdin); is undefined behaviour in standard C. To flush the newline character, you could simply use getchar() instead. printf("\nEnter staff name \t> "); getchar(); scanf("%[^\n]s", staff_name2); I would also use fgets() instead of scanf to read a line and trim the newline if necessary, which offers better control over invalid...

Why compiler is not issuing error while passing an int (not int *) as the argument of scanf()?

c,scanf,undefined-behavior

As per C11 standard, Chapter §6.3.2.3 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. So, the compiler will allow this,...

C sscanf (fscanf) behaving differently row from row

c,scanf,fscanf,sscanf

Scanning with %i and values with a leading zero are assumed to be octal. 08 and 09 are not octal values. Use %d instead as leading zero's are ignored and values are in base 10. For the sscanf you might try this. It will scan the five items present in...

I'm not able to use scanf() in else statment

c,if-statement,scanf

When you enter the data in the first read of this scanf: scanf("%c", &ms); A newline character remains in the keyboard. To solve this put a space in your second scanf: scanf(" %c",&s); This is to consume any trailing character in the stdin that might have been left by previous...

Getting a char* with spaces in C from sscanf

c,char,scanf

Most scanf() field descriptors implicitly cause leading whitespace to be skipped and expect the field to be whitespace-terminated. To scan a string that may contain whitespace, however, you can use the %[] field descriptor with an appropriate scan set. Thus, you might scan sequence of lines following the pattern you...

How to get each element of a array from user input then pass it to a function in another file in C

c,arrays,function,scanf,elements

The problem is that you are iterating from 1 and you should iterate from 0, in the inner_product() function for( i=1; i<count; i++) { /* ^ this should be 0 */ also, don't use global variables specially because you got the rest of it right, you are passing the arrays...

C program skips a line when asking for user input [duplicate]

c,char,user-input,scanf

Point 1 Do not use fflush( stdin );, it is undefined behaviour. Related: from C11 standard docmunet, chapter 7.21.5.2, (emphasis mine) int fflush(FILE *stream); If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten...

How to read and write files using printf and scanf in C? [closed]

c,file,printf,scanf

You basically have three options. Option I Redirect stdin/stdout (those are the streams scanf reads from and printf writes to) in the console, when you start your program. On both Windows and Linux, this can be done like so: < in.txt - redirects all reads from stdin to in.txt >...

C program loops infinitely after scanf gets unexpected data

c,scanf

When scanf() fails, the argument is not automatically initialized, and uninitialized values could be any value, so it might be less than 2 or greater than 64 no one knows. Try this int initialBase; /* some default value would be good. */ initialBase = 2; do { printf("Initial base: ");...

scanf in while loop only works on first iteration

c,while-loop,scanf

Remove the '\n' left by the previous scanf() like this scanf(" %c %x", &read_size, &address); /* ^ tell scanf to skip white spaces with the %c specifier */ also do not ignore scanf()'s return value, it might cause very weird things in case it fails and you fail to detect...

second scanf to recognize keyword to exit iteration of integers? C

c,scanf

first issue: Try to read to string and then translate the string to integer by using sscanf, if it is not equal to "end". (compare by strcmp not by ==). second issue: when you read numbers, you never reach to c++. the continue make it skip it. for example, you...

scanf int8_t corrupts stack

c++,visual-studio-2013,scanf

You have run into a bug of Microsoft's C/C++ runtime library, cf. http://mfctips.com/tag/format/ or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417 (which reports this bug for gcc under mingw, which links against the Microsft libs). "%hhd" just doesn't work; you must program around it (which isn't too hard, but sad). I am not aware of a...

Why fgets takes cursor to next line?

c,string,cursor,scanf,fgets

The reason printf is outputting a newline is that you have one in your string. fgets is not "adding" a newline --- it is simply reading it from the input as well. Reading for fgets stops just after the newline (if any). Excerpt from the manpage, emphasis mine: The fgets()...

Program blocks after input

c,arrays,gcc,scanf

I tried you code and manage to reproduce the weird behaviour. If you remove the " " around "%li" it doesn't block anymore. The problems were the blank spaces because scanf was expecting the input to match the empty space as well. From the scanf documentation: All conversions are introduced...

Issue with scanf(“%s”)

c++,scanf

I think "\r\n" are not copied when you copy paste the text

How to get several value in once enter ?(Number of uncertainty)

c,scanf

Have you considered doing this using a combination of scanf(), fgets() and strtok()? For example, you can use scanf() to simple query the number of inputs expected. Then use a loop around `strtok() to tokenize user's input: int main() { char line[1024]; char *token = {0}; char delim[]={" \r\n\t"}; long...

Do white spaces take space in txt files?

c,file-io,whitespace,scanf,fscanf

To your first question: No, white space does not need to be considered in fscanf(). To your second question: Neither of these will work. You seem to be misunderstanding how information is stored in a computer. In the program, an integer is 32 bits (4 bytes), so sizeof(int) will always...

Why doesnt getchar() stop reading strings in C?

c,string,input,scanf,getchar

Because you have an operator precendence problem here chr = getchar() != EOF this is evaluated as chr = (getchar() != EOF) because the != operator has higher precendence than the assignment operator =, so you just need to add parentheses like this (chr = getchar()) != EOF Tip: Check...

how to scan a string input

c,arrays,string,segmentation-fault,scanf

In your code, char * n = ""; makes n point to a string literal which is usually placed in read-only memory area, so cannot be modified. Hence, n cannot be used to scan another input. What you want, is either of below a char array, like char n[128] =...

printf after scanf always displays 1 (same unexpected value)

c,printf,scanf

You are assigning the returned value from scanf_s() to the variable n, that means that the program will print 1 in case a successful read happened. What you should do is int numberOfItemsMatched; int readValue; numberOfItemsMatched = scanf_s("%d", &readValue); if (numberOfItemsMatched == 1) printf("%d\n", readValue); I hope the variable names...

Please explain this output of the code

c,function,return,printf,scanf

In the following function: int GetPositiveInt(void) { int n; do { printf("Enter a positive number : "); scanf("%d",&n); }while(n<=0); } the function is expected to return an int value, whereas there is no return statement. You probably wanted to return the value of n. This can be achieved by adding...

scanf crops last byte of numbers

c,scanf

If you read e.g. this scanf reference, you will see that the "%[" format is for reading a string which of course includes writing the string terminator character. Since you only provide a single character to store the read string, the scanf function will always write out of bounds and...

Nonportable Pointer Conversion

c,scanf,turbo-c

The problem is are with Point 1: if(gender == "M") it should be if(gender == 'M') Reason: "M" represents a string, 'M' represents a char. Point 2: scanf("&s", gender); should be scanf(" %c", &gender); Reason: Need to use proper signature of scanf(). %c is the format specifier for scanning a...

How to limit input length with scanf

c,arrays,scanf

As pointed out by @SouravGhosh, you can limit your scanf with "%3s", but the problem is still there if you don't flush stdin on each iteration, you can do it in this way: printf("\n enter the names \n"); for(i = 0; i < 3; i++) { int c; scanf("%3s", name[i]);...

Reading from a CSV file and solving problems with “;”

c,csv,scanf,delimiter

You can change your code fscanf(fp,"%4s %7s %9s %16[^\n] %11[^\n] %2s %5s",... to the style fscanf(fp,"%4s;%7[^;];%10[^;];...... to have the ;s as a part of the format string. Then it won't be considered as a part of the string input. Note: The above method is just a workaround and will break...

How to make fgets() ignore a \n at beginning?

c,string,scanf,fgets

Ok, the original idea does not seem to work, so another try: scanf("%d",&s.rollNumber); printf("Name: "); fgets(s.name, 20, stdin); /* capture the new line */ fgets(s.name, 20, stdin); Original idea: Simply tell scanf to also capture the newline: scanf("%d\n",&s[i].rollNumber); ...

Spaces with scanf in a client to server message. C

c,client,server,scanf

Use fgets() to get your input into a string and sscanf() to evaluate it. Since you just want what the user entered, you don't really need sscanf() in this case anyway See this post...

How does scanf determine whether to block?

c,scanf,io-redirection

scanf is just reading from its input stream. If the input stream is a pipe, and the other end of that pipe is associated with a tty (which is usually the case if you are interactively entering data by pressing keys on a keyboard), scanf will return as soon as...

control scanf() duration for taking any input

c,scanf

#include <windows.h> #include <stdio.h> int main(void){ int num = 0; DWORD waitCode; printf("input number : "); //wait console input 10,000 Millisecond waitCode = WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE) , 10*1000); switch(waitCode){ case WAIT_TIMEOUT: fprintf(stderr, "\n10 seconds are over no more input\n"); return -1; case WAIT_OBJECT_0://normal status scanf("%d", &num);//input from stdin buffer if(num)//not zero printf("input...

Dynamically allocate user inputted string

c,arrays,user-input,scanf,dynamic-memory-allocation

First of all, scanf format strings do not use regular expressions, so I don't think something close to what you want will work. As for the error you get, according to my trusty manual, the %a conversion flag is for floating point numbers, but it only works on C99 (and...

Stop scanf loop if user enters a specific number (Not working) C

c,loops,compare,scanf

if (array[i] == '5') You're checking whether array[i] is equal to the ASCII value of the character '5'. Remove the '' to make it compare against the integer 5....