Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

CS101 Lecture 04 - Fundamentals of Programming

The document provides an overview of the basic structure of a C program, including comments, preprocessor directives, functions, statements, variables, and control structures. It explains key aspects like identifiers, keywords, whitespace, and semicolons. Examples are given to demonstrate how to write simple C programs to perform tasks like adding two numbers and displaying the output.

Uploaded by

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

CS101 Lecture 04 - Fundamentals of Programming

The document provides an overview of the basic structure of a C program, including comments, preprocessor directives, functions, statements, variables, and control structures. It explains key aspects like identifiers, keywords, whitespace, and semicolons. Examples are given to demonstrate how to write simple C programs to perform tasks like adding two numbers and displaying the output.

Uploaded by

John
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

CS101 - PROBLEM SOLVING &

PROGRAMMING

Lecture 4 (online)
Introduction to Computers & Programming
Week 11 May – 15 May 2020
Zoubeir Aungnoo
Lecture Aims

■ Recap on some basics of C

■ Programming language Structure


–The basic structure of a C program
■ Escape sequence
■ Program input and output
■ Documentation: comments
■ Identifiers
■ Keywords
■ whitespace
C Language - Overview
■ C is a general-purpose, high-level language that was originally developed by Dennis
M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally
first implemented on the DEC PDP-11 computer in 1972.
■ In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard.
■ The UNIX operating system, the C compiler, and essentially all UNIX application
programs have been written in C. C has now become a widely used professional
language for various reasons −
– Easy to learn
– Structured language
– It produces efficient programs
– It can handle low-level activities
– It can be compiled on a variety of computer platforms
Facts about C
■ C was invented to write an operating system called UNIX.
■ C is a successor of B language which was introduced around the early 1970s.
■ The language was formalized in 1988 by the American National Standard
Institute (ANSI).
■ The UNIX OS was totally written in C.
■ Today C is the most widely used and popular System Programming
Language.
■ Most of the state-of-the-art software have been implemented using C.
■ Today's most popular Linux OS and RDBMS MySQL have been written in
C.
Why use C?
■ C was initially used for system development work, particularly the programs that make-up the operating
system.
■ C was adopted as a system development language because it produces code that runs nearly as fast as the
code written in assembly language.
■ Some examples of the use of C might be −
– Operating Systems
– Language Compilers
– Assemblers
– Text Editors
– Print Spoolers
– Network Drivers
– Modern Programs
– Databases
– Language Interpreters
– Utilities
C Programs

■ A C program can vary from 3 lines to millions of lines and it


should be written into one or more text files with extension ".c";
for example, hello.c.
■ You can use "vi", "vim" (for Linux) or “NotePad” (for
Windows) or any other text editor to write your C program into
a file.
– Note: we will be using an IDE: Dev C++.
– You may use CodeBlocks as well if you prefer.
Local Environment Setup

■ If you want to set up your environment for C programming


language, you need the following two software tools available
on your computer,
– (a) Text Editor and
– (b) The C Compiler.

■ Both are present in an IDE


The C Compiler

■ The source code written in source file is the human readable


source for your program.
■ It needs to be "compiled", into machine language so that your
CPU can actually execute the program as per the instructions
given.
■ The compiler compiles the source codes into final executable
programs.
■ The most frequently used and free available compiler is the
GNU C/C++ compiler, otherwise you can have compilers either
from HP or Solaris if you have the respective operating systems.
Structure of a C Program

■ Consider:
■ Output is??
Structure of a C Program

■ Here’s the output:


Structure of a C Program
1. Line 1: Comments
– Lines starting with // are ignored
– Used to explain logic and give info.
– Which other lines are comments?

2. Line 2 & 3: Pre-processor directives


– #include <studio.h>
– #include <stdlib.h>
– C uses libraries of std functions which are included when we
build programs. (refer to Lecture 02 slides 16 – 24)

3. Line 5 – 8: comments (again)


– We have 2 types of comments: single line & multi line
– Line 5 – 8 are multi line comments
– Text surrounded by /* .. */ are multi line comments
– ALL comments are IGNORED by the compiler
Structure of a C Program
4. Line 10: comments again
5. Line 11: The main function
– int main(int argc, char *argv) {
..
}
– entry point of any C program
– It starts at line 11
– It finishes at line 19
– So, curly braces { } indicate the start and end of functions and blocks
of code.
6. Line 14: a tool used to display text info.
– printf(“ … “);
– whenever you want to display text on the screen, write it within the
double quotes inside the rounded braces.
– NOTE: every statement (line) ends with a semi-colon ( ; ) in C
■ There are exceptions e.g. line 2 – 3 for the pre-processor directives
Structure of a C Program
6. Line 14: a tool used to display text info.
– printf(“ … “);
– whenever you want to display text on the screen, write it within the
double quotes inside the rounded braces.
– NOTE: every statement (line) ends with a semi-colon ( ; ) in C
■ There are exceptions e.g. line 2 – 3 for the pre-processor directives
– printf(“Welcome to C! \n”);
– What is meant by \n ?

