Variable for Altering String Output in `printf()` - C Language [duplicate] - c

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 ".

Related

explanation of the output of the following program [duplicate]

This question already has answers here:
How is printf statement interpreted?
(5 answers)
Closed 5 years ago.
#include<stdio.h>
void main()
{
printf(5+"good morning");/*need explanation for this line
return 0;
}
The output of the program is - morning
can anyone explain how?
Prototype of printf
int printf(const char *format, ...);
Here format is a type of const char* and point to address of first element of string literal. When you pass 5+"Good morning" in printf, what you are really passing is the memory address of the string plus 5. The plus 5 means printing will start 5 chars beyond the start of the string, and the space after the word "Good",counts as a char.
when you call with 5+"good morning" parameter is converted to pointer. That means there is string constant "good morning" stored somewhere in the executable and compiler pass its pointer. something like this:
const char txt[]="good morning\0";
printf(5+txt);
So the printf will obtain the evaluated pointer txt+5 which bypassed first 5 characters in the string (as one char is single BYTE and single memory address on 8bit WORD addressing machines).
The output of the program is - morning
printf(5+"good morning");
prints the string inside the " ", overpassing the first five characters. So the first four characters g, o, o, d and the fifth character, the space, will be overpassed and the rest of the string will be printed.
Printf() method, is used to print the text in ()
It prints only "morning" and 5+ bypassed the initial 5 characters that is "g" "o" "o" "d" and a " " (space)

printf width modifier for special characters [duplicate]

This question already has answers here:
Number of character cells used by string
(6 answers)
Closed 5 years ago.
i have a problem with printf width modifier in C
example:
char a[] = "o", b[] = "l";
printf("%-3s %s", a, b);
console output gives me 3 spaces between strings, but when I change "o" to "ó", console shows 2 spaces between. Every time i use character like "ł", "ó", "ś" in the string, width modifier is shortened by 1 sign, why this happens?
OS X 10.11 (El Captain)
"Special" characters as you are showing them need more bytes (char) to be represented in a string. Your arbitrary limit of 3 is not enough, raise it to some suitable value.
In addition, the way and length that such special characters are represented depends on the system. For portable code you should never make such arbitrary assumptions.

Print from one position to another - using printf [duplicate]

This question already has answers here:
Is it possible to print out only a certain section of a C-string, without making a separate substring?
(3 answers)
Closed 7 years ago.
I want to do the following, taking into account a string as how are you I want to print from a until e ie the word are. I tried this
char str[] = "how are you";
printf("%*.*s\n", 5, 8, str);
Output
how are
Expected output
are
Someone has idea how this can be done?
The characters in the string are indexed from 0, so the a is at index 4. By passing &str[4] to printf, the printf will start at the a, and print as many characters as specified by the precision.
printf("%.*s\n", 3, &str[4]);
You are specifying field width and number of chars to print. However, the string always starts from the pointer as passed, there is no "start offset" specifier, as that would be unnecessary. See here for the format specifiers.
For your code, that would be printf("%.*s", width, str + start)
Note that &str[start] is identical.
try using this code
printf("%.*s\n", 3, &str[4]);

What does conversion specifier %n exactly do? [duplicate]

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

understanding a notation in printf [duplicate]

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!

Resources