Your First Steps in C Programing - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Your First Steps in C Programing

Description:

The memory associated with the variable stores the value. ... A for loop has three parts: for ( initialize ; condition ; incrament ) ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 72
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Your First Steps in C Programing


1
Your First Steps in C Programing
  • Hello World

2
Hello World
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

This simple program prints the string Hello,
world! to the standard output (the screen in
most cases)
3
include
Include the standard functions declaration
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

How do we use code that we wrote in a different
file here? The include declaration lets us do
that.
4
main Function Definition
Function definition. A function is a block of
statements that has a name and can be called from
elsewhere in the application. A function can
recieve arguments and can return a single value.
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

main is a special function. It is the MAIN
function. This is where the application starts
running. It has several resriction, which well
talk about in the future.
5
Curly Brackets
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

Curly Brackets. These define an enclosed block of
statements. They define a scope.
For readability, statements within a scope are
tabbed to the right (it makes it easier to
understand where the scope begins and
ends). Variables defined within a scope are not
recognized outside of it.
6
printf
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

Print the string Hello, world! to the standard
output.
printf is a function. It is declared in the
stdio.h file. It recieves as input a string of
chars and prints them to stdout.
7
return
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

Return the value 0.
0 means success ... no errors occured. Any
statements written after a return statement are
meaningless.
8
Semi-colon
  • include ltstdio.hgt
  • int main(void)
  • printf ( Hello, world!\n )
  • return 0

Semi-colon
The statement terminaning character. All
statement MUST end with a (there are several
exceptions).
9
Lets Look At Another Example
  • Variables and Formatted Printing

10
The distance of a marathon in kilometers
  • include ltstdio.hgt
  • int main(void)
  • int miles, yards
  • float kilometers
  • miles 26
  • yards 385
  • kilometers 1.609 (miles yards / 1760.0)
  • printf ( \nA marathon is f kilometers.\n\n,
    kilometers )
  • return 0

11
Variable Declaration
  • include ltstdio.hgt
  • int main(void)
  • int miles, yards
  • float kilometers
  • miles 26
  • yards 385
  • kilometers 1.609 (miles yards / 1760.0)
  • printf ( \nA marathon is f kilometers.\n\n,
    kilometers )
  • return 0

Variable declaration
Variables are the way we manipulate data in our
program. They are names associated with some
memory. A variable has a specific data type which
tell the computer how to interpret the memory.
12
Variable Assignment
Variables are assigned values. Values can
assigned more than once, however only the last
assignment counts. The memory associated with the
variable stores the value. Anytime we use the
variable we are actually using its value.
  • include ltstdio.hgt
  • int main(void)
  • int miles, yards
  • float kilometers
  • miles 26
  • yards 385
  • kilometers 1.609 (miles yards / 1760.0)
  • printf ( \nA marathon is f kilometers.\n\n,
    kilometers )
  • return 0

Variable value assignment
13
Formatted Printing
  • include ltstdio.hgt
  • int main(void)
  • int miles, yards
  • float kilometers
  • miles 26
  • yards 385
  • kilometers 1.609 (miles yards / 1760.0)
  • printf ( \nA marathon is f kilometers.\n\n,
    kilometers )
  • return 0

printf means print formatted. f and other such
characters are place holders for values of a
specific type. f means that it is going to print
a float variable value. \n is the newline symbol.
Formatted Printing (printf)
14
Yet Another Example
  • Loops and other stuff

15
Printing Example
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main (void)
  • printf ( "i equals 0\n" )
  • printf ( "i equals 1\n" )
  • printf ( "i equals 2\n" )
  • printf ( "i equals 3\n" )
  • printf ( "i equals 4\n" )
  • return 0

Five printf statement each with a different
string. Not the nicest looking code.
16
Remarks
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main (void)
  • printf ( "i equals 0\n" )
  • printf ( "i equals 1\n" )
  • printf ( "i equals 2\n" )
  • printf ( "i equals 3\n" )
  • printf ( "i equals 4\n" )
  • return 0

Remarks make the code more friendly. The computer
ignores the remarks but programers can read them
and understand what the code does.
Remarks begin with / and end in a /. Anything
in between these symbols is ignored. Remarks can
span several lines.
17
For Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main (void)
  • int i
  • for ( i 0 i lt 10 i i 1 )
  • printf ("i equals d\n", i)
  • return 0

Loops are a way of repeating the same
instructions over and over again. C provides some
flexible ways of deciding how many times to loop,
or when to exit a loop.
A for loop.
18
For Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main (void)
  • int i
  • for ( i 0 i lt 10 i i 1 )
  • printf ("i equals d\n", i)
  • return 0

