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

Lecture - 03 - Fundamental of Programming

1) The document discusses a simple C program that prints the text "This is my first program". It explains the key elements of the program including comments, preprocessor directives like #include, the main function, and output statements like printf. 2) Another section discusses a C program that takes in two numbers from the user using scanf and prints the numbers. It covers variable definitions and declarations, data types, and identifiers. 3) The document provides examples of escape sequences, using multiple printfs, printing multiple lines with one printf, and defining and declaring variables. It aims to explain fundamental concepts for beginners learning C programming.

Uploaded by

Lisa Allisya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lecture - 03 - Fundamental of Programming

1) The document discusses a simple C program that prints the text "This is my first program". It explains the key elements of the program including comments, preprocessor directives like #include, the main function, and output statements like printf. 2) Another section discusses a C program that takes in two numbers from the user using scanf and prints the numbers. It covers variable definitions and declarations, data types, and identifiers. 3) The document provides examples of escape sequences, using multiple printfs, printing multiple lines with one printf, and defining and declaring variables. It aims to explain fundamental concepts for beginners learning C programming.

Uploaded by

Lisa Allisya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

TMF1414

1
Introduction to Programming
Lecture 03: Fundamental of Programming
A Simple c program
2
3 A Simple C Program

Output
4 A Simple C Program
 Lines 1 and 2

 begin with //, indicating that these two lines are comments.

 You insert comments to document programs and improve program readability.

 Comments do not cause the computer to perform any action when the program
is run.
 Comments are ignored by the C compiler and do not cause any machine-
language object code to be generated.
 Comments also help other people read and understand your program.
5
A Simple C Program
 You can also use /*…*/ multi-line comments in which
everything from /* on the first line to */ at the end of the line is
a comment.

 We prefer // comments because they’re shorter and they


eliminate the common programming errors that occur with
/*…*/ comments, especially when the closing */ is omitted.
6 A Simple C Program
Blank Lines and White Space
 Line 3, 5, 7, and 9 are simply a blank line. You use blank
lines, space characters and tab characters (i.e., “tabs”) to
make programs easier to read.
 Together, these characters are known as white space. White-
space characters are normally ignored by the compiler.
7 A Simple C Program

#include Preprocessor Directive

 Line 4

#include <stdio.h>

 is a directive to the C preprocessor.


8
A Simple C Program
 Lines beginning with # are processed by the preprocessor before
compilation.
 Line 4 tells the preprocessor to include the contents of the standard
input/output header <stdio.h> in the program.
 This header contains information used by the compiler when
compiling calls to standard input/output library functions such as
printf, scanf etc.
A Simple C Program

Instead of <stdio.h>
there are many other
preprocessor
directives in C
language
You will learn other
preprocessor
directives from time to
time in other chapters

9
10 A Simple C Program

The main Function

 Line 6

int main( void )

 is a part of every C program.

 The parentheses after main indicate that main is a program building


block called a function.
11
A Simple C Program
 C programs contain one or more functions, one of which must be
main( ).
 Every program in C begins executing at the function main().
 The keyword int to the left of main indicates that main( ) “returns”
an integer (whole number) value.
12 A Simple C Program
A Simple C Program

For now, simply include the keyword int to the left of main in each of
your programs.

Functions also can receive information when they’re called upon to


execute.

The emptied in parentheses ( ) here refer to void which means that


main( ) does not receive any information.
13
A Simple C Program

 A left brace { , begins the body of every function (line 6).


 A corresponding right brace } ends each function (line 11).
 This pair of braces and the portion of the program between the braces
is called a block.
14 A Simple C Program
An Output Statement
 Line 8
