Eh, it's just the simplest when in bash. In C it may be possible to do:

    int ncpu = sysconf(_SC_NPROCESSORS_ONLN);

On the command line there is also:

     lscpu --online --parse | grep -c -v ^#

But I'm not sure that lscpu is any better than cpuinfo :-P