{"id":16104,"date":"2015-07-22T05:01:37","date_gmt":"2015-07-21T19:01:37","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=16104"},"modified":"2017-06-18T21:26:42","modified_gmt":"2017-06-18T11:26:42","slug":"c-sorting-command-line-switch-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-sorting-command-line-switch-tutorial\/","title":{"rendered":"C Sorting Command Line Switch Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.GIF\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C Sorting Command Line Switch Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.GIF\" title=\"C Sorting Command Line Switch Tutorial\"  \/><\/a><p class=\"wp-caption-text\">C Sorting Command Line Switch Tutorial<\/p><\/div>\n<p>Yesterday we started with a Windows C program that sorts arguments on the command line via the qsort method when we presented <a target=_blank title='C Sorting Primer Tutorial' href='#cspt'>C Sorting Primer Tutorial<\/a> as shown below.<\/p>\n<p>Today we show you a very useful technique to open up some functionality thoughts regards a command line program when we include some &#8220;homemade&#8221; switches.  Say &#8220;homemade&#8221; because you decide what switch means what bit of program configuration takes place, so today we allow for two &#8220;homemade&#8221; switches to our program &#8230;<\/p>\n<ol>\n<li>\/r &#8230; to reverse the sort (NB. Windows often uses &#8220;\/&#8221; while Unix and Linux more often use &#8220;-&#8221; &#8230; by convention &#8230; but it doesn&#8217;t have to be this way if you program differently &#8230; as we said, you make the rules with your switches)<\/li>\n<li>\/n &#8230; to sort numerically (rather than alphabetically)<\/li>\n<\/ol>\n<p>Nice to haves &#8230; not done (yet) &#8230; would include &#8230;<\/p>\n<ul>\n<li>explanatory advice if no arguments were entered (as is likely in all innocence) by the user<\/li>\n<li>allow for switches anywhere, rather than assuming they&#8217;ll be entered first (but again, the switch logic has your rules)<\/li>\n<li>control over the input and output (I\/O &#8230; input\/output) mechanisms &#8230; important for piping thoughts &#8230; we may talk more on this<\/li>\n<\/ul>\n<p>In this program, now, you may see the use of &#8230;<\/p>\n<ul>\n<li>function pointers &#8230; pointers that point at functions within your program code &#8230; cute, huh? &#8230; we talked about these, also, with <a target=_blank title='Function Pointers in C and C++ Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/?p=1376'>Function Pointers in C and C++ Primer Tutorial<\/a> and we also like <a target=_blank title='Function Pointers explained ... thanks' href='http:\/\/emunix.emich.edu\/~haynes\/341\/wi10\/Lectures\/C_FunctionPointers\/C_FunctionPointers.pdf'>this link<\/a> &#8230; thanks<\/li>\n<li><code><br \/>\n qsort((void *)argv, (size_t)argc, sizeof(char *), &(*(int (*)(void const *, void const *))funcArr[mymode]));<br \/>\n<\/code><\/li>\n<\/ul>\n<p>The programming C code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.c-GETME\" title='use_qsort.c'>use_qsort.c<\/a> changing from yesterday as per <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.c-GETME\" title='use_qsort.c'>this link<\/a>.<\/p>\n<hr>\n<p id='cspt'>Previous relevant <a target=_blank title='C Sorting Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/c-sorting-primer-tutorial\/'>C Sorting Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.JPG\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C Sorting Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.JPG\" title=\"C Sorting Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">C Sorting Primer Tutorial<\/p><\/div>\n<p>As we have mentioned before, as with <a target=_blank title='C String Function Timings Primer Tutorial' href='#csftpt'>C String Function Timings Primer Tutorial<\/a> as shown below, you don&#8217;t have to use an IDE to do C programming.  There is <a target=_blank title='Digital Mars' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Digital+Mars'>Digital Mars<\/a> C as an example of that.   We have talked about the <a target=_blank title='Xcode and its Command Line Tools Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=5519'>Xcode command line tools<\/a> before, that frees the gcc compiler to (also) be a command line tool to write C programs from the command line separate to any IDE usage, and we&#8217;d also like to add that Windows C programmers from the days of Microsoft C, who miss it (know I do &#8230; it was solid), can still write some of this code using Digital Mars C.  Luckily, one of the most useful functions, as exemplified by the C code line below (and with the WordPress 4.1.1 version <a target=_blank title='C Sorting Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/c-sorting-primer-tutorial\/'>C Sorting Primer Tutorial<\/a>) &#8230;<\/p>\n<p><code><br \/>\n qsort((void *)argv, (size_t)argc, sizeof(char *), isbigger);<br \/>\n<\/code><\/p>\n<p> &#8230; qsort &#8230; is available in Digital Mars C, as it used to work in Microsoft C.<\/p>\n<p>We show it in action today sorting, alphabetically, command line arguments (on the Windows (DOS) command line <font size=1>doh!<\/font>).  It has the scope, with its interface to tailoring your own sorting &#8220;algorithm&#8221; function abilities, to be able to handle any type of business logic you choose, that &#8220;sorting&#8221; jobs so often ask for.<\/p>\n<p>So &#8230; get &#8230; sorted &#8230; with our C code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/use_qsort.c_GETME\" title='use_qsort.c'>use_qsort.c<\/a> based on an idea out of a Microsoft C manual and Digital Mars compilable via (the taxing <font size=1>chortle, chortle<\/font>) &#8230;<\/p>\n<p><code><br \/>\n dmc use_qsort<br \/>\n<\/code><\/p>\n<p> &#8230; yes &#8230; no make files required here, nor IDE menus &#8230; but brush up on your <a target=_blank title='Pointers information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Pointer_(computer_programming)'>pointers<\/a>, perhaps.<\/p>\n<p>As a Windows programming afficianardo would tell you &#8230; desktop functionality opens you up to many scheduling and batch mode processing ideas often crucial to the best productivity tools in software.  It is no wonder that the web&#8217;s curl is so popular so that a bit of these software strengths from the desktop days can be brought to bear on the web application world we increasingly encounter today.<\/p>\n<hr>\n<p id='csftpt'>Previous relevant <a target=_blank title='C String Function Timings Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/c-string-function-timings-primer-tutorial\/'>C String Function Timings Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/string_timings\/timings_c.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C String Function Timings Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/string_timings\/timings_c.jpg\" title=\"C String Function Timings Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">C String Function Timings Primer Tutorial<\/p><\/div>\n<p>You don&#8217;t have to use an IDE to do C programming.  There is <a target=_blank title='Digital Mars' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Digital+Mars'>Digital Mars<\/a> C as an example of that.   We have talked about the <a target=_blank title='Xcode and its Command Line Tools Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=5519'>Xcode command line tools<\/a> before, that frees the gcc compiler to (also) be a command line tool to write C programs from the command line separate to any IDE usage.<\/p>\n<p>So it is today with this tutorial, where we work with Digital Mars C, to time some inhouse and C string library (string.h) library string manipulation functionality.   If you get to compile (recommend <a target=_blank title='Digital Mars download page' href='http:\/\/www.digitalmars.com\/'>Digital Mars C<\/a>) and run this tutorial&#8217;s download at a Windows Command Line (prompt) you will see how fast C is, working way down there near the <a target=_blank title='kernel information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Kernel_%28computing%29'>kernel<\/a> level.   So the second accuracy of <i>time(NULL)<\/i> won&#8217;t pass muster for the timings, and instead, here, we use the <i>clock()<\/i> functionality to get the refinement required to have it mean anything.   Could be affected by so many other things anyway, like what else is running, but is instructive as to what you might have thought was the fastest method.   Personally, love using strstr() to find strings within strings, but, as you can see from the numbers, there are better choices such as strchr() and strpbrk() &#8230; personally, I&#8217;m affronted, gobsmacked, insulted, resigned &#8230; impersonally, I&#8217;m anti-affronted, anti-gobsmacked, anti-insulted, anti-resigned &#8230; the nearby ants,  antily-anti-affronted, antily-anti-gobsmacked, antily-anti-insulted, antily-anti-resigned.<\/p>\n<p>So feel free to download the C programming source code here and rename it to <a target=_blank title='timings.c' href='http:\/\/www.rjmprogramming.com.au\/C\/string_timings\/timings.c_GETME'>timings.c<\/a><\/p>\n<p>Hope you enjoy this <a target=_blank title=\"Digital Mars compiling\" href=\"http:\/\/www.rjmprogramming.com.au\/C\/string_timings\/timings_c.jpg\">tutorial<\/a> showing you some command line C dmc (Digital Mars) compilation work.<\/p>\n<p>A really helpful tutorial for code above to do with using clock() is shown <a target=_blank title='Good tutorial 1' href='http:\/\/www.tutorialspoint.com\/c_standard_library\/c_function_clock.htm'>here<\/a> &#8230; thanks.<\/p>\n<p>A really helpful tutorial for C string functionality via string.h is shown <a target=_blank title='Good tutorial 2' href='http:\/\/www.cplusplus.com\/reference\/cstring\/'>here<\/a> &#8230; thanks.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d6788' onclick='var dv=document.getElementById(\"d6788\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Digital+Mars\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d6788' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d16078' onclick='var dv=document.getElementById(\"d16078\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d16078' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d16104' onclick='var dv=document.getElementById(\"d16104\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d16104' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday we started with a Windows C program that sorts arguments on the command line via the qsort method when we presented C Sorting Primer Tutorial as shown below. Today we show you a very useful technique to open up &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-sorting-command-line-switch-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[176,234,235,319,331,358,473,1638,2238,960,1637,997,1636,1173,1319,1435],"class_list":["post-16104","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-c","tag-command-line","tag-command-line-arguments","tag-desktop","tag-digital-mars","tag-dos","tag-function-pointers","tag-microsoft-c","tag-output","tag-pointer","tag-pointers","tag-programming","tag-qsort","tag-sort","tag-tutorial","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16104"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=16104"}],"version-history":[{"count":6,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16104\/revisions"}],"predecessor-version":[{"id":16114,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/16104\/revisions\/16114"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=16104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=16104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=16104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}