printf(“This is my first program \n");

 instructs the computer to perform an action, namely, to print on the


screen the string of characters marked by the quotation marks.
 A string is sometimes called a character string, a message or a
literal.
15
A Simple C Program
 The entire line, including the printf function (the “f” stands for
“formatted”), its argument within the parentheses and the semicolon
; , is called a statement.
 Every statement must end with a semicolon (also known as the
statement terminator).
 When the preceding printf statement is executed, it prints the
message This is my simple code on the screen.
 The characters normally print exactly as they appear between the
double quotes in the printf statement.
16
A Simple C Program
Escape Sequences
 Notice that the characters \n were not printed on the screen.
 The backslash (\) is called an escape character.
 It indicates that printf is supposed to do something out of the ordinary.
 When encountering a backslash in a string, the compiler looks ahead at the
next character and combines it with the backslash to form an escape
sequence.
 The escape sequence \n means newline.
 When a newline appears in the string output by a printf, the newline causes
the cursor to position to the beginning of the next line on the screen.
Common escape sequences
17
Escape Name Description
sequence
\n Newline Causes the cursor to go to the next line for
subsequent printing
\t Horizontal tab Causes the cursor to skip over to the next tab stop

\a Alarm Causes the computer to beep


\b Backspace Causes the cursor to back up, or move left one
position
\r Return Causes the cursor to go to the beginning of the
current line, not the next line
\\ Backslash Causes a backslash to be printed
\’ Single quote Causes a single quotation mark to be printed
\” Double quote Causes a double quotation mark to be printed

WARNING! When using escape sequences, do not place a space between backslash and the control
character.
18 A Simple C Program

Reserved Word

 In program 3.1, there are few reserved word for C language


 int
 Return

 This reserved words are already defined and they cannot be


redefined to mean anything else
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned

19
A Simple C Program:
Reserved words in c
20 A Simple C Program
Using Multiple printfs
 The printf function can print This is my first program in several different ways.
 For example, the program 3.2 produces the same output as the program 3.1
 This works because each printf resumes printing where the previous printf stopped
printing.
 The first printf (line 8) prints This is my followed by a space and the second printf
(line 9) first program begins printing on the same line immediately following the
space.
21 A Simple C Program (Using Multiple printfs)

Output
22 A Simple C Program: Printing a Line of Text
(Cont.)
 One printf can print several lines by using additional newline
characters as in Program 3.3
 Each time the \n (newline) escape sequence is encountered, output
continues at the beginning of the next line.
23
A Simple C Program (Print Multiple lines
using single printf)

Output
Another Simple C Program:
Entering Two Numbers
Printing the address of variables
25 Another Simple C Program: entering Two
numbers
 Our next Program 3.4 uses the Standard Library function scanf to
obtain two numbers typed by a user at the keyboard, prints both
numbers using printf.
26

Another Simple C
Program: entering
Two numbers

Output
27 Another Simple C Program: entering Two
numbers
Variables and Variable Definitions
 Lines 8–9
int number1;
int number2;

 are variable definition / declaration.


 The names number1, and number2 are the names of variables—locations in memory where
values can be stored and use by a program.
 These definitions specify that the variables number1, and number2 are of type int, which
means that they’ll hold integer values, i.e., whole numbers such as 7, –11, 0, 31914 and the
like.
28 Another Simple C Program: entering Two
numbers
 All variables must be defined with a name and a data type before they can be
used in a program.
 The preceding definitions could have been combined into a single definition
statement as follows:
int number1, number2;

 but that would have made it difficult to describe the variables with corresponding
comments as we did in lines 8–9.
29 Another Simple C Program: entering Two
numbers
A declaration is a syntactical element that associates a type with a
program entity, such as a variable.

Variables

int number1; OR int number1, number2;


int number2;

Data type
30 Another Simple C Program: entering Two
numbers
Identifiers and Case Sensitivity

 A variable name in C is any valid identifier.


 An identifier is a series of characters consisting of letters, digits and underscores
(_)
 Variable cannot begin with a digit – must begin with letter or underscore
 is case sensitive—uppercase and lowercase letters are different in C, so a1 and
A1 are different identifiers.
 Cannot contain any special characters like $, #, &, *, @, etc.
31 Another Simple C Program: entering Two
numbers
Data Types

 A data type is a set of data values and a set of operations on those values.
 Usually has three classes of data types:

a) Built-in data types / primary data types

b) Programmer-defined/user-defined data types

c) Derived data types


Another Simple C Program: Entering Two
32
numbers
Another Simple C Program: Entering Two
33 numbers
C Program: Entering Two numbers
42

Output
43 Another Simple C Program: entering Two
numbers

Value 7 10
Data type & Variable int number1 int number2
Address 62FE4C 62FE48
56

Thank ANY

You QUESTIONS?

You might also like