реклама на сайте
подробности

 
 
> Вывод sprintf
Andrew_k5
сообщение Oct 13 2009, 06:38
Сообщение #1


Частый гость
**

Группа: Участник
Сообщений: 77
Регистрация: 15-02-07
Из: Днепропетровск
Пользователь №: 25 387



С помощью этой функции вывожу данные, переменную объявляю volatile unsigned int, вывод %05d. Должен по идее выводить данные от 0 до 65535. А в выводе появляется знак минус и пр. Почему, ведь переменная объявлена как без знака. И еще вывожу переменную float %02.1f не добавляет ноль, те надо 02,5 а выводит 2,5.
Go to the top of the page
 
+Quote Post
 
Start new topic
Ответов (1 - 2)
Dima_G
сообщение Oct 13 2009, 07:03
Сообщение #2


Местный
***

Группа: Свой
Сообщений: 279
Регистрация: 2-07-08
Из: Новосибирск
Пользователь №: 38 699



Цитата(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 - ширина джробной части

Сообщение отредактировал Dima_G - Oct 13 2009, 06:49
Go to the top of the page
 
+Quote Post
Andrew_k5
сообщение Oct 13 2009, 08:46
Сообщение #3


Частый гость
**

Группа: Участник
Сообщений: 77
Регистрация: 15-02-07
Из: Днепропетровск
Пользователь №: 25 387



Спасибо за ответ
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 21st July 2025 - 08:26
Рейтинг@Mail.ru


Страница сгенерированна за 0.01349 секунд с 7
ELECTRONIX ©2004-2016