A for loop has three parts for ( initialize
condition incrament ) The loop terminates when
the condition evaluates to false (zero).
for loop definition.
19
For Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main (void)
  • int i
  • for ( i 0 i lt 5 i i 1 )
  • printf ("i equals d\n", i)
  • return 0

The printf statement will be repeated 5 times.
Each time the value of the variable i is
different. d means int value.
Repeated printf
20
While Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main(void)
  • int i 0
  • while ( i lt 5 )
  • printf ("i equals d\n", i)
  • i
  • return 0

21
While Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main(void)
  • int i 0
  • while ( i lt 5 )
  • printf ("i equals d\n", i)
  • i
  • return 0

A while (condition) loop repeats the following
block of statement while the condition evaluates
to tree. Anything that can be done with a for
loop can be done with a while loop and vice
versa. The initialize part is done before the
loop and the incrament part is done within the
loop.
while loop definition.
22
While Loops
  • / This program prints a list of integers, from 0
    to 4 /
  • include ltstdio.hgt
  • int main(void)
  • int i 0
  • while ( i lt 5 )
  • printf ("i equals d\n", i)
  • i
  • return 0

Initialize the i variable
Incrament the i variable
23
Last One I Promise
  • If statements, scanf, functions and more.

24
Print the Larger of Two Numbers
  • / The program receives 2 numbers from the user
    and prints the larger of the two /
  • include ltstdio.hgt
  • int main(void)
  • float x1, x2
  • printf( Please enter 2 numbers\n )
  • scanf( f f\n, x1, x2 )
  • if ( x1 gt x2 )
  • printf( The first number f is larger!\n,
    x1)
  • else
  • printf( The second number f is larger (or
    equal) to the first\n, x2 )

25
scanf
  • / The program receives 2 numbers from the user
    and prints the larger of the two /
  • include ltstdio.hgt
  • int main(void)
  • float x1, x2
  • printf( Please enter 2 numbers\n )
  • scanf( f f\n, x1, x2 )
  • if ( x1 gt x2 )
  • printf( The first number f is larger!\n,
    x1)
  • else
  • printf( The second number f is larger (or
    equal) to the first\n, x2 )

scanf scans formatted text from the standard
input (usually keyboard) into variables. The
characters have the exact same meaning as with
printf.
Scan formatted text
26
if else statements
  • / The program receives 2 numbers from the user
    and prints the larger of the two /
  • include ltstdio.hgt
  • int main(void)
  • float x1, x2
  • printf( Please enter 2 numbers\n )
  • scanf( f f\n, x1, x2 )
  • if ( x1 gt x2 )
  • printf( The first number f is larger!\n,
    x1)
  • else
  • printf( The second number f is larger (or
    equal) to the first\n, x2 )

An if else statement is simply that. If
(condition) evaluates to true than the block of
code following the if statement is executed. If
the condition evaluates to false then the else
block of code (if it exists) is executed.
if ... else statement
27
Again But Lets Use Functions
  • / The program receives 2 numbers from the user
    and prints the larger of the two /
  • include ltstdio.hgt
  • int isLarger( float x1 ,float x2 )
  • return (x1 gt x2)
  • The main is part of the same file due to lack
    of space its in the next slide

Function Definition
The function isLarger recieves 2 float values and
returns the integer value of the evaluation of
the condition is (x1 lt x2). Boolean variables are
not part of the C language.
28
Functions
  • int main(void)
  • float x1, x2
  • printf( Please enter 2 numbers\n )
  • scanf( f f\n, x1, x2 )
  • if ( isLarger( x1, x2 ) )
  • printf( The first number f is larger!\n,
    x1)
  • else
  • printf( The second number f is larger (or
    equal) to the first\n, x2 )

Calling the isLarger function
29
The C System
  • The preprocessor
  • The standard library

edit hello.c compile hello.o
link hello
30
Some Syntax
31
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.
  • In this course we do not use the c style!!!
  • // This is a comment in c

32
Keywords
  • Reserved words with strict meanings.
  • C does a lot with relatively few keywords.
  • Some implementations on some systems may have
    additional keywords.

33
Identifiers
  • A token composed of a sequence of letters
  • tax price tax_rate
  • First character cannot be a digit!!
  • Case sensitive
  • Some identifiers are already taken keywords.
  • Choose name that are meaningful.

5a_cse is illegal
CSE_5a is different from cse_5a
Bad Examples cse_5a a_VERY_VERY_long_name
34
Constants
  • 0
  • 0123
  • 123456789000
  • 0123
  • -49
  • 123.0

