Introduction to Programming in C - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Introduction to Programming in C

Description:

Programming in C . Chapter 2. Introduction to Programming in C. Hello World Program ... printf ('Hello World.n'); return 0; ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 12
Provided by: drjames82
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming in C


1
Chapter 2
  • Introduction to Programming in C

2
Hello World Program
  • / Program 1 - A first Program. Multiline
    Comments
  • Name D.L. Sonnier
  • Class CSC 140
  • Date 25 August 2006
  • PLEDGED
  • /
  • include Header
  • Access to standard library
  • //main() is where program execution begins.
    Single Line Comment
  • main()
  • main() begins here
  • printf ("Hello World.\n") console output
  • main() ends here

3
Program Compile and Run
  • ab1234compscilab cc prog1.c
  • ab1234compscilab ./a.out
  • Also
  • ab1234compscilab gcc prog1.c
  • ab1234compscilab ./a.out
  • To compile to an executable by the name of
    something other than a.out, do
  • ab1234compscilab cc -o prog1 prog1.c
  • ab1234compscilab ./prog1

4
Program General Form
  • / Program X - Description
  • Name D.L. Sonnier
  • Class CSC 140
  • Date
  • PLEDGED
  • /
  • include
  • directives
  • //Description
  • main()
  • statements

5
Return Statement
  • include
  • main()
  • printf ("Hello World.\n")
  • return 0
  • Note main() is a function, and as such it
    should always return a value. Make this return
    statement standard.

6
Using Variables
  • // Program 2 Using a Variable
  • include
  • main()
  • int value // value is a variable of type int
  • value 1023 // assigns 1023 to value
  • printf("This program prints the value ")
  • printf("d\n", value)
  • return 0

7
Using Variables
  • // This program does conversion of gallons to
    liters
  • include
  • int main()
  • int gallons, liters // two variables of type
    int
  • printf("Enter number of gallons ")
  • scanf("d", gallons) // console input
  • liters gallons 4 // convert to liters
  • printf("Liters d" , liters)
  • return 0
  • // IS THIS VERY ACCURATE??

8
Using Variables
  • // This program does conversion of gallons to
    liters
  • include
  • int main()
  • float gallons, liters // two variables of type
    float
  • printf("Enter number of gallons ")
  • scanf("f", gallons) // console input
  • liters gallons 3.7854 // convert to liters
  • printf("Liters f" , liters)
  • return 0
  • // Much more accurate

9
Classroom Project
  • Problem 3, p. 30

10
Classroom Project
  • We have an urgent need for a computer program
    which will calculate the area of a square
  • Your program should prompt for the base b and
    height h as input then it will calculate and
    print out the area.

h
b
11
Review
  • All programs have a main() function, and that is
    where program execution begins
  • All variables must be declared before they are
    used
  • C supports a variety of data types, including
    integer and floating point
  • For output use printf() statement
  • For input use scanf() statement
  • Program execution stops when the end of main() is
    reached (or return 0)
Write a Comment
User Comments (0)
About PowerShow.com