eTechtips

Tech Tips and Insights

Category: C/C++

  • C: Check if string is a number

    I was recently testing some basic input and wrote this code to test if the input was a number or not.

    #include 
    
    int main()
    {
      char name[10];
    
      scanf("%s",&name);
      if (checkifNumber(name))
      {
         printf("Is a numbern"); 
      }
      else
      {
         printf("Invalid numbern");
      }
    }
    
    int checkifNumber(char *inp)
    {
      int i=0;
      int isanumber = 1;
    
      while(inp[i] != '' && i < 10)
      {
        if (inp[i] >= '0' && inp[i] <= '9')
        {
        }
        else
        {
          isanumber =0;
        }
        i++;
      }
      return isanumber;
    }
    

    One thing that could be improved is to potentially use a global variable to define the length of the
    input string as it is used in two different places in the program and could potentially lead to an
    array overrun/underrun.

    0 Comments
  • Gallery of Hello World!

    Here is a list of the Hello World Programs for different languages:

    C

    #include <stdio.h>
    int main()
    {
    printf("Hello World!");
    }

    C++

    #include <iostream.h>
    int main()
    {
    cout << "Hello World!" << endl;
    }

    C#

    public class HelloWorld
      public static void Main()
      {
        System.Console.WriteLine("Hello World!");
      }

    Java

    class HelloWorld {
    static void main(String[] args)
    {
    System.out.println("Hello World!");
    }

    SHELL

    echo "Hello World"

    Python 2

    print "Hello World!\n"

    Python 3

    print ("Hello World!")

    Ruby

    puts "Hello World!"

    Perl

    print "Hello World!n";

    PHP

      <?php      
        print "Hello World!";
      ?>

    Rust

    fn main() {
        println("Hello World!");
    }

    This is the simplest form of Hello World for most of these languages.

    0 Comments
  • Using Variable Argument lists in functions for C and C++

    Using Variable Argument lists in functions for CC++

    In C there are times when you may want to have a variable amount of arguments passed into a function.  This can be accomplished when you pass the ellipses(…)  in as the last argument on your function.  The ellipses ,(…),  stands for zero  or more arguments.
    There are a set of functions available to handle accessing this data and making it available to your function.

    The source code example displays a version of the code for a printf like function and how to handle the optional arguments.

    One note is that the arguments passed in to this function are not typed.  The handling of the argument depends on either a specific type passed in(ints only) or as is the case of the example, a formatted string to define the types of the arguments. What this means is that if you pass random sets of arguments, (such as ints, strings,chars, floats) there is no data type that is directly associated to the variable being passed in.

    Required include file
    #include <stdarg.h>
    /* Old include <varargs.h> From before ISO C standard, GNU C compilers still support this */

    Available functions:
    Macro: va_start(va_list , last-required argument)
    This sets up the pointer for va_list with the avaiable argument list.

    Macro: va_arg(va_list, type)

    This returns the value of the next argument and modifies the va_list argument
    to point to the subsequent(next) argument.  The type of the value returned by
    va_arg is type as specified in the call. type must be a self promoting type
    not char or short int) that matches the type of the actual argument.

    Macro: va_end(va_list)

    This ends the processing of the va_list element and subsequent va_arg calls
    may no longer work.  Note: In the GNU C library implementation this does nothing
    and is used for portability.

    Sample Function Call:

      int int1 = 1;
    
        char char1 = "s";
    
        char *str1 = "test";
    
        /* Sample function call. */
        ecdprintf("Int=%d Char=%c String=%sn",int1,char1,str1);
    
    Sample Code follows:
    
    int ecdprintf(const char *pstr, ...)
    
    {
        const char *lstr;
        va_list argp;
        int lint;
        char *lchar;
        char strarr[255];
    
        /* This is the start of vararg processing. The first argument is the
             container argument of the vararg list and the second argument
             is the last fixed parameter passed into the function. */
    
        va_start(argp, fmt);
        for(lstr = pstr; *lstr = ''; lstr++)
        {
            if (*lstr != '%')
            {
                putchar(*lstr);
                continue;
            }
    
            switch(*++lstr)
            {
                case 'd':
                    i = va_arg(argp,int);
                    s = itoa(i,strarr, 10);
                    putchar(i);
                    break;
                case 'c':
                    i = va_arg(argp, int);
                    putchar(i);
                    break;
                 case 's':
                     lchar = va_arg(argp,char *);
                     fputs(lchar,stdout);
                     break;
                  case 'x':
                     i = va_arg(argp,int);
                     s = itoa(i, fmtbuf, 16);
                     fputs(lchar, stdout);
                     break;
                   case '%':
                      putchar('%');
                      break;
    
                   default:
                        break;
    
                 }
    
        }
    
        va_end(argp);
    
        }
    
    }
    

    This example created the a simple printf like function using characters within the string to let the function know how to deal with the extra variables used.

    0 Comments