Shop OBEX P1 Docs P2 Docs Learn Events
Question about variable sizes in C — Parallax Forums

Question about variable sizes in C

Hal AlbachHal Albach Posts: 747
edited 2015-05-15 16:48 in Learn with BlocklyProp
I'm working on a personal project, a roving autonomous bot I call "Slammer" for how it reacts to walls, furniture, pets, and me. The question I have, is there any advantage in propeller C to sizing variables for the maximum value that it will hold? In my project I really only need char and short variables and set them up accordingly. All well and good, program compiles without any grief, runs as expected. Then on a whim, I changed all the variables to integer and upon compiling found I had reduced the program size by about 200 bytes. Does C add extra code to manipulate the smaller variables?
Also, I'm using a serial lcd to display distances to various walls and things for "Slammer" to run over to and then run into. Finally getting the head-banging under control. With the lcd I use the dprint(...) function to send out the data. I had learned earlier that the print(....) function has a smaller sister called printi(...) which saves around 4k of program space. Again, on a whim, I replaced the dprint(...) with dprinti(...) and it worked. My code size dropped from about 12.8K to around 7.8K! Yes, I realize that I can't print floats, not an issue since I'm not using them.

Anyway, I just thought I"d pose the variable size thing and wondered why smaller variables would produce larger code.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-05-15 16:48
    Yes, C adds extra code to manipulate smaller variables. I don't remember offhand whether char variables are signed or unsigned ... probably unsigned. There should not be any extra code needed for loading a 32-bit temporary ("register") with an 8-bit unsigned char. Short integers are signed and the sign has to be extended to 32-bits which takes an extra instruction or two,

    The standard print functions require the floating point routines even if they're not used. The "i" equivalent routines do not reference the floating point routines.
Sign In or Register to comment.