A C program starts out as follows:
(You can write comments in a C program by using /* and */, as shown above)
/* Write your code here */
To make an executable file out of this C program, you have to compile it. First off, create a file called "prog.c", then copy and paste the above into it. You compile it as follows:
gcc prog.c -Wall -ansi -pedantic -o prog
I'll explain the commandline switches:
-Wall : This tells the compiler to give every warning possible
-ansi : This tells the compiler to follow the ANSI standard of C
-pedantic : This tells the compiler to follow the ANSI standard of C to the letter and to tell you about every little violation your program contains
-o prog : This tells the compiler that the executable file should be called "prog"
After you've compiled the program, you'll have a file called "prog". To run the program, just type "prog" and hit Return. (Our program is empty at the moment so it won't actually do anything). For now, that's all you need to know about compilation, so back to the actual programming:
"main" is a function, and it happens to be the function that is called when the program begins execution. When main finishes, your program is over.
In computer programming, there is the concept of a "variable". When you define a "variable", you're setting aside a little section of memory to use for storage. For instance, you might want to use a variable to store an integer number. To define a variable, you specify its type followed by its name. So for instance, let's say you want to store an unsigned integer ("unsigned" means that it cannot be negative); to do this you'll want to define a variable of type "unsigned int":
"unsigned int" is the type, and "i" is the name of the variable. In C, the default variable type is "int", so you can shorten "unsigned int" to "unsigned". We can set the value of "i" and also do mathematical operations on it:
unsigned int i;
(You'll notice in C, every statement is followed by a semi-colon, i.e. ";")
i = 4;
i = i + 2;
i = i * 5;
Some of the built-in types of C are as follows:
char = Store a character (such as a letter, digit, or symbol)
int = Store an integer
float = Store a floating-point number (e.g. a number such as 36.782)
Let's say we want to write a program to convert Fahrenheit to Celsius. In order to do this, we have to be able to ask the user to input a number using the keyboard, and also we have to be able to print the result to the screen. The "standard library" of C contains functions for doing this. The functions that are involved with user input and output are declared in the header file "stdio.h". In order to gain access to these functions, we have to "include" that header file. Here's a program that will print "I like monkeys" to the screen:
As you can see, you can use the function "printf" to write to the screen, and you enclose the text in inverted commas. In programming, we refer to text as a "string" (reason being that it's a string of characters in a sequence). The "\n" at the end of the string represents a new line. If you don't have a new line at the end of the string, then "printf" might not print the string to the screen right away, instead it will hold the string in a buffer to print to the screen at a later time (e.g. when it encounters a new line). On Windows, "printf" writes to the screen straight away regardless of whether you have the '\n'. On Linux, you need the '\n'.
printf("I like monkeys\n");
In order to read input from the user, you can use the function "scanf". "scanf" can read in all sorts of input from the user, it can read an integer number, a floating point number, a character. For our program to convert Fahrenheit to Celsius, we want to use a floating-point variable to store the temperature. Here's the beginnings of it (complete with line numbers):
I'll explain what each line does:
0: #include <stdio.h>
2: int main(void)
4: float i;
6: printf("Enter the temperature in Fahrenheit: ");
9: scanf("%f", &i);
11: return 0;
4: This creates a variable called "i", and its type is "float".
6: This prints a string to the screen.
7: As discussed previously, printf might not print to the screen right away if there's no newline, instead it will store the string in a buffer for later. In order to make sure this buffer gets printed to the screen immediately, you can flush the standard output stream; you do this by calling the function "fflush" and passing "stdout" as an argument to the function ("stdout" stands for "standard output").
9: This line reads in a floating-point number from the user and stores it in the variable "i". The string "%f" looks funny, but it's just how you tell scanf what type you want to read in (if you want to read in an unsigned integer, you do "%u"). Also you might notice that we have "&" before "i", this is called the "address of" operator, and it's used to get the address of a variable ("scanf" wants to know the address of "i" because it wants to store the number in that part of memory).
So to write our program to convert Fahrenheit to Celsius, we first have to figure out how to actually convert Fahrenheit to Celsius. What you do is as follows:
1) Subtract 32
2) Multiply by 5
3) Divide by 9
So altogether here's our program:
You'll see here that we use "%f" again with "printf". Here we use it to print the value of "i" to the screen. When using printf, you don't need to give the address of "i" because printf doesn't need to store anything at that memory address, instead you can just give it the number directly.
printf("Enter the temperature in Fahrenheit: ");
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
This program we have is a fully-portable program to the ANSI standard of C. What this means is as follows:
1) It doesn't contain anything that is in violation of the Standard
2) It will work as intended on every implementation of the ANSI standard of C