Output
Problem 1:
Write a program that prints out the following:
Hello World!
How would we go about solving this problem?
- Learn about some basic rules of the C language
- Learn what library will help us do output
- Learn how about functions and how to make a function call
- Learn how to compile a program into an executeable
Basic Rules
- Capitalization matters. The spelling is case sensitive, so there is a world of difference between INT, Int, and int. Make sure you are careful about capitalization.
- Spelling matters. Make sure your spelling is correct
- When a line ends matter. If your line is too long, there are ways to connect it to the line that follows, but in general each statement needs to be on its own line.
- Each statement typically starts on its own line and terminates with a semi-colon (;).
- Code readability tip: one statement per line, indent for each statement within a code block (area beginning and ending with {} )
- You can write notes about the code using comments. In C, comments begin with the symbols /* and end with */. The compiler will ignore these comments. However, as they will stay in your source code, you will be able to refer to them at a later date and remember what you were doing. For any program of any significant size, commenting (aka documentation) is a must.
Libraries
The C language is actually very small. To support it, there are a number of libraries that will help do advance tasks. In this case the task we want to accomplish is to print something, or produce output. The library that will help with that is the stdio library. stdio is short for standard input output.
In order to make use of a library you must first put the contents of the library's header file into your source code. The header file is named after the library and has a .h extension to the file name.
Finding and copying and pasting the contents each time would be awkward and also make your program really hard to read. Instead we use the #include statement to do that for us. In general, to include any of the standard libraries you would use the following line of code (changing libraryheader to the name of the library you want)
#include <libraryheader.h>
The #include statement is essentially a copy and paste command. It copies the contents of the file named and places it in the file where the #include statement is.
Generally #include statements are placed at the top of your file.
Functions
Every C program is made up of one or more functions. A function is a series of related instructions that work together to perform a task. A function will often accept arguments and produce a result. A function also has a name.
Every C program must have a function named main. It is where the program begins execution.
In general a C function has the following syntax:
returnType functionName(argumentType arg1,...){
}
returnType is the data type of the result a function will return. For the main function, this data type is int which is short for integer (signed whole number)
The function name for main, must be main. All lower case.
the argumentType for main is void. void means nothing. In other words the main function accepts no arguments. It is possible for main() to accept arguments but that is an advance topic that is not part of this book. For our purposes, main does not accept any arguments, and thus its only argument is void, indicating that it accepts nothing.
The curly brackets {} shows the start and end of the set of instructions, or a code block. In this case the code block are the instructions needed for the main function. Thus, everything between those two {} is part of the main function. Anything not between them is not.
Putting this all together, the main function will look like the following:
int main(void){
}
The code for performing the output will go between the { }.
printf
Each C library has a functions that will help accomplish related tasks. For the stdio library all the functions available have to do with input and output. One of these functions is printf. printf performs output.
Here is a link to a full reference for printf, we'll look more at it in detail later.
Here is the part we are interested in:
int printf( const char* format, ...);
the above is the function prototype for printf. It tells you what the function expects for arguments and what it returns. This will allow you to make a proper function call.
A function call within your program means you are asking a function to do something on your behalf. So, to print, we are going to call the printf() function to do some printing (output) for us.
From the printf() function prototype, we see that the first thing in the brackets is const char* format. Now, the char* part means a C string. The word const means that printf will not modify the string during the execution of the function. The word format is just a name so that the reference can more easily talk about this first parameter.
So what this all means is that what we give to the function must be a C string and that the string will not be changed by printf, so we can use a string literal. A string literal in C begins and ends with double quotes ("). The printf function will print this string.
So, if what we want it print is Hello World!, then what we should do is put that inside double quotes and give it to the printf function.
In effect, what we want is this:
printf("Hello World!");
So, if we do that, our program would now look like this:
#include <stdio.h>
int main(void){
printf("Hello World!");
}
Now... at this point your program can be compiled and it will run. Although there will be a small bug.
Once Hello World! is printed, the program will finish and give control back to the operating system which will print the command prompt on the same line.
What we want to do is to print Hello World! on one line by itself without anything after.
To do that, we need it to print a newline character after the exclamation mark. Unfortunately we cannot just hit return before the the ending double quote as our program will not compile. There are a number of special characters that cannot be just typed in. Below is a short list of some of the more common of these characters
character | what to type |
---|---|
newline | \n |
tab | \t |
backslash (\) | \\ |
percent sign (%) | %% |
double quote (") | \" |
beep | \a |
Thus, to ensure that our Hello World! is printed on a line by itself what we want to use is actually:
printf("Hello World!\n");
Return Statement
For each function that you write, you will often need to give back a result. In this case our main function will give back a result of 0 to indicate that everything went well. So the last statement in your main() function should be:
return 0;
Note that once a function reaches any line with return, it will immediately stop executing the function and go back to the calling function (ie the place where the function call was made). For us, our function call to main() will be made when we start our executeble. when the program is completed, the program hands back execution to the operating system. Thus, return 0; ends the program.
Our completed program:
#include <stdio.h>
int main(void){
printf("Hello World!\n");
return 0;
}
A Video Summary of above program:
Your First C program https://www.youtube.com/watch?v=eA96PBcclEsCompiling our program
Now that our program is completed We need to compile our program. To compile your program, open up a command prompt and go to the directory containing your program using the cd command. Make sure that the name of the file storing your program has a .c extension to it, rename it if it does not. In the instructions below, the assumption will be that your program is in the file hello.c
If it is not, you can sub in your file name.
To compile, follow the instructions as appropriate to your environment:
Linux
gcc hello.c
OSX
clang hello.c
Windows using visual studio.
cl hello.c
Here are some videos on the mechanics of compiling a program.
how to write and compile a program in windows how to write and compile a program on your mac