Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: Вывод sprintf
Форум разработчиков электроники ELECTRONIX.ru > Сайт и форум > В помощь начинающему > Программирование
Andrew_k5
С помощью этой функции вывожу данные, переменную объявляю volatile unsigned int, вывод %05d. Должен по идее выводить данные от 0 до 65535. А в выводе появляется знак минус и пр. Почему, ведь переменная объявлена как без знака. И еще вывожу переменную float %02.1f не добавляет ноль, те надо 02,5 а выводит 2,5.
Dima_G
Цитата(Andrew_k5 @ Oct 13 2009, 09:38) *
С помощью этой функции вывожу данные, переменную объявляю volatile unsigned int, вывод %05d. Должен по идее выводить данные от 0 до 65535. А в выводе появляется знак минус и пр. Почему, ведь переменная объявлена как без знака. И еще вывожу переменную float %02.1f не добавляет ноль, те надо 02,5 а выводит 2,5.


man 3 printf

Цитата
d,i The int argument is converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must
appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When
0 is printed with an explicit precision 0, the output is empty.


Вам нужно %u использовать

По второму вопросу - %03.1f

Цитата
The field width
An optional decimal digit string (with non-zero first digit) specifying a minimum field width. If the converted value has fewer
characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given).
Instead of a decimal digit string one may write ‘*’ or ‘*m$’ (for some decimal integer m) to specify that the field width is given in
the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a ‘-’ flag
followed by a positive field width. In no case does a non-existent or small field width cause truncation of a field; if the result
of a conversion is wider than the field width, the field is expanded to contain the conversion result.

The precision
An optional precision, in the form of a period (‘.’) followed by an optional decimal digit string. Instead of a decimal digit
string one may write ‘*’ or ‘*m$’ (for some decimal integer m) to specify that the precision is given in the next argument, or in the
m-th argument, respectively, which must be of type int. If the precision is given as just ‘.’, or the precision is negative, the
precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of
digits to appear after the radix character for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G
conversions, or the maximum number of characters to be printed from a string for s and S conversions.


Цитата(Dima_G @ Oct 13 2009, 09:48) *
man 3 printf
По второму вопросу - %03.1f


те %04.1
4 - ширина всего поля
1 - ширина джробной части
Andrew_k5
Спасибо за ответ
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.