Your First Steps in C Programming - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Your First Steps in C Programming

Description:

Reserved words with strict meanings. C does a lot with ... Name of Character. The character has a value of 0. Char. char cVal = 'a'; printf('%c', cVal) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 40
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Your First Steps in C Programming


1
Your First Steps in C Programming
  • Some Syntax

2
Comments
  • Arbitrary strings placed between the delimiters
    / and /.
  • /
  • A comment can be written in this fashion
  • to set it off from the surrounding code.
  • /
  • The compiler changes each comment into a single
    blank character.

Use comments in your code! It makes coding a lot
simpler!
3
An Example
  • / This program does not do much ... its an
    example /
  • include ltstdio.hgt
  • int main(void)
  • int a, b, c
  • float x 0.0f, y 3.3f, z -7.7f
  • printf( Input two integers )
  • scanf( dd, b, c )
  • a b c
  • x y - z
  • printf( d d d\n, b, c, a )
  • printf( f - f f\n, y, z, x )
  • return 0

4
VariablesandData Types
5
What is a Variable?
  • A space in the computers memory which
  • Belongs to my program.
  • Has a name.
  • Can store stuff for me.
  • Lets me read the stuff that I stored there.
  • This is how we store data in an application.

6
Fundamental data types
  • int
  • Short for integer.
  • Stores integer numbers.
  • float / double
  • Store real numbers (numbers with decimal points).
  • double is more accurate than float.
  • char
  • Short for character.
  • Stores characters.

7
Fundamental Data Types
  • Common ones
  • int float double char
  • Less common
  • unsigned int
  • Rarely used
  • short long ...

8
An Example
  • / This program does not do much ... its an
    example /
  • include ltstdio.hgt
  • int main(void)
  • int a, b, c
  • float x 0.0f, y 3.3f, z -7.7f
  • printf( Input two integers )
  • scanf( dd, b, c )
  • a b c
  • x y - z
  • printf( d d d\n, b, c, a )
  • printf( f - f f\n, y, z, x )
  • return 0

Defining variables
9
Variables
  • A variable is a space in memory reserved by your
    application.
  • You can access the variable through its name.
  • You can retrieve the value of the variable and
    you can change its value through assignment.
  • int main()
  • int a, b, c
  • float x 0.0f, y 3.3f, z -7.7f
  • ...

Memory
???
a
3.3
y
Application
10
Variables
  • Before you can use a variable, you must declare
    it.
  • Give the variable a name.
  • Define its data type.
  • int main()
  • int a, b, c
  • float x 0.0f, y 3.3f, z -7.7f
  • ...

11
Variables
  • Variables
  • Defined at the beginning of a scope.
  • Recognized anywhere within the scope.
  • Once outside the scope the variable dies.
  • void main()
  • ...
  • ...
  • ...
  • ...

A scope begin with and ends with
12
Variable Names
  • Variables names can include
  • English Letters (lowercase and uppercase)
  • Numbers
  • An underscore (_).
  • Rules
  • First character cannot be a number!
  • Case sensitive.
  • Some names are already taken keywords.

2temp is illegal
temp2 is different from Temp2
13
Keywords
  • Reserved words with strict meanings.
  • C does a lot with relatively few keywords.

14
Always Initialize Variables
Always initialize variables!!! Variables always
have a value, even if you dont initialize them.
The initial value is usually meaningless
(garbage). People, for some reason, assume that
this value is zero ... its not!!!
15
Give Meaningful Names
Give variables meaningful names!!! What do the
varibles a, i, x, t store? What do the variables
angle, time, ctr store?
16
Things You Can Do With Variables
x is assigned the value 5
  • x 5
  • a b c
  • y y 1
  • z y 5

a is assigned the value of b c
The value of y is increased by 1
z is assigned the remainder of y / 5
17
Things You Can Do With Variables
  • / This program does not do much ... its an
    example /
  • include ltstdio.hgt
  • int main(void)
  • int a, b, c
  • float x 0.0f, y 3.3f, z -7.7f
  • printf( Input two integers )
  • scanf( dd, b, c )
  • a b c
  • x y - z
  • printf( d d d\n, b, c, a )
  • printf( f - f f\n, y, z, x )
  • return 0

