Learn C By Example


Author: Eddie Meyer

Version: 1.0.2

Date: 12th December 2005

Outputting Text

Here is my version of the classic 'Hello World' program.

/*      ====================
 *      Hello World Program
 *      ====================
 *      Purpose: Print 'Hello World' on the screen.
 *      Author: Eddie Meyer
 *      Date: 21 OCT 2005
 */
#include <stdio.h>

int main(void)
{
    printf("Hello World!\n");
    return 0;
}

Variables

The program below is designed to introduce you to the most basic variable types available in C. Note: This program does not do anything other than create and initialise some variables.

/*      Variables Program
 *      ====================
 *      Purpose: Demonstrating the creation and assignment of some simple variables.
 *      Author: Eddie Meyer
 *      Date: 5 DEC 2005
 *
 *      History
 *      =========
 *      12 DEC 2005 - Eddie Meyer
 *      I changed the example I used for the double data type.  If you want to know
 *      why, I just decided that I didn't like the old example.
 */
#include 

int main(void)
{
	/* ============================================================
	 * Let's create (declare) some variables of different types.
	 * I've tried to give an indication of what each variable
	 * will hold by choosing variable names that essentially
	 * describe what they will be used for.  You are always
	 * recommended to try and do this to help others understand
	 * your code.  Oftentimes, it is you that will need reminding
	 * of what each variable in your program does.  Choosing
	 * descriptive variable names will prove extremely valuable
	 * to you when you come to review your code a year or two
	 * down the line.
	 * ============================================================
	 */


	/* Create a simple string (a sequence of characters).
	 * Technically, there is no such thing as a string type in C.
	 * A string in C is really just a sequence (array) of
	 * characters.  As you will soon learn, to use strings
	 * effectively in C, you are going to want to learn how to
	 * use C's standard string functions.  These will be covered
	 * later in the tutorial (still to be written).
	 *
	 * In this program, we will use a string variable to store my
	 * last name.
	 */
	char* last_name;


	/* Create a char (a single character value). For example,
	 * this could be used to store any single character in the
	 * alphabet.
	 *
	 * In this program, we will use a char variable to store my
	 * middle initial.
	 */
	char middle_initial;


	/* Create an integer (a whole number). Example values for
	 * an integer could be 0, 5, 384, -25 etc.  Note, that there
	 * is no decimal part to an integer.
	 *
	 * In this program, we will use an integer variable to store
	 * my age.
	 */
	int age;


	/* Create a float (a number with a fractional part). Example
	 * values could be 0.0, -5.6, 312.666 etc.
	 *
	 * In this program, we will use a float variable to store my
	 * hourly wage (in dollars and cents).
	 */
	float hourly_wage;


	/* Create a double (another number type with a fractional part).
	 * A double should be used instead of a float when you are going
	 * to need a higher level of precision for your program's
	 * calculations.
	 *
	 * In this program, we will use a double variable to store the 
	 * likelihood (as a percentage) that I will become president of
	 * the United States.  We will want to use a double for this, 
	 * because we know that the likelihood will be very small.
	 */
	double president_likelihood;


	/* ================================================================
	 * Now let's assign some values to the variables that we created
	 * ================================================================
	 */

	/* Use the 'last_name' variable to store my last name.
	 * Note the use of double quotes when dealing with strings.
	 */
	last_name = "Meyer";


	/* Use the 'middle_initial' variable to store my middle initial.
	 * Note the use of single quotes when dealing with chars.
	 */
	middle_initial = 'J';	/* For Jonathan	*/


	/* Use the 'age' variable to store my age */
	age = 30;


	/* Use the 'hourly_wage' variable to store my hourly wage
	 *(in dollars and cents).
	 */
	hourly_wage = 250.75;	/* Yah, I wish it were this much. */


	/* Use the 'president_likelihood' variable to store the
	 * percentage chance that I will become president of the
	 * United States.  Note	that there is no scientific basis
	 * for the value that I	chose to use.
	 */
	president_likelihood = 0.000000000000152;


	/* End of program */
	return 0;
}

Please come back for more soon.

-- Eddie.



Copyright © 2006 Eddie Meyer