C for Engineers - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

C for Engineers

Description:

{ printf('Hello, world!n'); return 0; must be included so we can use ... { printf('Hello, '); printf('world!n'); #include stdio.h /* Another first C program ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 13
Provided by: andrew84
Category:
Tags: engineers | hello

less

Transcript and Presenter's Notes

Title: C for Engineers


1
  • C for Engineers

2
A Simple C Program
must be included so we can use the printf()
function printf() comes from the stdio library
include / A first C program / int
main() printf(Hello, world!\n)
return 0
a comment
functions return a value whose type is known
every C program begins by executing a function
called main()?
printf() is a function that prints to the
standard output device ie, screen, terminal,
monitor
\n is a symbol that means new line, what we
used to call carriage return, line feed.
blocks of code are surrounded by braces ...
text inside double quotes are called strings
3
More Details
  • C code is declared in files that have a .h
    extension, called a header file
  • C code is defined in files that have a .c
    extension
  • C programs consist of functions that call (ie
    use) other functions
  • The file that contains the function main() has no
    header file.

int a int foo(int x)
a 4 int foo(int x) return x
4 ... a foo(a)
4
Example
include / A second C program / int
a 4 int foo(int x) return x 4 int
main() printf(a d\n,a) a
foo(a)? printf(a d\n,a) return 0
5
Exercise
  • Read the following web page and try its
    examplesExperiment with specifying limited
    output digits.

http//www.cs.utah.edu/zachary/isp/tutorials/io/i
o.html
printf("\.2 .2f\n",1.23456789) printf("\.2
.2f\n",12.3456789) printf("\.2
.2f\n",123.456789) printf("\.2
.2f\n",1234.56789) printf("\.2
.2f\n",12345.6789) printf("\.2
.2f\n",123456.789) printf("\.2
.2f\n",1234567.89) printf("\.2
.2f\n",12345678.9) .2 1.23 .2 12.35 .2
123.46 .2 1234.57 .2 12345.68 .2
123456.79 .2 1234567.89 .2 12345678.90
6
Special Characters
  • A normal character, like n, can be turned into a
    character with special meaning, like \n, by
    putting a back-slash before it.
  • Some Special Characters
  • \t - horizontal tab
  • \r - carriage return
  • \a - rings a bell
  • \\ - puts a single back-slash inside a string
    this back-slash has no power to enhance the
    following character.
  • \ - puts a double quote inside a string.

printf(\Hello, world!\\n\) prints
out Hello, world!\n
7
Examples
  • Compare

include / A first C program / int
main() printf(Hello, world!\n)
include / Another first C program
/ int main() printf(Hello, )
printf(world!\n)
include / Another first C program
/ int main() printf(Hello,\nworld!\n)
Hello, world!
Hello, world!
Hello, world!
8
A More Complicated Program
  • This program has both standard input (keyboard)
    and standard output (monitor). It adds two numbers

x tells scanf() to put the integer it has just
read in into the location whose name is x.
include int main() int x, y,
z printf (Enter an integer\n)
scanf(d,x) printf (Enter another
integer\n) scanf(d,y) z xy
printf(The sum of d and d is d,x,y,z)
The scanf() function is also declared in the
stdio.h file. It reads from the standard input
device usually the keyboard.
the d string is called a format control
string. d is called a conversion specifier and
tells scanf() to expect to read an integer
9
Complicated Stuff
  • scanf() works only with x while printf() can
    work with just plain x.
  • The reason is that scanf() changes the value of x
    while printf() does not change the value of x. If
    printf() could change the value of x we would
    have to use x there as well.

10
Complicated Stuff (cont)
  • The C programming language distinguishes between
    x the name of a place in memory where we can
    store a value and x the address of that
    place in memory.
  • Suppose a classmate, John, lives at 186 Main
    Street and we all know that. Everybody
    understands when we say we are going to John's
    apartment.
  • However, an external agent like USPS would need
    to told to make a delivery to 186 main St, New
    Paltz, John's place wouldn't do.
  • scanf() is like an external agent inside main().
  • If you are mailing a letter a return address of
    John's place would not hinder the letter from
    being delivered.

11
Variables in Memory
int x 4
x
4
a variable name is like label for a location in
memory
12
Sample Program
  • A program that prompts for 2 numbers and prints
    out the average of the two

include int main() int num1,
num2 printf(Enter two numbers separated by
a comma) scanf(d,d,num1,num2)
printf(The average of d and d is
f\n,num1,num2,(num1num2)/2.0) Enter two
numbers separated by a comma 3,4 The average of 3
and 4 is 3.5
the conversion specifier f says to expect to
print a real number
divide by 2.0 and not 2 to be sure the result is
real and not just an integer.
these must match the input must look exactly
like the format control string d,d
Write a Comment
User Comments (0)
About PowerShow.com