C Basics - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

C Basics

Description:

C++ Basics C++ is a high-level, general purpose, object-oriented programming language. – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 36
Provided by: minemercan
Category:
Tags: basics | brackets | math

less

Transcript and Presenter's Notes

Title: C Basics


1
C Basics
C is a high-level, general purpose,
object-oriented programming language.
2
C Basics
  • When  executing a program the computer only does
    what you have programmed ,
  • no less, but also no more.
  • If you forget anything, or the output does not
    look pretty, or the results are incorrect,
    there's nobody to blame
  • but yourself.

3
C Basics
  • The computer understands (long) sequences of 0's
    and 1's, humans only understand a natural
    language such as English.
  • Therefore, there is a translation mechanism
    involved when you try to tell the computer what
    you want it to do

4
C Basics
TRANSLATION
Compiler
Object code
Source code
5
C Basics
  • Source code The source code is a text document
    that contains statements written according to the
    rules
  • Object code A sequence of 0's and 1's that the
    computer can, in principle, understand (machine
    code).
  • A program called the compiler is used to
    translate the source code into object code.
  • Executable code Sequence of 0's and 1's,
    consisting of your object code plus any other
    modules (machine code).

6
C Basics
TRANSLATION
Other files
Compiler
Linker
Object code
Source code
Executable code
7
C Basics
  • When you write C programs,
  • There are several things to keep in mind
  • Your program does exactly and only what you
    define, no more and no less
  • C programs execute, from top to bottom
  • C programs can use cin to get input from a
    user, and cout to display output
  • C programs usually do some computations

8
Basic C Rules
  • every C program uses certain keywords that have
    special meaning (e.g. int, main, void, return)
  • every C program uses certain operators that
    perform specific actions (e.g. , , cin, cout)
  • every C program is case-sensitive,(e.g. int is
    different from Int or INT)
  • every C program uses curly brackets to group
    statements together
  • every C program has a semicolon at the end of
    every instruction almost
  • every C program that handles user input and
    output will contain the line include ltiostream.hgt

9
Basic C Rules
  • almost every C program will contain the lines
    ..   int main(void)         .....
  • return 0   

10
Declaring Variables
  • C provides several different basic data types.
    The most important ones are
  • double a decimal number
  • int an integer number
  • char a single letter or special symbol,
    anything that is on your keyboard

11
Declaring Variables
  • Example
  • double x 
  • int X,y, z 
  • char d
  • C is case-sensitive
  • Variable names can not contain spaces. They must
    start with letters, and can contain only letters,
    numbers, and certain special symbols such as an
    "underscore" _.

varName
type
12
Assigning values to variables
  • Once a variable is declared, you can assign
    values to it.
  • varName expression
  • Example
  • double x
  • x 10.0

expression
varName
Note that this operation looks like the math
symbol for equal, but it works differently
13
Assigning values to variables
  • When you assign an expression to a variable, the
    following happens
  • first,
  • the value of the right side is computed
  • second,
  • that computed value is assigned to the variable
    on the left

14
Assigning values to variables
  • Example
  • double x, y
  • int i, k
  • x 2.8
  • y -1.4x
  • i 9
  • k (i 2) (7 5i)

the value of the right side is computed
computed value is assigned to the variable on the
left
15
Assigning values to variables
  • Combined Declaration and Assignment
  • In C, you can declare a new variable, and at
    the same time assign a value to it (or initialize
    the variable)
  • Example
  • double x 1.0
  • int i 10, j 20
  • int k i j

16
Defining Constants
  • To define a constant in C you preface the type
    of the variable by the keyword const.
  • Example
  • const double pi 3.1415

17
Defining Constants
  • A constant
  • can not change inside your program.
  • usually declared at the beginning of your
    program,
  • must be assigned a value at the time you declare
    them.

18
  • Now we can produce some more interesting programs.

19
A sample program
  • Task 1 Create a program that asks the user for
    the radius of a disk, then computes the area and
    circumference.

20
A sample program
  • Stage 0 As usual, our stage-0 program is
  • include ltiostream.hgt
  • int main()   
  • ......  
  • return 0

21
A sample program
  • Stage 1 We use comments to break up our task
    into smaller subtasks
  • include ltiostream.hgt
  • void main()
  •    // get the radius from the user
  •    // compute the area
  •    // compute the circumference
  •    // display the answers

