by BehindJava

A Simple Factorial Program in C Language

Home » cse » A Simple Factorial Program in C Language

A Simple Factorial Program

Below is a very simple C program that finds the factorial of 6. Fire up your favorite editor and enter it. Do not copy the file or cut and paste: Actually type the code, because the act of typing will cause it to start entering your brain. Then save the program to a file named samp.c. If you leave off .c, you will get a Bad Magic Number” error when you compile it, so make sure you remember it.

/* Program to find factorial of 6 */ 
#include <stdio.h> 
#define VALUE 6 
int i,j; 
void main()  
    for (i=1; i<=VALUE; i++)      
    printf("The factorial of %d is %d\n",VALUE,j);  

When you enter this program, position #include and #define so that the pound sign is in column 1. Otherwise, the spacing and indentation can be any way you like it. On most UNIX systems, you will find a program called cb, the C Beautifier, which will format code for you.

To compile this code, type cc samp.c. To run it, type a.out. If it does not compile or does not run correctly, edit it again and see where you went wrong.

Now let’s look at the equivalent Pascal code:

{ Program to find factorial of 6 } 
program samp; 
    for i:=1 to value do      
    writeln('The factorial of ',value,' is ',j);  

You can see an almost one-to-one correspondence. The only real difference is that the C code starts with #include <stdio.h> . This line includes the standard I/O library into your program so that you can read and write values, handle text files, and so on. C has a large number of standard libraries like stdio, including string, time and math libraries.

The #define line creates a constant. Two global variables are declared using the int i,j; line. Other common variable types are float (for real numbers) and char (for characters), both of which you can declare in the same way as int.

The line main() declares the main function. Every C program must have a function named main somewhere in the code. In C, { and } replace Pascal’s begin and end . Also, = replaces Pascal’s := assignment operator. The for loop and the printf statement are slightly strange, but they perform the same function as their Pascal counterparts. Note that C uses double quotes instead of single quotes for strings.

The printf statement in C is easier to use than the Pascal version once you get used to it. The portion in quotes is called the format string and describes how the data is to be formatted when printed. The format string contains string literals such as The factorial of and \n for carriage returns, and operators as placeholders for variables. The two operators in the format string indicate that integer values found later in the parameter list are to be placed into the string at these points. Other operators include for floating point values, for characters, and for strings. You can type man printf to get the man page on formatting options.

In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains the operators and it must be followed by exactly three parameters, and they must have the same types in the same order as those specified by the operators.

This program is good, but it would be better if it read in the value instead of using a constant. Edit the file, remove the VALUE constant, and declare a variable value instead as a global integer (changing all references to lower-case because value is now a variable). Then place the following two lines at the beginning of the program:

printf("Enter the value:");  

The equivalent code for this in Pascal is:

write('Enter a value:');  

Make the changes, then compile and run the program to make sure it works. Note that scanf uses the same sort of format string as printf (type man scanf for more info). Also note the & sign in front of value. This is the address operator in C: It returns the address of the variable, and it will not make sense until we discuss pointers. You must use the & operator in scanf on any variable of type char, int, or float, as well as record types, which we will get to later. If you leave out the & operator, you will get a segmentation fault when you run the program.

C errors to avoid:

  1. Forgetting to use the & in scanf.
  2. Too many or too few parameters following the format statement in printf or scanf.
  3. Forgetting the */ at the end of a comment.