Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
scanf is a function which takes user inputs. But according to the code in the picture the compiler understands 34 as the value for "y", but it is only the neglected input of x, so can anyone explain this?
Scanning for the %2d directive stops when the two digits 12 have been read. That leaves "3" as the next character in the input stream.
Then the space character in the format string says to skip white space characters (spaces, tabs, and so on), if there are any. There are not, so “3” remains the next character in the input stream.
Then the %d specifier causes a decimal integer to be read, so 34 is assigned to y.
If you wish to skip the digits remaining after x is scanned, you can use %*[0-9] to scan and ignore digits until a non-digit is seen., as in %2d%*[0-9] %d %*f %5s.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
So I have a string char *str = someString(); and I want to print the string and retain any formatting that may be present in the string with printf("%s", str); So for instance, if str were equal to "\033[32mPassed!\n\033[0m", I would want it to print Passed! in green followed by a new line. But currently, it prints the string literally.
Is this something printf can do or is it not designed for this? I understand that this could cause issues if the string contained something like %d without actually having a number passed.
Sending ␛[32mPassed!␊␛[0m to the terminal is what causes the desired effect.[1]
You are asking how convert the string "\033[32mPassed!\n\033[0m" into the string ␛[32mPassed!␊␛[0m, just like the C compiler does when provided the C code (C string literal) "\033[32mPassed!\n\033[0m".
printf does not provide a way to convert a C string literal into the string it would produce.
And nothing else in the standard library does either. The functionality of parsing C code is entirely located in the compiler, not in the executable it produces.
You will need to write your own parser. At the very least, you will need to do the following:
Remove the leading and trailing quotes.
Replace the four character sequence \033 with character 0x1B.
Replace the two character sequence \n with character 0x0A.
Footnotes
Assuming the terminal understands these ANSI escape sequences.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an array like this
int numbers[]={5,6,5,8,9,1,-6516,8,811,981,981};
and I need to print them to screen with spaces in front of them so the total number of characters printed will be 4. si the number 5 will be printed as 3 spaces and 5 5 the number 811 will be 811 and so on.
As was previously mentioned in comments, this is something you can do with printf. I'm reluctant to write the code because a) it looks like homework and b) you'll have this nailed once you've read up on printf. (You'll need to put that printf in a for-loop to go thru the array, for sure, so read up on for as well if you need to.)
Some good resources for printf are whatever textbook you're using, plus
https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
https://www.dummies.com/programming/c/how-to-format-with-printf-in-c-programming/
http://www.cplusplus.com/reference/cstdio/printf/ (a reference, not a tutorial)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if someone knows how to allow a user to input both string(char) and integers at the same time where each character of the input is verified. An example of the input would be "05JK1010". I’ve only started studying the C language last week so I only have this figured out
char studentID [20];
printf("Enter your student ID:\n");
scanf("%s", &studentID );
You're close but I think I have a better way of doing this. GNU libc's scanf accepts ranges with its %[ format, like this:
scanf("%19[0-9a-zA-Z]", studentID);
You should put a different size in place of 19 if you resize your array. This is in place to ensure that scanf doesn't overflow the buffer.
Also, you generally shouldn't take the address of one of scanf's parameters if the format specifier for that parameter is %s or %[.
If you require a portable solution, then it's a mouthful:
scanf("%19[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]", studentID);
For more details about scanf, see my post What can I use for input conversion instead of scanf?
C doesn't have a facility to allow integers and chars to be input at the same time.
Like Joshua commented above characters are just integers represented as ASCII symbols.
If you need the numbers entered to be in numerical integer form, you would have to parse them from the string and use various methods to convert them to there numerical value.
C does have a function that will convert ASCII to integer atoi()
http://www.cplusplus.com/reference/cstdlib/atoi/
PLEASE note, atoi() dose not have error checking, this is just to give you ideas.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a data file with information like this:
3 10.9
1 2.1
10 100.5
//This is a blank line
10 200
The first is an integer and the second is a float data. It also needs to check whether a blank line exist. So I use a float x[20] array to contains it and use fgets() to get the value of each line. But how can I get back these values as printf("%d%f",x[0],x[1]); can't get back the value I wanted, it gives some strange values.
Use
fgets(buffer, sizeof buffer, filehandle);
Then use
if (sscanf(buffer, "%d %f", &Intvar, &floatvar) == 2)
// Data ready
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Can someone explain me this format in C language?
fscanf(file_name,"%20[^\n]%*20c%ld%*c%d%*16c",name,&idE,&cod);
I don't understand what %20[^\n] means.
As noted in the comments, the %20[^\n]scan set conversion specification means that the code will read not more than 20 characters that aren't a newline, null terminating the string (using the 21st byte if necessary). The scan set conversion is complete at the ] — there are special rules when you need to include ] in the scan set. See the POSIX scanf() manual for the full details.
Note that there are various scenarios. First, the scan set won't skip leading white space (and it plus %c and %n are the only three conversion specifications that don't skip white space).
Suppose that the next character is a newline: the conversion fails because there must be at least one character that matches for it to succeed.
Suppose instead that the next few characters are not newlines, but there is a newline before 20 characters are read. Those characters will be read into the string, which will be null terminated.
Alternatively, suppose that the next few characters are not newlines, but there is a newline (immediately) after the 20th character. There will be 20 characters in the string that's read, plus a null terminator, and the newline will be processed by the next part of the format string. In the question, that's %*20c, which means "read 20 characters, including white space, but do not assign to any variable". If, instead of %*20c, the character was c, then the match of the c would fail; the next character is a newline. You'd know because scanf() would return just 1, not 2 or more.
The other alternative is that there are more than 20 non-newlines to read; 20 of them will be saved into the variable associated with the scan set, and the following characters will be matched (or not) by the subsequent characters or conversion specifications in the format string.