C Programming Basics Chapter 1 Lecture CSIS 10A - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

C Programming Basics Chapter 1 Lecture CSIS 10A

Description:

The CPU uses machine language. Assembly instructions to ... Experiment with line breaks. 24. Lets get moving. We'll learn as we proceed. You'll be saying 'Aha! ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 37
Provided by: solo50
Category:

less

Transcript and Presenter's Notes

Title: C Programming Basics Chapter 1 Lecture CSIS 10A


1
C Programming BasicsChapter 1 LectureCSIS 10A
2
Agenda
  • Hardware Basics ?
  • The IDE
  • My First Program
  • Its all G(r)eek to me
  • Variables and Declarations
  • Input and Output

3
Anatomy of a Computer
4
The CPU uses machine language
  • Assembly instructions to calculate the radius of
    a circle
  • LOAD radius
  • LOAD pi
  • MULTIPLY
  • LOAD two
  • MULTIPLY
  • STORE circle

5
Compilers to the Rescue !
  • High level languages like C allows writing code
    that is easier to understand and universally
    works on any CPU.
  • circle2.0radiuspi
  • The compiler is what translates instructions
    from C into Machine language.

6
Agenda
  • Hardware Basics
  • The IDE ?
  • My First Program
  • Its all G(r)eek to me
  • Variables and Declarations
  • Input and Output

7
THE MECHANICS OF WRITING A PROGRAM
  • Editing -- Writing a program
  • Compiling -- Translating from C into machine
    language
  • Linking -- Combining your program with other
    libraries
  • Running Letting the computer execute a program
  • Debugging Running step by step through a
    program searching for mistakes
  • The Integrated Development Environment (IDE)
    combines 1-5.

8
Agenda
  • Hardware Basics
  • The IDE
  • My First Program ?
  • Its all G(r)eek to me
  • Variables and Declarations
  • Input and Output

9
Hello World !
  • Lets start off the traditional way
  • Program that prints out Hello World on your
    output console (your screen)
  • Lets start off on our journey..

10
Steps
  • Create new source file
  • Write the code
  • Create a workspace (only in MSVC)
  • Compile
  • Link
  • Execute

11
Agenda
  • Hardware Basics
  • The IDE
  • My First Program
  • Its all G(r)eek to me ?
  • Variables and Declarations
  • Input and Output

12
Its all G(r)eek to me
  • actually, its C
  • Lets dissect and analyze a simple program

13
20,000 ft. above sea level
  • include ltiostreamgt
  • using namespace std
  • int main()
  • // This is my first program
  • cout ltlt "Hello World" ltlt endl
  • system("pause")
  • return 0

14
20,000 leagues under the sea
  • include ltiostreamgt
  • Actually includes some information into your code
  • Contains some definitions that are needed for
    your code
  • More of this later on in the course

15
22,000 leagues under the sea
  • using namespace std
  • This line refers to a set of standard object name
    definitions
  • For now, this is boilerplatestick it in cause
    it makes everything work!!

16
deeper.
  • int main()
  • This is the piece of code (function) that is
    operated on first when a program is executed
  • Whats a function ???
  • . all thats coming soon

17
even deeper.
  • //This is my first program
  • This is a comment you write to yourself
  • Useful when writing large programs
  • Starts with a //

18
and deeper..
  • Output operator
  • cout ltlt Hiltltendl
  • cout is the console monitor (your display)
  • ltlt is the output operator. Use to chain together
    your output message.
  • Hiis a string literal
  • endl means end-line (like enter key)

19
getting sleepy?..
  • For the Bloodshed environmentHold the display
    open so you can read it (pressing a key will
    continue) system("pause")
  • Finish up this program, return a 0 to operating
    system (everything ended OK)
  • return 0

20
and even deeper .
  • delineates the code block
  • Each line ends with a

21
Other types of cout statement
  • coutltlt my age is ltltendl ltlt 39ltltendl
  • coutltlt my age is 39
  • coutltlt whats yours
  • Escape with \
  • Output a coutltlt \ ltltendl
  • Output a endl coutltlt\n

22
Toms All Purpose Program Shell
  • include ltiostreamgt
  • using namespace std
  • int main()
  • // Your code here ...
  • system("pause")
  • return 0
  • Future slides may neglect some of the above
    components for clarity

23
You Do It
  • Modify your hello world program to print your
    name, address and phone number (COULD BE FAKE!)
    on three lines
  • Tom Rebold
  • 1600 Pennsylvania Ave
  • 123-456-7890
  • Experiment with line breaks

24
Lets get moving..
  • Well learn as we proceed
  • Youll be saying Aha! or Oho! when you hear
    these terms again
  • Lets play around a bit more

25
What more ?
  • Variables and Declarations
  • Getting User Input

26
Variables and Declarations
  • Variables represent storage locations in the
    computers memory
  • variable expression
  • Assignment is from right to left
  • n 5
  • Would give n the value 5

27
Using int Variables
  • int main()
  • int m,n
  • m 44
  • cout ltlt m ltlt m
  • n m 33
  • cout ltlt and n ltlt n ltlt endl

28
Clearing the haze
  • int m

m 44.0
44
29
Clearing the haze
  • int m 44, n

n m 33
44 33 77
77
30
Want more ?
  • Variables and Declarations
  • Getting User Input

31
User Input with cin
  • How shall I feed in data ?
  • Remember cout ?
  • Meet cin
  • cin gtgt m
  • Will put the value entered through the console
    (keyboard) into m

32
User Input with cin
  • int main()
  • cout ltlt "Enter the value of m"
  • cin gtgt m
  • cout ltlt "m " ltlt m ltlt endl

33
User Input with cin
  • int main()
  • cout ltlt Enter the value of m
  • cin gtgt m
  • cout ltlt m ltlt m ltlt endl

HEY THERES SOMETHING WRONG HERE !!!
34
User Input with cin
  • int main()
  • int m
  • cout ltlt "Enter the value of m"
  • cin gtgt m
  • cout ltlt "m " ltlt m ltlt endl

35
Your turn ()
  • from hello.cpp FilegtSaveAs age.cpp
  • Delete all the cout statements. Add lines to
  • a) Declare a variable called age
  • b) Display a message asking for data (such as
    tell me your age)c) read the data into age
    d) display the variable age with a descriptive
    message. (refer to slide 34)

36
Thats a wrap !
  • What we learned so far
  • How to write a basic C program
  • The structure of a program
  • Displaying to the console (cout)
  • Variables
  • Getting user input via the console (cin)
Write a Comment
User Comments (0)
About PowerShow.com