0% found this document useful (0 votes)
4 views

Lecture Two

Uploaded by

alvinmwangisir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture Two

Uploaded by

alvinmwangisir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIVERSITY OF EMBU

SCHOOL OF PURE AND APPLIED SCIENCES


DEPARTMENT OF MATHEMATICS, COMPUTING & IT

STA 121: PROGRAMMING METHODOLOGY

WRITTEN BY: EDITED BY:


MR JOHN KIRWA

Copyright © UNIVERSITY OF EMBU, AUGUST 2020


All Rights Reserved

LESSON 2: COMPILING A SIMPLE C PROGRAM


Understanding the basic C Structure

Compiling a C Program

Now, let us try to apply the basic elements above to compile a C program, instructions are
provided below.

#include <stdio.h>

The line in the above program which starts with a # symbol are called directives and is
instructions to the compiler. The word ‘include’ with '#' tells the compiler to include the
file stdio.h into the file of the above program. File stdio.h is a header file needed for input/ output
requirements of the program. Therefore, this file has been included at the top of the program.

int main ( )

The word ‘main’ is a function name. The brackets ( ) with main tells that main ( ) is a function.
The word ‘int’ before main ( ) indicates that integer value is being returned by the function main
(). When the program is loaded in the memory, the control is handed over to function main ( )
and it is the first function to be executed.

Curly bracket and body of the function main ( )

A C-program starts with a function called main (). The body of the function is enclosed between
curly braces. The program statements are written within the brackets. Each statement must end
by a semicolon, without which an error message is generated.

printf("Hello World!");

This statement prints our "Hello World!" message on the screen.

return 0;

This is a new type of statement, called a return statement. When a program finishes running, it
sends a value to the operating system. This particular return statement returns the value of 0 to
the operating system, which means "everything went okay!".
Printing Multiple Lines of Text with a Single Statement

The characters print exactly as they appear between the double-quotes. However, if we type \n,
the characters \n are not printed on the screen. The backslash (\) is called an escape character. It
indicates that a "special" character is to be output. When a backslash is encountered in a string of
characters, the next character is combined with the backslash to form an escape sequence. The
escape sequence \n means newline. It causes the cursor to move to the beginning of the next line
on the screen.

/* This program illustrates how to print multiple lines of text

with a single statement */

#include <stdio.h>

int main()

printf("Welcome\nto\nC");

return 0;

The following table gives a listing of common escape sequences.

Escape Sequence Description

\n Newline

\t Horizontal tab
\a Bell (beep)

\\ Backslash

\' Single quote

\'' Double quote

Related links

The development of C language by Dennis M. Ritchie

DJGPP a complete 32-bit C/C++ development system for Intel 80386 and higher PCs.

List of Free C Compilers and interpreters available on the internet.

https://www.sanfoundry.com/c-programming-examples/

You might also like