too large?
an octal integer
a constant expression
a floating constant
35
String Constants
  • a string of text
  • a b c
  • a string with double quotes \ within
  • a string backslash \\ is in this string
  • 2 string constants separated by white space are
    concatenated
  • "abc" "def" is equivalent to "abcdef"

The NULL string (empty string)
Nothing is executed ... this is simply a string
36
Operators and Punctuators
the expression a plus b
  • ab
  • a_b

a 3-character identifier
37
Increment, Decrement Assignment Operators
  • int a 0, b 0, c 0
  • a c
  • b c
  • printf( d d d\n, a, b, c )
  • b 2
  • c 3 a (b 2) (c 3)
  • a b c

1 1 2 is printed
These are equivalent
38
Fundamental Data Types
  • int, float, char and other

39
Fundamental data types
  • 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
  • return 0

Declaration
Declaration with initialization
Function calls
Assignment statements
40
Variables
  • Before you can use a variable, you must declare
    it.
  • A declaration introduces a variable name.
  • Associates it with a data type.
  • Sets some optional characteristics called
    qualifiers.

41
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!!!
42
Fundamental data types
  • Integral (signed or unsigned)
  • char
  • int
  • Floating point types
  • float
  • double
  • The computer doesn't store the type, just the
    bits.
  • The type tells the computer how to interpret the
    bits.

43
Fundamental Data Types
  • Long form
  • char signed char
    unsigned char
  • signed short int signed int
    signed long int
  • usigned short int unsigned int
    unsigned long int
  • float double
    long double
  • Common (short) form
  • char signed char
    unsigned char
  • short int
    long
  • usigned short unsigned
    unsigned long
  • float double
    long double

44
Functionality Groups
  • Integral Types
  • char signed char unsigned
    char
  • short int long
  • usigned short unsigned unsigned
    long
  • Floating Types
  • float double long double
  • Arithmetic Types
  • integral types floating types

45
Constants
46
Some Character Constants and their Integer Values
  • character 'a' 'b' 'c' ...
    'z'
  • integer value 97 98 99 ... 112
  • character 'A' 'B' 'C' ...
    'Z'
  • integer value 65 66 67 ... 90
  • character '0' '1' '2' ...
    '9'
  • integer value 48 49 50 ... 57
  • character '' '' ''
  • integer value 38 42 43

The character 0 has a value of 48
47
Some Character Constants and their Integer Values
The character \0 has a value of 0
48
The Ascii Table
49
Some Character Constants and their Integer Values
  • char c 'a'
  • printf("c", c)
  • printf("d", c)
  • printf("ccc", c, c1, c2)

a is printed
97 is printed
abc is printed
50
Some Character Constants and their Integer Values
  • character 'A' 'B' 'C' ...
    'Z'
  • integer value 65 66 67 ... 90
  • char c 0
  • int i 0
  • for ( i 'a' i lt 'z' i )
  • printf( c, i )
  • for ( c 65 c lt 90 c )
  • printf( c, c )
  • for ( c '0' c lt '9' c )
  • printf( d, c )

abc ... z is printed
ABC ... Z is printed
48 49 50 ... 57 is printed
51
User Defined Constants
  • include ltstdio.hgt
  • define LOOP_LIMIT 25
  • int main(void)
  • int ctr 0
  • while ( ctr ! LOOP_LIMIT )
  • do something
  • ctr
  • return 0

A user defined constant.
The define statement is a precompiler directive
it changes the source code before it's compiled.
The string following the word define, up to the
first blank, is replaced by the statement that
follows, if any.
52
Constants
53
Decimal, Hexadecimal, Octal conversions
  • include ltstdio.hgt
  • int main(void)
  • printf( d x o\n, 19, 19, 19 )
  • printf( d x o\n, 0x1c, 0x1c, 0x1c)
  • printf( d x o\n, 017, 017, 017)
  • printf( d\n, 11 0x11 011)
  • printf( x\n, 2097151)
  • printf( d\n, 0x1FfFFf)
  • return 0

19 13 23
28 1c 34
15 f 17
37
1fffff
2097151
54
compute the size of some fundamental types
  • include ltstdio.hgt
  • int main(void)
  • printf( The size of some fundamental types is
    computed.\n\n")
  • printf( char 3d byte \n, sizeof(char) )
  • printf( short 3d bytes\n, sizeof(short) )
  • printf( int 3d bytes\n, sizeof(int) )
  • printf( long 3d bytes\n, sizeof(long) )
  • printf( unsigned 3d bytes\n,
    sizeof(unsigned) )
  • printf( float 3d bytes\n, sizeof(float) )
  • printf( double 3d bytes\n, sizeof(double) )
  • printf( long double 3d bytes\n, sizeof(long
    double) )
  • return 0

sizeof returns the number of bytes reserved for a
variable type.
55
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

56
compute the size of some fundamental types
  • sizeof(char) 1
  • sizeof(short) lt sizeof(int) lt sizeof(long)
  • sizeof(signed) sizeof(unsigned) sizeof(int)
  • sizeof(float) lt sizeof(double) lt sizeof(long
    double)

57
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 )
  • for ( i 0 i lt 5 i )
  • printf( u d u\n, u, i, u i )
  • return 0

