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

Lecture02_HelloWorld

The document provides an introduction to writing a simple C program that prints 'Hello World'. It explains key components of the program such as including libraries, the main function, the printf command, and the importance of comments and whitespace for readability. Additionally, it covers the concepts of syntax and semantics in programming.

Uploaded by

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

Lecture02_HelloWorld

The document provides an introduction to writing a simple C program that prints 'Hello World'. It explains key components of the program such as including libraries, the main function, the printf command, and the importance of comments and whitespace for readability. Additionally, it covers the concepts of syntax and semantics in programming.

Uploaded by

EG JP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Hello World

1
Your First Program

#include<stdio.h>

int main() {

printf(“Hello World\n”);
return 0;

2
#include<stdio.h>

‣ this includes a file called stdio.h which allows us


to use certain commands

‣ stdio is short for Standard Input / Output which


means it has commands for input like reading from
the keyboard and output like printing things on the
screen

3
main()

‣ is a function “called” by the operating system when


the program is executed

‣ the name of the point where the program starts

‣ entry point of every program

‣ the word main is followed by a pair of round


brackets

‣ all C programs must have a main function


4
{}

‣ a pair of curly brackets are used to group all the


commands together so it is known that the set of
commands inside the braces belong to the function
main

‣ used very often in C to group commands together

5
printf(“Hello World\n”);

‣ printf is the command used to display and print


text to the screen

‣ the data to be printed is put inside the round


brackets

‣ the command ends with a semi-colon

6
return 0;

‣ sends an exit signal/status to the operating system

‣ the end of the program has already been reached

‣ the command ends with a semi-colon

7
Comments
‣ serve as inline documentation

‣ provide supplementary information to better


understand a program

‣ ignored by the preprocessor directive and compiler

// this comment runs to the end of the line

/* this comment runs to the terminating


symbol, even across line breaks */

8
White Space
‣ spaces, blank lines and tabs

‣ used to separate words and symbols in a program

‣ extra white space is ignored by the compiler

‣ a valid C program can be formatted in many ways

‣ programs should be formatted to enhance readability


e.g using proper and consistent indentation

9
#include<stdio.h>
main() {
//a sample comment
printf(“Hello World\n”);
return 0;}

The code above is valid, but hard to read

10
RECALL
Software Development Method

Edit and save program

errors errors

Compile Program

Execute program and


evaluate results

11
RECALL
Syntax and Semantics
Syntax

‣ rules of a language on how we can put together symbols, reserved


words, and identifiers to make a valid program

Semantics

‣ define what a program statement means

‣ define the purpose or role of a program statement

A program that is syntactically correct is not necessarily semantically


correct

A program will always do what we tell it to do, not what we meant to


tell it to do
12
Questions?

13

You might also like