printf - assign zero in unsigned short int in c -
i try assign 0 field in structure called list
list.ultimo = 0;
but when use printf
printf("%d", list.ultimo);
i result
32766
but when add
unsigned short int cero = 0;
the result of printf correct, no matter not use cero. why?
lista.h
#ifndef lista_h #define lista_h typedef struct{ elemento lista[tamanomax]; unsigned short int ultimo; }lista; void insertar(elemento, unsigned short int posicion, lista); #endif
lista.c
#include<stdio.h> typedef short int elemento; #ifndef tamanomax #define tamanomax 10 #endif #include"lista.h" void insertar(elemento x, unsigned short int p, lista lista){ if (lista.ultimo >= tamanomax){ puts("full list"); printf("%hu",lista.ultimo); } }
main.c
#include<stdio.h> typedef unsigned short int elemento; #define tamanomax 4 #include"lista.h" int main(){ lista esferas; esferas.ultimo = 0; insertar(2,0,esferas); printf("\n%hu\n", esferas.ultimo); return 0; }
the result is
$gcc -wall lista.c main.c full list 32765 0
i'm new this, i'm slow now, regret delay
use %hd
print short int
, %hu
print unsigned short int
. %d
int
.
edit: have posted full code, appears tamanomax
set 10
in lista.c
, set 4
in main.c
, leading incompatibilty between main()
function , insertar
one. should have same value in files. , if want work different array lengths, add length
member in lista
structure. tamanomax
should absolutely same everywhere, else you're in fact working different data types , program won't work.
Comments
Post a Comment