Typically equal to 4294967295
58
Going over the Limit
  • 4294967295 0 4294967295
  • 4294967295 1 0
  • 4294967295 2 1
  • 4294967295 3 2
  • 4294967295 4 3
  • 4294967295 0 0
  • 4294967295 1 4294967295
  • 4294967295 2 4294967294
  • 4294967295 3 4294967293
  • 4294967295 4 4294967292

The variable restarts itself
59
Representation
v means value
s means sign
60
Representation
1000 0000
  • signed char sc -128
  • unsigned char uc 255
  • sc sc - 1
  • uc uc 1
  • uc 255
  • uc uc2

1111 1111
0111 1111 127 Underflow
0000 0000 0 Overflow
1111 1111
1111 1110 254
61
Float Representation
  • Decimal int.frac10exp
  • 2.510-27 ( 2510-28 )
  • Binary 1.frac2exp
  • 1.12-11111010
  • Fixed size, limited accuracy
  • seee eeee sfff ffff ffff ffff ffff ffff
  • double uses 8 bytes

62
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

Output 0.999999
63
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

64
Mathematical Functions
  • sqrt()
  • pow()
  • exp()
  • log()
  • sin()
  • cos()
  • tan()

The functions are declared in ltmath.hgt
All the functions use doubles
65
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main(void)
  • double x 0
  • printf( \ns\ns\ns\n\n, The square root of
    x and x raised,
  • to the x power will be computed., --- )
  • while ( 1 )
  • printf( Input x )
  • scanf( lf, x )
  • if ( x gt 0.0 )
  • printf( \n15s22.15e\n15s22.15e\n15s2
    2.15e\n\n,
  • x , x, sqrt(x) , sqrt(x), pow(x,
    x) , pow(x, x) )
  • else printf( \nSorry, your number must be
    nonnegative.\n\n )
  • return 0

66
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main(void)
  • double x 0
  • printf( \ns\ns\ns\n\n, The square root of
    x and x raised,
  • to the x power will be computed., --- )
  • while ( 1 )
  • printf( Input x )
  • scanf( lf, x )
  • if ( x gt 0.0 )
  • printf( \n15s22.15e\n15s22.15e\n15s2
    2.15e\n\n,
  • x , x, sqrt(x) , sqrt(x), pow(x,
    x) , pow(x, x) )
  • else printf( \nSorry, your number must be
    nonnegative.\n\n )
  • return 0

Infinite Loop
67
  • include ltstdio.hgt
  • include ltmath.hgt
  • int main(void)
  • double x 0
  • printf( \ns\ns\ns\n\n, The square root of
    x and x raised,
  • to the x power will be computed., --- )
  • while ( 1 )
  • printf( Input x )
  • scanf( lf, x )
  • if ( x gt 0.0 )
  • printf( \n15s22.15e\n15s22.15e\n15s2
    2.15e\n\n,
  • x , x, sqrt(x) , sqrt(x), pow(x,
    x) , pow(x, x) )
  • else printf( \nSorry, your number must be
    nonnegative.\n\n )
  • return 0

This is the interesing part
68
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.000000000000000e000
  • sqrt(x) 1.414213562373095e000
  • pow(x, x) 4.000000000000000e000
  • Input x

69
The usual arithmetic conversion (promotion)
  • If either operand is of type long double, double,
    float or unsigned long, the other operand is
    converted to long double, double, float or
    unsigned long appropriately.
  • Otherwise, the "integral promotions" are
    performed on both operands, and the following
    rules are applied
  • If one operand has type long and the other
    operand has type unsigned then one of two
    possibilities occurs
  • If a long can represent all the values of an
    unsigned, then the operand of type unsigned is
    converted to long.
  • If a long cannot represent all the values of an
    unsigned, then both operands are converted to
    unsigned long.
  • Otherwise, if either operand is of type long, the
    other operand is converted to long.
  • Otherwise, if either operand is of type unsigned,
    the other operand is converted to unsigned.
  • Otherwise, both operands have type int.

70
Expressions and Types
  • char c short s int i unsigned u
  • unsigned long ul float f double d
    long double ld

71
Cast
  • 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
Write a Comment
User Comments (0)
About PowerShow.com