Foundations of Programming with C: Lesson 1

Posted on September 12, 2009. Filed under: C, C Programming | Tags: , , , , , , , , , , , , , , , , , |

Welcome to programming with C.  This tutorial is the first of many to come in C programming and is important to learn for the foundations of other programming languages, mostly for the syntax and logic of programming with computer languages.

Before we begin lets talk about what tools we will need a compiler.  You can Google c compiler to find any but I recommend Borland C++ compiler.  Be sure to read the instructions on how to use this program as I will not be referring to any specific program how how to compile and execute.

Lets begin.

Lesson 1: First C Program

Usually when using a compiler program and opening a new C file you will be given:

# include <stdio.h>


main()
{
printf("Hello, world\n");
}

This is a C program that displays one line. “Hello, world!”

The first line in this program is #include <stdio.h> is the code that will tell the following function where to find the library of all the definitions the program will be asking for. This declaration is important because without it your program will not function.

The next section main() declares where the main part of the program begins.  (note the main is spelled all in lower case, all programs written in c are case sensitive.)

Next lets look at the printf(“Hello, world\n”); line.  Here we learn that in proper C syntax you typically end a statement with a semi-colon (;), in contrast to the English language where you would end a sentence with a period(.).  This particular statement is a printf statement, which in C means that something is to be displayed.  In other words printf is telling your computer to create an output on to your screen or “print” on to your screen.  It is unsure to what the meaning of the f at the end of the statement means.  Some speculate that it means you have some control over the format of what gets displayed where others say it is there just to make the program look more intimidating!.

The bracket after the printf contains the data that is to be displayed.  In this case the data is contained within double quotation marks (“), which is used to tell the C compiler that what is between the quotes is not C, but a bunch of characters to be displayed, or a character string.

The \n at the end of the character string is an technique for including special characters that are not part of the output.  These special techniques always begin with a backwards slash (\) and are followed by one or more characters.  In this case, the special character \n is  called the newline character and causes the display to advance to the beginning of the next line.

Here is a list of some other commonly used characters:

\a – beep (a stands for “alarm”)
\b – backspace
\f – form feed (usually only affects printer output)
\t – go to the next tab stop (usually every 8 columns)
\\ – output backslash

Why don’t you try out these other special characters and also adding a second printf function that will display “This program was made by yourname”.  Keep trying until it works out.

Click here for sample executable
Click here for source code

Click here for next lesson: Input, Output, and Variables (coming soon)

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

  • Categories

  • Top Clicks

  • SocialVibe


Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.