A C Program contains functions and variables.

The functions specify the tasks to be performed by the program. The hello world program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function.

c programming tutorial

Table of Contents:

Also notice that C is case-sensitive. The commands have to be written like they are below. C also denotes the end of statement with a semi-colon like Java & Pascal. Brackets signify either to "{" begin a group of statements, or "}" end a group of statements. The // or /* comment */ designates a comment. Anything after two slashes the compiler ignores. The last part of the program you should notice is the #include. This simply includes a group of functions from the filename specified between the less than and greater than signs (<...>). The file below stdio.h contains a list of standard functions for C to use, the function in our below program uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen.

Here is our next program to discuss. It declares several variables, performs some computations, and then outputs the results of those computations to the screen.

#include <stdio.h>
void main()
	int numcandy; // declare a number variable
	double cost; // declare a variable that can store decimals

	printf("How many lollipops do you want: ");
	scanf("%d", &numcandy); // get input from user
	cost = 0.55 * numcandy; // do some math
	printf("\nPlease pay %f to the cashier!\n", cost);

This program when ran, will prompt the user for how many lollipops they would like. After they enter a number and press enter, it will print out how much it will cost them to buy that many lollipops assuming lollipops cost 55 cents each.

The main, {} brackets, comments, and include should be familiar. The new part of this program is the scanf, variables, and the use of printf to print out numbers are the parts of this program that we haven't seen before.

First lets talk about the variables. This program has two variables numcandy, and cost. These are the names of the variables. Before a variable can be used it must be declared. Declaring a variable in C is easy, you simply tell the compiler the type, and the name you want the variable to have. So in the above example. The line int numcandy; tells the compiler you want a variable of type integer with the name numcandy. Then you can simply use numcandy through out your program to store int's (integers). Integers are non decimal numbers like -13, 0, 10, 5, etc. The other variable in the above program is a double which is used for storing numbers with decimals.

Next the scanf, and printf statements. After running the program you will notice that the scanf function simply gets a value from the user. It waits for the user to enter a number, and then press enter. It puts this value into our variable numcandy. Next notice some special character sequences contained in both the scanf and printf. First the printf has the sequence "\n". This sequence simply means move to the next line. Next we have the %f and %d. These tell printf and scanf what type of variables to expect. %f corresponds to double, and %d is for int. So if you changed the above programs printf line to ...pay $%d to... then you would get wierd output since printf would try to print cost as an integer, which does not make sense, because cost is a double. You need to make sure the "%" go with the right types.

Variable names are arbitrary (with some compiler-defined maximum length, typically 32 characters). C uses the following standard variable types:

int    -> integer variable
short  -> short integer
long   -> long integer
float  -> single precision real (floating point) variable
double -> double precision real (floating point) variable
char   -> character variable (single byte)

The printf function can be instructed to print integers, floats and strings properly. The general syntax is

	printf( "format", variables );
where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are
%.nd	integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf	float or double (optional m = number of columns,
			 	  n = number of decimal places)
%ns	string (optional n = number of columns)
%c    	character
\n \t 	to introduce new line or tab
So using this new knowledge, you could change the above program where it prints the cost to ...pay $.2f to... which would print the number, and then only two decimal places of the double. Look back up at the program one more time at the line cost = 0.55 * numcandy;. This is the math of the program. This line is fairly self explanatory. It takes the value which is stored in numcandy multiplies it with 0.55 and puts the resulting value into cost. Here are a list of the standard math operators you can use in your programs:
+	add
-	subtract
*	multiply
/	divide
=	equals (assignment operator)

Data Types and Printf

Now that we have covered printf and the basic data types, we can move onto program control meaning loops and conditions.