08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1 ■ Elementary Programming Concepts

We add special words such as if, else and while to form statements such as

if (a > 0) b = a + 2; else b = a – 2;

And we put statements together to form programs.

1.6 Welcome to C Programming

We take a quick peek at the C programming language by writing a program to print the message

Welcome to Trinidad & Tobago

One solution is Program P1.2.

Program P1.2

#include <stdio.h>

int main() {

printf("Welcome to Trinidad & Tobago");

}

The statement

#include <stdio.h>

is called a compiler directive. This simply means that it provides information the compiler needs

to compile your program. In C, input/output instructions are provided by means of standard

functions stored in a standard library. These functions use variable (and other) declarations

stored in a special header file called stdio.h. Any program that uses an input/output instruction

(such as printf) must inform the compiler to include the declarations in the file stdio.h with

the program. If this is not done, the compiler will not know how to interpret the input/output

statements used in the program.

A C program consists of one or more functions (or, subprograms), one of which must be

called main. Our solution consists of just one function so it must be called main. The (round)

brackets after main are necessary because, in C, a function name is followed by a list of arguments,

enclosed in brackets. If there are no arguments, the brackets must still be present. Here, main has

no arguments so the brackets alone are present. The word int before main indicate the type of

value returned by main. We will explain this in more detail later.

Every function has a section called the body of the function. The body is where the work of

the function is performed. The left and right braces, { and }, are used to define the start and end,

respectively, of the body. In C, one or more statements enclosed by { and } is called a block or

compound statement.

The body of main contains one statement:

printf("Welcome to Trinidad & Tobago");

12

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!