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]);
Related
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)
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.
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:
sprintf buffer sizes
(3 answers)
Closed 9 years ago.
I'm having an issue with sprintf in C resetting the value of a counter variable when I run it. Here in a nutshell is what is happening:
int count = 0;
count++;
printf("%d\n", count); // count will equal 1
char title[7];
sprintf(title, "%.3d.jpg", count);
printf("%d\n", count); // count now equals 0 again
Is this normal behavior for sprintf?
title is too small, sprintf is writing beyond the bounds of title, and writing into count, corrupting its value.
Note that title needs to be long at least 8 bytes: 3 for %.3d, 4 for .jpg and 1 for the terminator \0.
As Grijesh Chauhan points out, you can make sure that you never write beyond the allocated size of the string by using snprintf, i.e.:
char title[8];
snprintf(title, sizeof(title), "%.3d.jpg", count);
You simply need more space in the string title: 3 digits+".jpg" = 7 chars, and u need one extra for '\0'(end of the string). Sprintf can change argument in a case such as this(Using sprintf will change the specified variable)
Solution: change
char title[7];
to
char title[8];
You problem is called buffer overflow. sprintf has no idea of the size of title, and write has much as needed, even if it exceeds the boundaries of your array. To fully understand, you also have to know that strings are terminated by a zero. This allows to find the end of a string, without prior knowledge of its true size. This extra zero takes the place of an extra character that you have to consider when dimensioning your buffers.
Consider also the use of snprintf that ensures you are not going over the boundaries of your buffer.
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.