Computer Programming - Intermediate C-Like

Updated on October 7, 2016
charles griffiths profile image

Charles is a software engineer and college professor interested in technology, medicine, economics, and nutrition.

C Language Keywords, by Dennis Ritchie 1973
C Language Keywords, by Dennis Ritchie 1973 | Source

Solving More Puzzles

This article assumes you know something about programming in C-like languages. If that's not the case, then start here and come back when you're ready.

Back so soon? You can already solve most puzzles pretty well with the simple tools from my previous article. You can read most C-like programs (written in C, C++, C#, Java, Cg, and others that borrow heavily from C syntax) and get a gist of what's happening. You can also write simple programs. This article will add some tools to your programming toolbox. That means learning more of the C language keywords pictured above.

More Variables

There are still only three main variable types, but there are subtypes that use different amounts of memory, processing time, and have different limitations. If you want to use ints, floats, and strings your whole programming life then you can, but the following variants can sometimes make things easier.

  • long - A long in C is usually the same as an int but may be larger.
  • short - A short, or "short integer" is just like an int but smaller. It has a narrower range, takes up less memory, and the time to process is complicated. On most modern hardware the time to add two shorts will not be less than the time to add two ints, and it might take a bit longer. Why? It's because hardware is optimized to process ints (when 16-bit computers were more common, ints were 16 bits or two bytes just like a short).
  • double - A double, or "double precision floating point number" is just like a float but may be larger. Just like long is a variant of int, same idea.
  • void - Use void when you want a function to return nothing, or a pointer to be able to point to any type.

Strictly speaking a C compiler has a lot of leeway about how big these variables are, while other C-like languages define the same types with less variation. For the minimal guarantees in C, see Wikipedia. The version of events I give here is what you'll typically find in C programs written for a modern PC. Integer types can also be signed or unsigned, with the unsigned variant having twice the maximum and always >= 0. Variants of float also differ on how close a positive number can get to zero, or the minimum difference between two numbers. This is called the epsilon. Be aware it exists but don't worry about it for now.

Type Name
Size In Bytes
+/- 32767 (2^15 - 1)
+/- 2147483647 (2^31 - 1)
+/- 2147483647 (2^31 - 1)
+/- 3.4e38
+/- 1.8e308

Double Check

If you ever want to check the size of a type use sizeof(), and if you want to check a bit of code without firing up a compiler try ideone or codepad.

Here's an example on codepad.

Include Import Using

You may recall my reference to using functions written by other people as a time-saving nightmare. Usually it's just time-saving, and generally a good idea whenever possible. The way you achieve it in C is with a #include directive that you've already seen at the top of example C programs. #include tells C the function declaration (what a function looks like, return type and input variables) so you can use it in your program, and the appropriate function definition (what a function does, inside the function body) can be found in a library or object file when you build an executable.

Other C-Like languages might use #include (Cg), import (Java), or using (C#) for the same effect.

More Functions

As an intermediate level programmer, it's time to get familiar with your language's standard library. In C, this means the hundreds of functions of the C standard library. You should learn these as you need them, but here are some of the more common functions. Don't forget to use #include.

  • putch( char ch ) puts( char *string ) - Write a character or string to the screen.
  • getch() getche() gets() - Get a character or string from the keyboard.
  • strcpy( char *target, char *source ) - Copy a string from source to target.
  • strcmp( char *a, char *b ) - Compare two strings by alphabetical order.
  • malloc( size_t amount ) - Get some memory that must later be sent to free().
  • free( void *p ) - Free memory that was granted with a call to malloc().
  • fopen( char *filename, char *mode ) - Open a file for reading or writing.
  • fread/fwrite( void *data, size_t size, size_t count, FILE *fp ) - Read from or write to a file.
  • fclose( FILE *fp ) - Close a file that was opened with fopen().

A strength and a weakness of C is that none of these functions are part of the C language. There are standard definitions and they are often included with C compilers, but the functions themselves are written in C and you are free to substitute your own versions. This is most often done with malloc() / free().

C implementations of standard library functions are not only widely available, you have choices. Reading the library source and documentation is not a bad way to dive into intermediate programming if you're not sure where to start.

More Loops

The C for loop is the best and most common, and once you understand it there are two others that are easy to understand too.

Think of a for loop as: for ( Initialize; Test; Advance ) { Body }

Another kind of loop called while just leaves out Initialize and Advance, like so: while ( Test ) { Body }. Yet another variant, the do-while is a while that always performs the work in Body at least once, and looks like this: do { Body } while ( Test );

In any kind of loop, break means the loop is finished, and continue means the current Body is finished so do the next Advance and Test.

More IFs

C and most variants support two additional types of conditional statements. The first is a multi-path if statement called switch. The second is a three argument (ternary) operator designed to assign a variable ?:. Some people say they're confusing, but nothing could be further from the truth. Like anything, they can be used in a confusing way so try to use them in a straightforward manner that takes advantage of their strengths whenever possible.

A switch statement is just a multiple path if that searches a list of constant integer values for equality to an integer expression.

switch( choice )
  case 0:
    printf( "Choice is 0.\n" );
    break;   // break means the switch() is finished
  case 1:
    printf( "Choice is 1.\n" );
    break;   // in C and C++ the break after each case is optional
  case 2:
    printf( "Choice is 2.\n" );
    break;   // in C# it is required, each case must end in break
    printf( "Choice is not 0, 1, 2.\n" );

The ?: operator is known as the ternary operator because it has three parts. It works like this:

int result = Test ? value_if_test_is_true : value_if_test_is_false;

int maximum = 10;

for (int i=0; i<maximum; i++)
  printf( "%d This is%s the last time.\n", i,
           (maximum-1) == i ? "" : " not" );


0 This is not the last time.
1 This is not the last time.
2 This is not the last time.
3 This is not the last time.
4 This is not the last time.
5 This is not the last time.
6 This is not the last time.
7 This is not the last time.
8 This is not the last time.
9 This is the last time.

Integers and Floats in Memory

You're A Programmer Now

When you understand the ideas in this article through reading examples and solving a few puzzles on your own, you may consider yourself a C or C-Like language programmer. Your programming skills will transfer pretty easily to other C-Like languages, with the most difficulty being different styles of I/O (input/output, data from the keyboard, to the screen, to and from files and so forth), and memory management (C uses malloc()/free(), C++ uses new/delete, C# and Java give back memory automatically when it's no longer being used).

There are still inconvenient gaps in your programming knowledge. You don't know about data structures. You lack appropriate caution when comparing floats. You have a limited understanding of how to go about solving puzzles, or comparing two solutions which are also called algorithms. All these are very typical of intermediate programmers, but if you wish to improve your skill there are books, courses, and other resources available to help.

Good luck, programmer!

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)