C Introduction

C Language

Link to Table of Contents:

TOC

Introduction

To learn programming, one have to read examples and do practices. So, we are going to start our tutorial with the hello, world! program in C.

#include <stdio.h>               /*Notes are recorded like this in C.*/

int main()
{
  printf("hello, world!\n");
}

The result will be when executed:

hello, world!

As you see, this is the C program that prints out hello, world! on the Terminal. You can experiment with it for a little while.

Notes are recorded between /* and */ in C language. Notes will not affect the program's process, but only for you and other programmers to understand your program.

The first line in the program tells the computer to include a header file called stdio.h (STanDard Input/Output). This file is important as it tells the computer what to do when it sees commands like printf and scanf that you will use in the program. Be sure to put that line in the beginning of you program (we will also be including other header files as the program gets more complicated). (We will discuss this more in Preprocessors)

The int main() line defines a function in your C program. (Functions break the program, a big task, into smaller tasks. For example, a calculator program may have functions Add, Subtract, Multiply, and Divide.) This function is called main, and it is required to be present in every C program. It is where the computer starts when it executes your program. The contents of the function is included in the braces. Be sure to indent the contents in braces to make your program structure clear visually (this is very important that some languages, like Python, requires this to be done and depend on this instead of braces).

Last, the printf statement tells the computer to print a string (a series of characters like "hello, world!\n"). Inside the quotation marks is your string that you want to print to the Terminal. The '\n' character is the newline character. It tells the computer to use a new line when printing something else. Lines like this one always ends with a semicolon ';'.


Practice

Play around with the hello, world! program. What will happen if you take away the #include line? What about the '\n' in printf? Try putting in a different string to print.

Try executing these programs:

#include <stdio.h>

int main()
{
  printf("Special characters:\n\tThis is tab\n12345\bBackspace\n");
}

#include <stdio.h>

int main()
{
  printf("C");
  printf("Programming");
  printf("Language");
}

Additional practices on printf statements and the very basics of C:

Activity 2.2.2:

Characteristics of C

As you observe in the previous examples, a C program:



TOC Next >>