Getting Started With Coding

Download Report

Transcript Getting Started With Coding

Chapter 3
By:
Mr. Baha Hanene
Learning Outcomes
We will cover the learning outcome 02 in this chapter
i.e.
 Use basic data-types and input / output in C programs.
(L02)
2
Contents
 Introduction to Programming
 Stages & Steps of Writing Programs
 Preprocessor Directives
 Main Function
 Output Statement
 Variables in C
 Assignment Statements
 Sample Programs
 References
3
Programming Introduction
 A language which is used to instruct computer to
perform different tasks according to the requirements.
 Every programming language has following basic
things.
 Declarations
 Assignments
 Control
 I/O
Statements
(Input/output)
Statements
4
Programming Introduction
 Any programming language consists of its own
keywords and syntax rules that distinguishes it from
other programming languages.
 If you want to program in a specific language, you
need the compiler of this specific language.
 For example, C language compiler translates programs
written in C language to machine language.
5
Programming Style
 Keep it clear & simple
 Keep it short
 Think it through in advance
6
Stages & Steps of Writing Programs
Specification
Design
Coding
Testing
Documentation
Maintenance
7
Preprocessor Directives
 In C programming whenever you see lines that begin
with a
in column 1 are called preprocessor
directives (commands)
 The #include<stdio.h> directive allows the processor
to include basic Input / Output in the program at this
point.
#
8
stdio.h
 This header file we include in all programs where we
need to use basic I/O commands e.g.
 printf(“”);
scanf(“”);
 Some functions are very difficult and very long to
write, these header files contains built in functions to
provide us help in writing the programs.
9
void main( )
 Every C program has a main function, this is where the
program starts it’s execution, as this is the first
function to execute in the whole c program.
 The reserved word “void” shows that this function will
not return any value.
 Each C program can contain only one main function
e.g.
void main()
10
Function Body
 A left brace (curly bracket) -- { -- begins the body of
BOD
Y
every function. A corresponding right brace -- } -ends the function body.
void main()
Main Function
{
……
……
}
11
printf(“”);
 This line is called a C statement
 printf(“”); is a function which is used to instruct computer to
display something on computer screen
 All text, special symbols you will type inside printf(“”); between
the double quotations i.e. “……..” will be displayed as it is on your
computer screens except few commands like /t, /n, %i etc. e.g.
 printf(“Welcome at KIC”);
 Notice that the above line ends with a Semicolon
statement ends with a semicolon i.e. ;)
;
(every C
12
Sample #include<stdio.h>
Program Explanation
Header File used for Input / Output
void main()
Function Start
Main Function Necessary
to start any C program
{
Output Statement
used to display on printf(“Hello to programming”);\n Start New Line
screen
printf(“\n Welcome at KIC”); \t
Give Space between text
printf(“\t at Fall 2010/11”);
Function End
}
13
Variables in C
 In computer programming, a variable is a name given to
some known or unknown quantity or value, for the purpose
of allowing the name to be used independently of the value
it represents
 [Data Type][Variable Name] ;
Data Types: In C programming there are more data types
but we generally use the following data types. (more details in
next chapter)
1. int2. float
3. char
14
Variables in C
 Variable Name: You can give any name to the variables in c
but there are few rules for writing variable names.
 (A-Z) The first character must be a letter
 Space is not allowed between names of identifiers
 (&*^%$#@) Special symbols are not allowed in identifier
names
 - Hyphens are not allowed in identifier names however
underscores _ can be used
 (int, float, private etc.) The reserve words are not allowed in
identifier names.
15
Variables in C
Some valid identifiers / variable names
hello j9 box22a get_data bin3d4
count
 Some reserve words (can’t be used as identifier):
private else static int const float
16
Variables
in C
velocity
freda
time-of-day
int
tax_rate
x2
4x
first value
radio@4
emp.
velocity
freda
time_of_day
INVALID
tax_rate
x2
x4
first_value
radio4
emp
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
WRONG
WRONG
WRONG
WRONG
17
Variables in C
 How & where we write variables in C programming….?
 We usually declare all variables inside main function
for example:
#include<stdio.h>
Data Type
void main()
{
int avar ;
int salary ;
float percentage ;
}
Variable
Name
Semi Colon (every C
statement ends with a
semicolon ;)
18
Sample Program
Multiple Variable Declaration
Any
Questions
?
???
?????
Assignment
Statements
19
Assignment Statements
 Before using variables in assignment statements you should
define / declare them first e.g. int a;
 Assignment Statements
 first_number = 35;
 second_number = 21;
 sum = first_number + second_number;
 The first two statements assigns the values 35 and 21 to the
variables first_number and second_number respecitively
 The next statement assigns the sum of the two values to the
variable sum.
20
Sample Program
Header File
Main Function
Variable Declaration
Function
Body
Assignment Statements
Screen Output
21
Displaying
Variable
Value
 Do you see something different in normal printf
statements and these which we have seen in the last
sample program???
%d These are called Format
Specifiers used to display the
value stored in the variable i.e.
Inside computer memory
What are
these
things????
22
Sample Program Output
23
The Logical
Flow
Source Program
Compile
Syntax Error
OK
Object Code/ Program
Run
Run-time Error
OK
Result
Logical Error
OK
Continue
END
24
References
 http://en.wikipedia.org
25