Таинственный (для меня) printf. Почему печатает "не то" - C (СИ)
Формулировка задачи:
Всем привет!
Есть отлично работающая программа, но я её хочу подправить, соответственно мне надо разобраться в некоторых моментах её работы. Что б разобраться я вывожу данные через КОМ-порт на терминал.
Вот участок кода который никак не укладывается у меня в голове, хотя он примитивен
В терминале я получаю следующее:
То в терминале получаю:
Листинг программы
- void getString(long int run)
- {
- unsigned int res = 123000;
- printf("res = %d\n", res);
- printf("run = %d\n", run);
- res = run;
- printf("res = run = %d\n", res);
- res = 65535 - res;
- printf("65535 - res = %d\n", res);
- decToChar(res);
- }
res = -7616
run = -8072
res = run/32 = -8072
65535 - res = 8071
Хотя ожидал увидеть 123000 и так далее...
Что я делаю не так в printf ?
Спасибо!
ХЕЛП прилагается:
The following conversion type characters are supported:
'i' - the function argument is a signed decimal integer;
'd' - the function argument is a signed decimal integer;
'u' - the function argument is an unsigned decimal integer;
'e' - the function argument is a float, that will be outputted using the [-]d.dddddd e[±]dd format
'E' - the function argument is a float, that will be outputted using the [-]d.dddddd E[±]dd format
'f' - the function argument is a float, that will be outputted using the [-]ddd.dddddd format
'x' - the function argument is an unsigned hexadecimal integer, that will be outputted with lowercase characters;
'X' - the function argument is an unsigned hexadecimal integer, that will be outputted with with uppercase characters;
'c' - the function argument is a single character;
's' - the function argument is a pointer to a null terminated char string located in RAM;
'p' - the function argument is a pointer to a null terminated char string located in FLASH;
'%' - the '%' character will be outputted.
И вдогонку.
Если я использую: 'u' - the function argument is an unsigned decimal integer;
Листинг программы
- unsigned int res = 123456;
- printf("res = %u\n", res);
- printf("run = %u\n", run);
- res = run;
- printf("res = run/32 = %u\n", res);
- res = 65535 - res;
- printf("65535 - res = %u\n", res);
- decToChar(res);
res = 57920
run = 57464
res = run/32 = 57464
65535 - res = 8071
Почему ТАК? Решение задачи: «Таинственный (для меня) printf. Почему печатает "не то"»
textual
Листинг программы
- #include <iostream>
- using namespace std;
- void getString(long int run)
- {
- unsigned int res = 123000;
- printf("res = %d\n", res);
- printf("run = %d\n", run);
- res = run;
- printf("res = run = %d\n", res);
- res = 65535 - res;
- printf("65535 - res = %d\n", res);
- //decToChar(res);
- }
- void main()
- {
- getString(100);
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д