7. Line 17: indication of successful completion


– return 0;
– This tells the compiler that the main
function has successfully completed its
execution. So, the main function can be
exited.
A C Program – E.g.#2
■ What is the output?
■ How many statements are used for the output?
A C Program – E.g.#3
■ Is line 5 a valid line of code (loc)?
■ What is the output if any?
■ How many statements are used for the output?
So, what is a C Program?

A C program basically consists of the following parts (tools):


•Pre-processor Commands
•Functions
•Statements & Expressions
•Comments
•Variables (more on that later)
•Control Structures (Sequence, Selection & Repetition)

•That’s it, a C program is just a mixture of all the tools which


act upon the LOGIC YOU WRITE!
Semicolons

■ In a C program, the semicolon is a statement terminator. That is,


each individual statement must be ended with a semicolon. It
indicates the end of one logical entity.

■ Given below are two different statements −

printf("Hello, World! \n");


return 0;
Comments

■ Comments are like helping text in your C program and they are
ignored by the compiler. They start with /* and terminate with
the characters */ or // as shown below −

/* this is a multi-line comment */

// this is a single line comment


Identifiers
■ Are names given to various program elements such as variables,
functions and arrays.
■ Identifiers consists of letters and digits in any order except that the
first character must be a letter.
■ Both lower and upper case are permitted
■ C is case sensitive. So, Salary and salary are two different
identifiers
■ The underscore ( _ ) can be included and is used mostly to link 2
words. E.g. gross_salary
■ An identifier may also begin with an underscore but this is rarely
done in practice
■ Write meaningful identifier names
Identifiers

■ Some valid identifiers ■ Some invalid identifiers:


x”
Mohd order-no
zara
error flag
abc
move_name 3fold
a_123
myname50
_tempj
a23b9
retVal
Keywords
■ The following list shows the reserved words in C. These reserved words may not be
used as constants or variables or any other identifier names.

auto else long switch


break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
■ Keywords have their own special purpose. Use them only for their intended purpose!
default if static while
do int struct _Packed
double
Whitespace in C
■ Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments. C compiler ignores them.
■ Whitespace separates one part of a statement from another and
enables the compiler to identify where one element is in a
statement. E.g.
int age;

■ On the other hand, in the following statement


fruit = apples + oranges; // get the total fruit
■ no whitespace characters are necessary between fruit and =, or
between = and apples, although you are free to include some if you
wish to increase readability.
■ This statement is also valid but less readable:

fruit=apples+oranges;// get the total fruit


A C Program – E.g.#4
■ Let’s write a program to add two integer (whole) number and display the
sum.

■ Code:

■ Output:
A C Program – E.g.#4
■ Let’s write a program to add two integer (whole) number and display the
sum.
■ printf("%d \n", sum);
– %d is a placeholder. Whenever we want to display an integer in the
printf(), we write %d

■ Output:
A C Program – E.g.#5
■ Let’s write an improved version of a program to add two integer (whole)
number and display the sum.
■ Write the Pseudocode for it

– Begin
– DISPLAY “Enter Number 1: “
– READ number1
– DISPLAY “Enter Number 2: “
– READ number2
– CALCULATE sum = number1 + number2
– DISPLAY sum
– End
A C Program – E.g.#5
■ The corresponding program in C:
A C Program – E.g.#5
■ The output:

■ Try with other inputs.


■ Does it work with floating point (decimal) values??
– Try with 60.4 and 12.25
– What happen?
– How to solve this?
– Hint: look for an appropriate place holder
The Tool For User Input – scanf()

■ scanf() is the tool we use for a user to input data into a program
■ scanf("%d", &number1);
■ scanf() needs two arguments (options) to work:
1. placeholder – indicates the type of input
– E.g. %d indicates the input data to be a decimal (integer)
– E.g. %c indicates the input data to be a single character
2. Container – indicates where to store the input
– E.g. &number1 means to store the input value into the variable
(container) called number1.
– But how do we find out where this variable is in memory??
– The ampersand (&) gives the address of the specified variable

You might also like