C Sorting Command Line Switch Tutorial

C Sorting Command Line Switch Tutorial

C Sorting Command Line Switch Tutorial

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 some functionality thoughts regards a command line program when we include some “homemade” switches. Say “homemade” because you decide what switch means what bit of program configuration takes place, so today we allow for two “homemade” switches to our program …

  1. /r … to reverse the sort (NB. Windows often uses “/” while Unix and Linux more often use “-” … by convention … but it doesn’t have to be this way if you program differently … as we said, you make the rules with your switches)
  2. /n … to sort numerically (rather than alphabetically)

Nice to haves … not done (yet) … would include …

  • explanatory advice if no arguments were entered (as is likely in all innocence) by the user
  • allow for switches anywhere, rather than assuming they’ll be entered first (but again, the switch logic has your rules)
  • control over the input and output (I/O … input/output) mechanisms … important for piping thoughts … we may talk more on this

In this program, now, you may see the use of …

  • function pointers … pointers that point at functions within your program code … cute, huh? … we talked about these, also, with Function Pointers in C and C++ Primer Tutorial and we also like this link … thanks

  • qsort((void *)argv, (size_t)argc, sizeof(char *), &(*(int (*)(void const *, void const *))funcArr[mymode]));

The programming C code you could call use_qsort.c changing from yesterday as per this link.


Previous relevant C Sorting Primer Tutorial is shown below.

C Sorting Primer Tutorial

C Sorting Primer Tutorial

As we have mentioned before, as with C String Function Timings Primer Tutorial as shown below, you don’t have to use an IDE to do C programming. There is Digital Mars C as an example of that. We have talked about the Xcode command line tools 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’d also like to add that Windows C programmers from the days of Microsoft C, who miss it (know I do … 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 C Sorting Primer Tutorial) …


qsort((void *)argv, (size_t)argc, sizeof(char *), isbigger);

… qsort … is available in Digital Mars C, as it used to work in Microsoft C.

We show it in action today sorting, alphabetically, command line arguments (on the Windows (DOS) command line doh!). It has the scope, with its interface to tailoring your own sorting “algorithm” function abilities, to be able to handle any type of business logic you choose, that “sorting” jobs so often ask for.

So … get … sorted … with our C code you could call use_qsort.c based on an idea out of a Microsoft C manual and Digital Mars compilable via (the taxing chortle, chortle) …


dmc use_qsort

… yes … no make files required here, nor IDE menus … but brush up on your pointers, perhaps.

As a Windows programming afficianardo would tell you … 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’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.


Previous relevant C String Function Timings Primer Tutorial is shown below.

C String Function Timings Primer Tutorial

C String Function Timings Primer Tutorial

You don’t have to use an IDE to do C programming. There is Digital Mars C as an example of that. We have talked about the Xcode command line tools 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.

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 Digital Mars C) and run this tutorial’s download at a Windows Command Line (prompt) you will see how fast C is, working way down there near the kernel level. So the second accuracy of time(NULL) won’t pass muster for the timings, and instead, here, we use the clock() 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() … personally, I’m affronted, gobsmacked, insulted, resigned … impersonally, I’m anti-affronted, anti-gobsmacked, anti-insulted, anti-resigned … the nearby ants, antily-anti-affronted, antily-anti-gobsmacked, antily-anti-insulted, antily-anti-resigned.

So feel free to download the C programming source code here and rename it to timings.c

Hope you enjoy this tutorial showing you some command line C dmc (Digital Mars) compilation work.

A really helpful tutorial for code above to do with using clock() is shown here … thanks.

A really helpful tutorial for C string functionality via string.h is shown here … thanks.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

28 Responses to C Sorting Command Line Switch Tutorial

  1. You made some clear points there. I did a search on the subject matter and found most people will agree with your website.

  2. Whoa. That was a terrific short article. Please retain writing because I really like your style.

  3. free says:

    Extremely intriguing information !Perfect just what I was searching for!

  4. Oh my goodness! an astounding post dude. Thanks a great deal Even so We’re experiencing challenge with ur rss . Do not know why Can not sign up for it. Could there be everyone discovering identical rss challenge? Anyone who knows kindly respond. Thnkx

  5. contact says:

    Definitely, what a magnificent site and educative posts, I will bookmark your blog.Have an awsome day!

  6. concerned says:

    I believe you have remarked some very interesting points , thanks for the post.

  7. I am continuously looking online for ideas that can facilitate me. Thanks!

  8. solve says:

    Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just too magnificent. I actually like what you have acquired here, certainly like what you’re saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I can’t wait to read much more from you. This is really a wonderful web site.

  9. Hello. superb job. I did not anticipate this. This is a exceptional story. Thanks!

  10. bliss says:

    I was just seeking this information for a while. After 6 hours of continuous Googleing, finally I got it in your site. I wonder what’s the lack of Google strategy that do not rank this type of informative websites in top of the list. Normally the top web sites are full of garbage.

  11. Hey, great blog you might have here, think I came across it on Yahoo but im not certain nowanyway, Ill check back again! Are guests allowed to post here?

  12. Magnificent site. Lots of useful information here. I am sending it to some buddies ans additionally sharing in delicious. And naturally, thank you to your sweat!

  13. on Bing says:

    Thank you for sharing superb informations. Your web site is very cool. I’m impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found simply the information I already searched everywhere and simply couldn’t come across. What a great website.

  14. next says:

    I have been checking out a few of your stories and i can state pretty clever stuff. I will make sure to bookmark your blog.

  15. Strong says:

    Treasure the admission you offered.. Is not it amazing when you uncover a good article? Get pleasure from the post you supplied.. My internet surfing seem total.. thank you.

  16. elegant says:

    I enjoy you writing your point of view.. Isn’t it awesome any time you come across a good article? Really interesting mindset, thanks for posting.. Extremely beneficial perception, many thanks for sharing..

  17. I have been absent for a even though, but now I remember why I used to really like this blog. Thanks , Iรขโ‚ฌโ„ขll try and check back far more often. How regularly you update your site?

  18. Wonderful blog layout here. Was it hard creating a nice searching website like this?

  19. I should admit that this is 1 excellent insight. It surely gives a company the opportunity to get in on the ground floor and truly take part in creating something unique and tailored to their needs.

  20. I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this increase.

  21. Quinn Vasil says:

    Hello, you used to write excellent, but the last several posts have been kinda boringโ€ฆ I miss your super writings. Past several posts are just a little bit out of track! come on!

  22. What i do not understood is actually how you are no longer really much more well-appreciated than you may be right now. You are very intelligent. You already know thus considerably in the case of this subject, produced me personally consider it from so many numerous angles. Its like women and men aren’t fascinated until it’s something to accomplish with Lady gaga! Your own stuffs outstanding. Always take care of it up!

  23. Ben Shaheen says:

    Hey there, You’ve done an excellent job. Iโ€™ll certainly digg it and personally recommend to my friends. I am confident they’ll be benefited from this site.

  24. Ultimate says:

    Very advantageous standpoint, many thanks for blogging.. Loving the weblog.. thank you so much Is not it fantastic after you find a superb submit? My searches seem total.. thanks.

  25. Myra Beucler says:

    You have noted very interesting points! ps nice website.

  26. naturally like your web site however you need to check the spelling on several of your posts. Several of them are rife with spelling issues and I find it very troublesome to inform the truth then again I’ll certainly come again again.

  27. Ed Sigg says:

    Great website. A lot of useful information here. I am sending it to several pals ans also sharing in delicious. And naturally, thank you for your sweat!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>