C Programming Unit 1
C Programming Unit 1
Here is how C code can be commented to add notes that you do not want the
compiler to see, but that will make the code easier to understand on later review.
//Note that I can create comments in my code by typing two forward
slashes and
//everything from the slashes to the end of the line will be considered
comments
/* I can also use slash-star to begin a comment and everything after that
becomes a single continuous comment
until I end the comment with a star-slash */
Our Second C Program
When a line begins with a # (hash mark or pound sign) that is called a
preprocessing directive. The compiler scans the code before compiling and follows
the instructions given by preprocessing directives.
In this case, it is an include directive. An include finds the C code in the file name
given and inserts it as the position of the INCLUDE as if it were typed in along with
your code. This allows the C programmer to take advantage of functions that have
already been written without re-inventing the wheel.
The include directive here compiles in the header file containing functions for
Standard I/O in C
We also now have content within the main() function that actually does something.
One line commands within a function must end with a semi-colon (;).
The printf command tells the program to print something to the screen, in the this
case, the string "Hello Word!\n"
Note the backslash-n inside the print string. The \n is a command for New Line. If
this command were not in the string, when the program stopped running, the
command prompt would be on the same line as the program output, which could be
confusing. The New Line command provides a fresh new line for the command
prompt to appear on after the program runs.
==========================
Open your text editor again to write a program called helloworld.c
vi helloworld.c
Press i to go into -- INSERT -- mode
Type the following:
#include <stdio.h>
main ()
{
printf("Hello world!\n");
}
Type <Esc> then :wq to save and exit.
Now compile.
gcc -o helloworld.out helloworld.c
Now run.
./helloworld.out