22
A sample program
  • Stage 2 Now we get into the details of which
    variables and formulas to use
  • include ltiostream.hgt
  • int main()
  • const double pi 3.1415
  •    // need a variable r for the radius
  • double r
  •    // getting the input from the user
  •    cin gtgt r
  •     // computing the area A pi r2   
  • double A pi r2
  •    // computing the circumference
  •    double C 2 pi r
  •     // displaying both answers
  •    cout ltlt A
  •    cout ltlt C
  • return 0

23
A sample program
  • Stage 2 Now we get into the details of which
    variables and formulas to use
  • include ltiostream.hgt
  • int main()
  •    const double pi 3.1415
  • double r,A,C
  •    cin gtgt r
  •    A pi r2
  •    C 2 pi r
  •    cout ltlt A
  •    cout ltlt C
  • return 0

24
A sample program
  • At this point, we let the compiler tell us if the
    C grammar is correct or not.
  • The compiler will tell us the r2 is "unknown",
    so we change that line to
  • A pi rr

25
A sample program
  • Then we compile it again. It will now compile
  • So we can link it to produce the executable file.
  • Finally, we execute the program to test it, and
    we find that everything works, but it does not
    look good.
  • So, we'll modify the program inputoutput one
    more time.

26
A sample program
  • Stage 3 We add some more input/output statements
    to make our program more "appealing" to the user
  • include ltiostream.hgt
  • int main()
  •    const double pi 3.1415
  • double r,A,C
  • cout ltlt "Please enter the radius "
  •    cin gtgt r
  •    A pi rr
  •    C 2 pi r
  • cout ltlt "The area is "
  •    cout ltlt A
  • cout ltlt "The circumference is "  
  •    cout ltlt C
  • return 0

27
A sample program
  • Acutally, the last four statements can be linked
    together. Instead of saying
  •   
  • cout ltlt "The area is "
  •    cout ltlt A
  •    cout ltlt "The circumference is "  
  •   cout ltlt C
  • we can also say
  • cout ltlt "The area is " ltlt A ltlt " and the
    circumference is " ltlt C

28
A sample program
  • After changing that, our program will work
    correctly, and produce reasonably nice looking
    results on the screen.
  • include ltiostream.hgt
  • int main()
  •    const double pi 3.1415
  • double r,A,C
  • cout ltlt "Please enter the radius "
  •    cin gtgt r
  •    A pi rr
  •    C 2 pi r
  • cout ltlt "The area is " ltlt A ltlt "The
    circumference is " ltlt C
  • return 0

29
Software Development
  • When creating a program, you usually proceed in
    four distinct stages
  • Problem Analysis
  • Design
  • Coding
  • Verification and Validation

30
Stage 1 Problem Analysis
  • In this stage you analyze what exactly it is that
    your program needs
  • In particular, you describe
  • all input values,
  • i.e. values that must be supplied from outside
    the program
  • all constant values,
  • i.e. values that are given with the problem
  • all output values,
  • i.e. values that must be produced as part of the
    solution to the problem
  • You should also think about the types of all
    these values.

31
Stage 2 Design
  • In this stage,
  • break up the problem into subtasks in the order
  • write the pseudocode, using comments to describe
    the subtasks instead of actually coding it.
  • All necessary formulas are part of this stage.

32
Stage 3 Coding
  • In this stage
  • enter the code for your program, following the
    rules that C requires.
  • leave the comments from stage 2, and put your
    code after the respective comments.
  • The end product of this stage should be a program
    that compiles and links without errors.

33
Stage 4 Verification and Validation
  • Check if your program works correctly,
  • Is the sequence of events correct ?
  • Does your program print out enough information to
    guide the user as to what to do and what the
    output means ?

34
1.Problem
  • Create a program that will compute the volume of
    a sphere, given its radius.

35
2.Problem
  • You are working as a consultant for a cable
    company. For each installation that is performed
    by the company, there's a 25.00 service charge
    and an additional 2.00 charge per meter of cable
    used.
  • They need a program to compute the total income
    per month.
  • In other words, if they use 263 meter of cable
    at 27 different locations, they make 1201.00
    income.
Write a Comment
User Comments (0)
About PowerShow.com