How can I guarantee one #define is larger than another? - arduino

#define MY_CONST 20
#define OTHER_CONST 10
My code only makes sense if MY_CONST > OTHER_CONST. How can I guarantee this with the preprocessor? Is there any command like this?
#assert MY_CONST < OTHER_CONST

Is there any command like this?
#assert MY_CONST < OTHER_CONST
#if OTHER_CONST >= MY_CONST
#error "Error, OTHER_CONST >= MY_CONST"
#endif

as #attersson said #if will do it.
(as a good habbit try to parenthesize your macros to guarantee order of evaluation for more complex expressions. this answer shows why.)
#include <stdio.h>
#define A 10
#define B 11
#if (A) > (B)
#define RES "yes"
#else
#define RES "no"
#endif
int
main(int argc, char *argv[])
{
printf("is A larger? %s\n", RES);
return 0;
}

Related

3rd argument is ignored when I use memcpy in C

I'm a beginner in C and try to use memcpy to send byte in order to transfer data to server. It seems like no matter what I choose for 3rd argument. It's always the same.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char myChar[100] = "Hello world! ABC is an online sandbox that makes it easy to try out";
char myChar2[100];
memcpy(myChar2, myChar, 3);
printf("%s", myChar);
return 0;
}
This is the result.
Hello world! ABC is an online sandbox that makes it easy to try out
memcpy is working correctly.
The problem with your code is that you are printing myChar instead of myChar2.
printf("%s", myChar2); // Hel
Working example

Where is defined lispobj struct in SBCL sources

My question is self-explanatory. I'm grepping for ages and I can't find it ...
-------------------------------------------------------------------------------
lispobj is not a struct, just a typedef. It is defined in src/runtime/runtime.h currently after line 234:
#if 64 == N_WORD_BITS
#define LOW_WORD(c) ((pointer_sized_uint_t)c)
#define OBJ_FMTX "lx"
typedef uintptr_t lispobj;
#else
#define OBJ_FMTX "x"
#define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
/* fake it on alpha32 */
typedef unsigned int lispobj;
#endif

Understanding unix fork

Can anyone explain why the line containing "here" is executed 5 times and how exactly the program runs because I don't seem to understand how I get this output
Output:
12958: 0 here
12959: 0
12958: 0 here
12958: 1 here
12960: 1
12958: 0 here
12958: 1 here
Code:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(){
int i;
for(i=0; i<2; i++){
printf("%d: %d here\n", getpid(), i);
if(fork()==0){
printf("%d: %d\n", getpid(), i);
exit(0);
}
}
for(i=0; i<2; i++){
wait(0);
}
return 0;
}
Edit: because I'm running windows on my computer I used this website link to check the code, could that be a problem?
Fork creates an almost identical process, including the output buffers. If these are not flushed before the fork, both processes can end up printing the contents. Try putting a fflush(stdout); after the parent's printf.
It is not supposed to get the output you mentioned. As I tested your code my output was the following
11194: 0 here
11194: 1 here
11195: 0
11196: 1
Maybe you should recompile it and try again?

How to shorten std::vector?

Instead of doing this for calloc:
TCHAR *sText = (TCHAR *) calloc(1024, sizeof(TCHAR));
I have this at the top of my C++ file:
#define tcalloc(nCharacters) (TCHAR*)calloc(nCharacters,sizeof(TCHAR))
so I can more easily write this:
TCHAR *sText = tcalloc(1024);
Now, how do I do a shorthand for my std::vector statement? This is my code:
std::vector <TCHAR>sText(1024,0);
maybe
typedef std::vector<TCHAR> tcVec;
#define init_1k_charVector tcVec(1024,0)
int main(int, char**)
{
tcVec sText(1024,0);
tcVec sText2 = init_1k_charVector;
...
}

Serial Programming for POSIX, non-standard baud rate

I am implementing a simple program in unix that takes a RS232 input and saves it into a file.
I've used these references:
http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux and
http://www.easysw.com/~mike/serial/serial.html
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
FILE *file;
unsigned char c;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open("/dev/ttyS1", O_RDWR | O_NONBLOCK);
speed_t baudrate = 1843200; //termios.h: typedef unsigned long speed_t;
cfsetospeed(&tio,baudrate);
cfsetispeed(&tio,baudrate);
tcsetattr(tty_fd,TCSANOW,&tio);
file = fopen("out.raw", "wb");
while (1)
{
if (read(tty_fd,&c,1)>0) {
fwrite(&c, 1, 1, file);
fflush(file);
}
}
//close(tty_fd);
}
I've tried at 921'600 bps and at 1'843'200 bps, and it works correctly.
However, it does not work if I set-up a non-standard baud rate, for instance 1'382'400 bps.
i.e., this works:
cfsetospeed(&tio,1843200); cfsetispeed(&tio,1843200);
but this doesn't (it gets random data):
cfsetospeed(&tio,1382400); cfsetispeed(&tio,1382400);
What can be the problem?
I've tried with WinXP (using the WIN32 functions CreateFile, SetCommState and ReadFile),
and it works correctly (with 1'843'200 bps and also with the non-standard 1'382'400 bps)
ps: if you ask why I need to set-up this non-standard baud-rate, it's because of a special machine that works only at this speed.
Regards,
David
According to mans cfsetospeed accepts macros, B0, B50 , B75 and so on which are not equal to actual baudrate values (B9600 is equal to 15 e.g.). So passing random integer will cause undefined behaviour.
cfsetospeed() sets the output baud rate stored in the termios
structure pointed to by termios_p to speed, which must be one of
these constants: B0, B50 and so on

Resources