ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to write C programs?

Updated on April 3, 2011

This hub is part of a series I'm writing about C programming in Linux, please check out the hub "C programming in Linux" to see the full coverage.

In this hub, I'd like to take you through a whirlwind journey of writing C programs.

Now, buckle up!

Writing C programs can be fun, exciting and rewarding, however for some, it can also be daunting. I will not pretend that the C language is for everyone. I do encourage you, however, to give it a try and recognize that sometimes it can be hard and can require a bit of persistence in order to carry on.

Programming is a craft, and like any other crafts, do require practice, patience and sometimes persistence.

Alright, I hope I haven't discouraged you in pursuing C programming. Take it from me, it can be really really rewarding. If you haven't already, don't forget to check out my hub about the features of the language; please click this link: Introduction to C programming.

I know you are as excited as I am to get right down to the nitty-gritty details and get you writing C programs immediately. However, in this hub, I'd like to quickly run you through a preview of writing C programs. I'm not delaying your progress in learning C but I believe it would be to your benefit if I lay down a good foundation of C on which you can build on to make your journey as smooth as possible.

Basic elements of a C program

A C program is really just a collection of the so-called "statements"; like "sentences" in spoken language like English. Just like the spoken language, C statements are also governed by some rules in order for them to be valid.

These rules are referred to as "syntax" and "semantics" in conjunction with "constructs". This is like "grammar" in conjunction with "lexicon" of a language.

A C statement is a collection of constructs that accomplishes something. It's like complete and valid sentences or phrases that mean something. For instance, this is a meaningful English phrase: "Print the word 'Hello' on the screen." If I were to compare this to an equivalent C statement, I would write it like this:


In the C statement above, I've written it in the manner that complies with the syntax and semantics of grouping some constructs in order to instruct the computer to print the word "Hello" on the screen.

These are the constructs that I've used:

  • The function called "printf".
  • A character array, also known as string, "Hello".
  • The statement separator symbol which is the semi-colon character ";".

Please note that I'll use the word "symbol" and "character" interchangeably to mean any of the symbols you can type on your keyboard, like A, B, 1, 2, *, &, [, }, etc.

Notice that the word Hello is enclosed by double quotes which are further enclosed by parenthesis characters. This statement would not have been valid if I wrote it in the following three different ways:

(printf "Hello");
"printf; (Hello)"

So what are these constructs then? Please continue reading down below.

C language constructs


There are so-called "language words" in C that are defined by the C language and can only be used according to how they're defined. These words are also called "reserved words" or also "keywords".

Since you can define your own "words" (known as "identifiers") when writing your own programs, it's important to know what these C keywords are since you can't redefine them and make them your own. Luckily, there aren't that many reserved words, they're listed down below:

auto      break    case    char      const    continue
default   do       double  else      enum     extern
floatfor  goto     if      int       long     register
return    short    signed  sizeof    static   struct 
switch    typedef  union   unsigned  void     volatile

I'd like to describe each of the keyword above but it would be a bit too much for this hub. Don't worry, we'll get to discuss each one in depth when appropriate. If you really want to dive into them right now, please click this external link:


Words that you create or make up when writing your C program are called identifiers. You use them to name "variables", "constants" and "functions".

As mentioned earlier, you can't redefine keywords, therefore you can't use keywords as identifiers.

To create an identifier, you simply use a letter or use words. Like the ones below:

x    temp    Height    var    total    FinalGrade

You can also use letter-number combination, also known as alpha-numeric, like this:

x1   temp4   Height2   var6   total3   Final3Grade

Notice in the preceding examples that you can't use a number to start an identifier. Also, you can't use any other characters like $, *, % or # in your identifiers except for the underscore character which is valid even to start the name of an identifier, like this:

_x1  temp_4  _Height2_  _var_6 total_3  Final_3_Grade

Please note that identifiers are case-sensitive. In other words, the following identifiers are different:

Temp    temp    TemP    TEMP    TEmp

You need to remember that C language as a whole is case-sensitive. Thus, all the keywords above must also be typed as such. You can't use "WHILE" instead of "while".

As mentioned above, identifiers are used to name "variables", "constants" and "functions".

Variables are data placeholders or storage. A variable is an identifier that corresponds to a memory location in your computer where data can be stored or accessed. Remember that computers use a circuitry called "memory" in order to store and read data.

Constants are data placeholders or storage that can only hold one value that never changes. In other words, you can only assign it a value at the time you declare it. More on this later, for now, just think of this like PI or Euler's number or the golden ratio which are constants; values that don't change.

Functions are used to identify a block ("group") of C statements. Once you group C statements into a function, you can simply use the function name in order to execute the statements in that group.


C uses non-alphabet symbols in conjunction with identifiers in order to create expressions; like mathematical expressions for instance.

As an example, C uses the following symbols for arithmetic:

+    Addition
-    Subtraction
*    Multiplication
/    Division

So using operators in conjunction with identifiers, you can write code like this:

x = y + 100;
z = x * 2 - 4;
a = b * b + c * c;

C is rich in operators like the ones above that you can use in your programs to enable you to create more complex expressions to help you achieve what you're trying to accomplish in your programs.

Data types

I'll finish this hub with "data types". You will almost always deal with different types of data in your programs. Data like numbers, names, measurements, labels, etc. And these data will most likely be stored in variables or constants or manipulated in functions.

When creating identifiers, a process known as "declaration" in C, you will need to specify the data type.

For instance, if you want a variable to hold a person's age, you will declare a variable whose type is an Integer, like this: (note the use of the keyword "int")

int age;

The declaration above creates a variable called "age" that can hold Integer values. In C, integers are whole numbers like 1, 5, 6, and 10000. It can also hold negative numbers and even the number zero.

If you want a variable to store a person's name, you'll need to declare a character array variable. A person's name is really just an array of letters, thus a character array in C. See below for an example:

char name[20];

The code above creates a variable called "name" and can hold up to 20 characters.

Here's the list of all data types used in C:

  • int
  • char
  • float
  • double
  • void
  • enum

I hope this hub would have given you a glimpse on how to write C programs. Unfortunately I can't cover all the details of everything I've mentioned above in one hub. So please make sure you check out my other hubs.

What do you think of the style of the C language? Many would describe it as "spartan". What do you think?


    0 of 8192 characters used
    Post Comment

    • profile imageAUTHOR


      7 years ago

      C programming is one of the oldest language and almost every modern programming language has borrowed some concepts from it. Having said that, the Internet is littered with information about C programming, but not hubpages. So this is my attempt to write some easy-to-understand articles about C programming for the hubpages community.

    • prettydarkhorse profile image


      7 years ago from US

      I don't know anything about programming except social data packages SPSS. Do you have a website, maybe you can make one with the purpose of educating people about C programming?


    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)