Assignment during declaration
Assignment and math operators
Printing variable values
18
Fahrenheit to Celsius
  • include ltstdio.hgt
  • int main()
  • double cels0.0, fahr0.0
  • printf("Please enter a fahrenheit
    temperature\n")
  • scanf("lf",fahr)
  • cels 5(fahr-32)/9
  • printf("Is equal to lf degrees celsius\n",
    cels)
  • return 0

Definition with initialization
Computation and assignment
19
Char
  • for some reason this requires
  • additional explanation

20
Char
  • char data type
  • Represents a character.
  • One byte in size.
  • Stores the characters as a number.
  • Ascii Table
  • Converts a number to a character (and
    vice-versa).
  • char c A ? char c 65

21
Char
The character \0 has a value of 0
22
Char
  • char cVal 'a'
  • printf("c", cVal)
  • printf("d", cVal)
  • printf("ccc", cVal, cVal1, cVal 2)

a is printed
97 is printed
abc is printed
23
Variable Limits
24
compute the size of some fundamental types
  • run on PC Pentium 3"
  • The size of some fundamental types is computed.
  • char 1 byte
  • short 2 bytes
  • int 4 bytes
  • long 4 bytes
  • unsigned 4 bytes
  • float 4 bytes
  • double 8 bytes
  • long double 8 bytes

25
Going over the Limit
  • include ltstdio.hgt
  • int main(void)
  • int i 0
  • unsigned u UINT_MAX
  • for ( i 0 i lt 5 i )
  • printf( u d u\n, u, i, u i )
  • return 0

Typically equal to 4294967295
26
Going over the Limit
  • 4294967295 0 4294967295
  • 4294967295 1 0
  • 4294967295 2 1
  • 4294967295 3 2
  • 4294967295 4 3

The variable restarts itself
27
Float Representation
  • include ltstdio.hgt
  • int main()
  • int i 0
  • float f 0
  • for ( i 0 i lt 100 i )
  • f 0.01f
  • printf( f\n, f )
  • return 0

Due to the way a float / double are stored in
memory, there are precision errors.
Output 0.999999
28
Special Float Values
  • NaN Not a Number - represents an illegal value.
  • printf(f\n, sqrt(-1))
  • will print -1.IND00 or NAN
  • INF infinity - will print 1.INF00

29
Printf Scanf
  • Youre going to use them a lot!

30
Printf
  • Writes data to the standard output (stdout)
  • Arguments are filled with corresponding values
    requested in the format string.
  • d ? int
  • c ? char
  • f ? float
  • lf ? double

31
Scanf
  • Reads data from the standard input (stdin)
  • Arguments are filled with corresponding values
    requested in the format string
  • Return Value  The number of items successfully
    read.  If EOF is returned an error has occurred
    before the first assignment could be done
  • Whitespace tab, space, newline (CR) (isspace())

32
A Simple scanf Example
  • include ltstdio.hgt
  • int main()
  • int n
  • char c
  • while (scanf("d , c",n, c) 2)
  • printf ("d, c\n",n,c)
  • return 0

Dont forget the '' Its very important!! Well
talk about it later in the course.
SimpleScanf.c
33
A Simple scanf Example
  • SAMPLE INPUT SAMPLE OUTPUT
  • 123,a 123, a
  • 123 , a 123, a
  • 123 123, a
  • ,
  • a
  • 123a lterrorgt

34
Math
35
Something to Remember
  • When doing a math on variables of different
    types
  • The computer converts the variables to the more
    accurate type.
  • 7 / 2.0 ? 3.5
  • When doing math on variable of the same type
  • All computation remain within the type
  • 7 / 2 ? 3

36
Casting
Treating a value of one type as if it were of
another type.
  • double d 3.3
  • int i 0
  • unsigned ui 0
  • i (int)d
  • d (double)i / 2
  • ui (unsigned)i

i 3
d 1.5
37
Mathematical Functions
  • sqrt()
  • pow()
  • exp()
  • log()
  • sin()
  • cos()
  • tan()

The functions are declared in ltmath.hgt
All the functions use doubles
38
Square Root and Power
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main()
  • double x 0
  • printf( "Input x \n" )
  • scanf( "lf", x )
  • if ( x gt 0.0 )
  • printf("x lf\n", x)
  • printf("sqrt(x) lf\n", sqrt(x))
  • printf("pow(x, x) lf\n", pow(x, x))
  • else
  • printf( "Sorry, your number must be
    nonnegative.\nBye Bye\n" )
  • return 0

This is the interesing part
SqrtPow.c
39
The Result of the Program
  • The square root of x and x raised to the x power
    will be computed.
  • Input x 2
  • x 2.000000
  • sqrt(x) 1.41421
  • pow(x, x) 4.000000
Write a Comment
User Comments (0)
About PowerShow.com