This question already has answers here:
What does "%.*s" mean as a format specifier in printf?
(7 answers)
Closed 9 years ago.
This might be a very basic question to many of you, but I am not able to understand
what %.*s doing?
void substring(int i, int j, char *ch)
{
printf("The substring is: %.*s\n", j - i, &ch[i]);
//what is %.*s doing?
}
The * is taking the length limit for the string from the argument before the string. So the printf will output (at most) j - i characters from &ch[i] to stdout. If the string is shorter, then the entire string will be printed, but it will not be blank padded.
Here is a good reference for printf: http://en.cppreference.com/w/c/io/fprintf.*
And this is what it says:
. followed by integer number or * that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. See the table below for exact effects of precision.
And for s, it says:
Precision specifies the maximum number of bytes to be written.
So in your case, it prints a maximum of j-i characters.
* In fact, it's a very good reference for just about all standard C and C++ libraries. Use it!
Related
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 5 years ago.
So i really want this to happen. I have an integer variable and i want to use that variable to give spacing in my printf function but C doesn't give me permission to do that , is there any way around it.
#include<stdio.h>
int main(void){
int s = 5;
printf("%sd",s);
}
Thanks a lot in advance!
The * in a format specifier means "I'll pass this number as an argument."
int s = 5;
printf("--%*d", s, s);
output
-- 5
Nice page with details here https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
Using * for the width will cause printf to take the width from the next argument, which should have type int:
printf("%*d", s, ValueToBePrinted);
You can find information about printf and other C features in the C standard. In the C 2011 version, formatted I/O is covered in clause 7.21, about <stdio.h>. 7.21.6.1 discusses fprintf, which uses the same format syntax as printf and is referred to from the printf clause, 7.21.6.3. Paragraph 5 discusses using * for a field width.
What do you mean by spacing here ? You can simply put ‘\t’ before ‘%d’ to give a tab before printing the value of the variable. Also ‘%sd’ doesn’t mean anything. Here %d is replaced by the value of your variable stated after the comma, ie, in your case it’s variable s.
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 6 years ago.
In C, as I understand so far, this would be correct:
printf("%10s\n", "This is C");
would return:
" This is C!"
(with the intentional space prior to the string; no quotations).
My question is can you replace the 10 specifying the length of the print using a variable? If so, how?
That's how:
printf("%*s\n", 10, "This is C");
The format changed from %10s to %*s. The printf() now would expect among the argument, before the string, an int with the width to pad the string to (the 10 in the example above; obviously could be a variable too).
To tell the printf() to pad the output to the left (instead of the default right) use the -: %-*s. (The output would change from " This is C" to "This is C ".)
To tell the printf() to take only few first bytes from the string, or if the string is not null-terminated, you can add .* to the format at the same place as the precision for floating point types. The printf() would print up to that number of characters, stopping at the first null character. Example:
int width = 10;
int chars = 4;
printf( "%-*.*s", width, chars, "This is C" );
would produce output "This ".
This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
I just encountered this by looking in the standard:
7.19.6.1 The fprintf function
in
8 The conversion specifiers and their meanings are:
regarding to:
n The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined.
What does this mean? What does %n do?
Did I get it correct, that acording to:
Returns
14 The fprintf function returns the number of characters transmitted
In this snippet:
int a, b;
b = printf ("Thi%n\s is just a test",&a);
a would equal to b?
the number of characters written to the output stream so far
"So far", means wherever you place your %n, the result will change. As of your example, it will be 3.
If your increase your %s position by one char, the resulting variable pointed will increase by one. Placing your %s at the very end of the string will make it equal to the value returned by printf
a = 3 and b = 19 for your case
a will be equal to number of character printed before %n.
Suppose you try to print printf ("This%sis%n just a test","coder", &a);
Then the value of a will be this + coder + is = 11.
And the value of b is always the total number of characters printed
This question already has answers here:
Reading a character with scanf_s
(3 answers)
Closed 3 years ago.
I'm trying to use scanf_s() to read in multiple values but every time I run the program, I get
Unhandled exception at 0x592AD6AC (msvcr120d.dll) in lab 2.exe: 0xC0000005: Access violation writing location 0x00000000.
in a popup window. How do I fix this?
float inTemp;
char inUnit;
char outUnit;
printf("Please enter the starting temperature with its units and the units\nyou would like to convert to (i.e. 74.5 F C): ");
scanf_s("%f %c %c", &inTemp, &inUnit, &outUnit); //takes in all user input (NOT WORKING)
You have not provided a length indicator. From MSDN:
Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size
to be specified for all input parameters of type c, C, s, S, or string
control sets that are enclosed in []. The buffer size in characters is
passed as an additional parameter immediately following the pointer to
the buffer or variable. For example, if you are reading a string, the
buffer size for that string is passed as follows: char s[10];
scanf_s("%9s", s, _countof(s)); // buffer size is 10, width
specification is 9
Here is a link to that page.
If you really want to use (less portable) scanf_s, from the C11 standard (n1570), Annex K 3.5.3.2 p.4:
The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair.
You need to give the lengths of your char * arguments:
scanf_s("%f %c %c", &inTemp, &inUnit, 1, &outUnit, 1);
Alternatively, just use scanf:
scanf("%f %c %c", &inTemp, &inUnit, &outUnit);
And, as always, check the return values.
In general, as scanf is somtimes useful for little quick-and-dirty programmes, it’s of less use for productive software, because handling errors is hard. Use fgets and sscanf/strtod/strtol/… instead.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what does “%.*s” mean in printf in c
I have found the following line :
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
And I want to know the meaning of %.*s
The %.*s format means "print a string using a field width of n characters, where n is read from the next argument".
So here, it prints buffer with a width of size * rxed characters. (padding with spaces if necessary)
I would highly recommend reading the manual...
.* in a format string means:
the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Details can be seen here.
So you didn't give any details, but if the result of: size * rxed was 5, then you could do this:
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
or
asprintf(&c, "%s%5s", *msg_in, buffer)
to the same effect.