Computer Programming - Advanced C-Like

Updated on October 1, 2016
charles griffiths profile image

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

Arithmetic Shift Right
Arithmetic Shift Right | Source

Solving All The Puzzles

You're an intermediate level programmer. If that's not true, read this introductory article or this intermediate one before continuing.

Now you're an intermediate level programmer, and you want to improve your skill to expert level. This article will help point the way. Earlier articles in this series covered the skills required for most programming, leaving out data structures, algorithms, and a few rare variable types. An expert programmer is aware of all these things, even if they go unused almost all the time.

Let's get started.

All The Variables

  • boolean - A boolean value can be true or false. In traditional C, boolean values are represented by ints but some variants (including C as of C99) have a dedicated type called "bool" or "boolean".
  • char - In the previous article I introduced char as a way to store strings. They're also a type of int, just half the size of a short.
  • long long - A long long, or "long long integer" is just like an int but bigger. That means it can store a wider range of values, takes up more memory in RAM or in a file, and probably takes more time to process. Adding two long longs might take longer than adding two ints, but how much longer depends on your hardware.
  • long double - A long double is just like a double, but bigger.

In the last article, I mentioned epsilon and how most of the time you don't have to worry about it. When you start tackling problems that require long doubles, you're probably worrying about epsilon and rounding errors.

It's a complicated problem, and this is an advanced article but see this reference for a good explanation. In short, floating point numbers have a limited number of digits so when they get large enough adding one is the same as adding zero.

Type Name
Size In Bytes
+/- 127 (2^7 - 1)
long long
+/- 2^63 -1
long double
10 to 16
+/- 1.18e4932
enum boolean { false, true };

More Integers With Enumerated Types

An enum or "enumerated type" allows you to define a set of int constants that make your program easier to read. The first constant in the list is 0, the next is 1, and so on.

Don't forget that they're ints. In the boolean example to the right, false is 0 and true is 1 so "if (false)" and "if (true)" work as expected, but "if (true == (expression))" is not the same as "if (expression)" because in C any non-zero integer value is true even though you've defined an enum called boolean where true is always 1.

Modifying Variables With Const Extern Static

Const, extern, and static can be used to modify a variable in different ways.

A variable declared as const is a constant, meaning its value can never change.

A variable declared as extern is shared between source files.

A variable declared as static inside a function will maintain its value between function calls. It's like a global variable that can only be accessed from within that function.

typedef struct BigIntegerStruct
  bool negative;
  char *data;
  int dataLength;
} BigIntegerType;

BigIntegerType createBigInteger( int length );
BigIntegerType destroyBigInteger( int length );
BigIntegerType addBigInteger( BigIntegerType a, BigIntegerType b );
BigIntegerType subtractBigInteger( BigIntegerType a, BigIntegerType b );

Pushing The Limits

What if you want to go beyond the limits? Maybe your program needs very large ints or very precise floats. You can make a new type of data with struct. College programming courses often give such an assignment in the first year or two: write a set of functions and data structures to support operations on very large integers (hundreds of digits).

To accomplish this, a student might define a struct BigIntegerStruct and make it a new type BigIntegerType with typedef. In practice, BigIntegerStruct and BigIntegerType need not be different names and you could call them both BigInteger: typedef struct BigInteger {...} BigInteger;

Defining the usual integer operations on this new type allows a programmer to break the larger puzzle into smaller parts that are each more easily solved and tested than the whole. A the same time, students are introduced to the idea of an algorithm (a series of steps that eventually solves a puzzle) and how to analyze the order of its runtime in terms of its inputs. In the case of big integers, adding and subtracting take as much time as the length of their inputs, or O(n). Multiplying and dividing would take as much time as the product of the length of their inputs, or O(n2).


Operation XOR


Integer Operations

Until now, you were probably aware that C data types are each a series of one or more bytes, and that bytes are each eight bits, and that each bit is 0 or 1. C allows you to manipulate and test these bits directly. To help with this, you can define integer values with a leading 0x to specify a hexadecimal value, and use %x or %X in the printf() format string to display an int in base 16.

The usual operators still apply: + - * / % and recall the tests == > < >= <=. Testing an integer by itself is false if the integer is 0, and true otherwise.

Add to these operators: & (AND), | (OR), ^(XOR), and the unary ~(NOT) which turns all 1s to 0s, and 0s to 1s. The bitwise AND and OR work just like their logical counterparts, but treat each integer as an array of boolean. XOR (exclusive OR) is true only if exactly one of its two inputs is true. That is 1^0 and 0^1 are both true (1), and 1^1 and 0^0 are both false (0).

You can also shift bits right (>>) and left (<<) within an integer. The picture at the top of this article shows a right shift in action.

With these operations, you can treat an int (x) as an array of 32 bits and test bit n (0-31) with:

  • if (x & (1<<n))

Why would you want to do this? Most of the time, you wouldn't want to. For example you may have wondered why a C99 standard boolean value takes up as much as an int (four bytes, or 32 bits) when boolean data is only one bit's worth. The reason is speed. Until you run out of RAM, speed is more important than memory and checking the value of an int is usually faster than isolating one bit and then checking its value.

But not always. If your program needs enough boolean values and needs to access them at random, eventually the bottleneck is moving data instead of processing data. Even in cases where it's better to use one bit per boolean, it's almost always not important and it's simpler to just use an int.

This point about simplicity bears emphasis. Simplicity wins. When you achieve an expert level of knowledge, don't spend it as fast as you can. Do it the boring way. Do it the boring way until an expert level of knowledge is required because none of the boring options are good enough.

Future Directions

In these three articles I have attempted to treat C-Like languages as more alike than they are different, but actual programming is done in an actual language with all of its strengths and weaknesses bundled in.

To gain further skill, a programmer must appreciate language differences despite all similarities. C is still the Latin of modern computing, but the Renaissance is in full swing and important scholars are studying in French, Italian, Spanish, English, German, each with its own purpose whether those purposes are achieved or not.

For your next mission should you choose to accept it, focus on one language at a time (like C). It may be the language you have to use for a particular project, or one that has idioms you wish to emulate, or simply one that interests you for interest's own sake.

Whatever path you choose from here, you're already well on your way to spending the roughly 10,000 hours of practice that it takes to be a world class expert.

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
    • charles griffiths profile imageAUTHOR

      charles griffiths 

      3 years ago

      Hi paxwill, I can't recommend a good Python resource. I was never a fan :D

      What I've heard about Python is that you should just jump right in and start using it. Learn it as you go. Good luck to you!

    • profile image


      3 years ago

      I know this page is about C, but I was wondering if you knew of any good resources for learning Python? thanks


    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)