CSE 459'21: Programming in C - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CSE 459'21: Programming in C

Description:

character constant is an integer, can be used in numeric operations. ... Keywords are reserved words, and may not be used as identifiers. auto, double, int... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 30
Provided by: weiku
Category:
Tags: cse | programming

less

Transcript and Presenter's Notes

Title: CSE 459'21: Programming in C


1
CSE 459.21 Programming in C
  • Instructor Na Li
  • Email lina_at_cse.ohio-state.edu
  • Phone 614-292-0056
  • Office DL 274 (Dreese Laboratories)
  • Office Hour Tuesday 100--200PM

2
Course Information
  • Course Page
  • http//www.cse.ohio-state.edu/lina/teaching/cse45
    9-21/
  • Prerequisite
  • cse321 or equivalent
  • Textbook
  • The C Programming Language, Kernighan Ritchie

3
Syllabus
  • Lectures
  • Chapter 1-8 will be covered.
  • Labs
  • Four labs
  • Electronic submission
  • run on stdsun.cse.ohio-state.edu
  • lab quality, correctness
  • Try to develop good coding style
  • Exams
  • No exams

4
Sample 1
  • / Hello World! /
  • include ltstdio.hgt
  • int main()
  • printf(Hello World!\n)
  • return 0

5
Sample 1 Dissection
  • What it has?
  • A function definition main
  • A line of comment
  • A line of preprocessor directive
  • An output statement
  • A return clause
  • What it does?
  • Ask the computer to say hello to the world.
  • What it does not do?
  • It seems not computing!!
  • No real work
  • not taking input
  • does not change any value

6
Sample 2
  • include ltstdio.hgt
  • define MAGIC 10
  • int main(void)
  • int i0
  • int factor, remainder
  • while (i lt 3)
  • printf(Guess a factor of MAGIC larger
    than 1 ")
  • scanf("d, factor)
  • remainder MAGIC factor
  • if (remainder 0)
  • printf(You got it!\n)
  • else
  • printf(Sorry, You missed it!\n)
  • return 0

7
Sample 2 Dissection
  • What more it has?
  • Macro definition
  • Variable declaration
  • Operations represented by operators
  • Conditional computation
  • Input Processing
  • Loops three chances

8
Syntax Dissection
  • What is syntax?
  • How to put your C elements together into a
    program?
  • Variable declaration,
  • Identifier case, keywords, style
  • datatype specification
  • char, short, int, long, float, double
  • Operators
  • , -, , /, , , --, gt, lt, , , ,
  • Priority and Precedence
  • Associativity

9
More
  • Preprocessor directive
  • macro
  • header file inclusion
  • Loop constructs
  • for, while, do-while, break, continue, label--go
  • Conditional statements
  • If else, ifelse if, switch, break
  • Function definition
  • return type, parameter definition, parameter
    passing
  • Standard library and function calls

10
Walk through
  • C program hello.c
  • emacs, vi, vim, pico, joe
  • Compilation hello.o, a binary file
  • gcc -c hello.c
  • Linking a.out or hello, an executable file
  • gcc hello.o
  • gcc -o hello hello.o
  • Loading (dynamical linking) and execution
    ./hello
  • ./a.out
  • ./hello

11
Take home practice
  • Walk through sampe1 and sample2
  • preprocessing, compilation, linking,
    loading/execute
  • Modify and repeat once more if you can
  • I will put the instructions on course webpage
    today.

12
Recap of last lecture
  • include ltstdio.hgt / Preprocessor directive
    /
  • define MAGIC 10 / Macro substitution/
  • int main(void) / Every C program has a main
    function/
  • int i0 /
    Variable declaration/
  • int factor, remainder
  • while (i lt 3) /Control flow/
  • printf(Guess a factor of MAGIC larger
    than 1 ") /input output/
  • scanf("d, factor)
  • remainder MAGIC factor
    /Operations represented by operators/
  • if (remainder 0) /conditional
    statement/
  • printf(You got it!\n)
  • else
  • printf(Sorry, You missed it!\n)
  • return 0 /Return clause/

13
Recap of last lecture
  • Program environment Unix System
  • stdsun.cse.ohio-state.edu
  • gcc compiler
  • Walk through your program
  • http//www.cse.ohio-state.edu/lina/teaching/cse45
    9-21/instructions.html

14
Types, Operators, and Expressions
  • Constants
  • Sample constants
  • Variables
  • Declaration must be declared before being used,
    declaration should be made before any execution
    statements.
  • Naming
  • Type
  • specify the set of values a variable can have and
    what operations can be performed on it.
  • basic types char, int, long, float, double, void
  • other types arrary,pointer, enumeration,
    structure
  • Type cast
  • Initialization optional
  • Operator
  • Type conversion
  • Precedence
  • Associativity
  • Operations What is to be done to the data
    objects
  • Samples
  • Expression
  • Conditional expression

15
Sample constants
  • int 123, -1, 2147483647, 040 (octal), 0xab
    (hexadecimal)
  • unsigned int 123u, 2107433648, 040U (octal), 0X02
    (hexadecimal)
  • long 123L, 0x1FFFl (hexadecimal)
  • unsigned long 123ul, 0777UL (octal)
  • float 1.23F, 3.14e0f
  • double 1.23, 2.718281828
  • long double 1.23L, 9.99E-9L
  • char 'A', 'B'
  • character constant is an integer, can be used in
    numeric operations.
  • Special characters \\ (backslash), \n (newline),
    \t (tab), \', \", ...
  • How about \000,\xhh?
  • How about \0?
  • String constant( reference as pointers)
  • x
  • abcde
  • Representation abcde\0

16
Variable Declaration
  • Declaration
  • Purpose define a variable before it is used.
  • Format type identifier , identifier
  • How to Declare an array? A string?
  • char line6
  • char s
  • Qualifier
  • const
  • const int a9 /Right/
  • const in a
  • a9 /compliers will give warning message/
  • static

17
Variable Naming
  • Keywords are reserved words, and may not be used
    as identifiers
  • auto, double, int
  • a-z, A-Z, 0-9, and _
  • The first character must be a letter or _
  • Case sensitive
  • Good naming style should
  • Separate words with _' or capitalize the first
    character
  • Use all UPPERCASE for symbolic constant, macro
    definitions, etc
  • Be consistent
  • Be meaningful

18
How to use void?
  • void a()
  • Here a is a function name, void represents that
    this function has no return value.
  • void a
  • Here a is a pointer, void represents that this
    pointer can point to any data type.
  • int a(void)
  • Here a is a function name, void represents that
  • the parameter lists checking can be omitted.

19
Type Cast
  • A type coercion (type) identifier
  • Example
  • int i 258
  • char a / range -128 to 127 /
  • a (char) i / What is the value of a? /
  • i0x00000102
  • a0x02

20
Static Variable
  • Program 1 Program2
  • include ltstdio.hgt include ltstdio.hgt
  • int increment() int increment()
  • static int x 0 int x 0
  • x x
  • return x return x
  • int main( ) int main()
  • int y int y
  • int i int i
  • for (i0 ilt10i) for (i0 ilt10i)
  • yincrement( )
    yincrement( )
  • printf(d\n, y) printf(d\n, y)
  • return 0 return 0

21
Variable Initialization
  • Initialization
  • Each identifier can also be assigned
    (initialized) with a value of the same data type.
  • char line6"ABCDE" ?/ copy ABCDE and /0 to
    array line /
  • char line6ABCDEFG? /copy first 6 elements
    in string to array line /
  • char s "ABCDE" / s point to a constant/
  • Attention Direct assignment from a constant
    string can only happen at initialization but not
    assignment statement!
  • Char line6
  • Char s
  • LineABCDE / Wrong/ ---do you know why?
  • sABCDE / Wrong /-----do you know why?
  • sABCDE /Right/ -----do you know why?
  • Initialization rules
  • Automatic variable each time the function or
    block is entered (garbage)
  • Non-automatic variable only once before the
    program executes
  • External and static variable0 by default



22
Type conversion
  • When an operator has operands of different types,
    the only automatic conversion is to convert a
    narrower operand into a wider operand without
    losing information.
  • long doublelt-others doublelt-others
    floatlt-others intlt-char, short longlt-others
  • Assignments the value of the right side is
    converted to the type of the left
  • Function call in absence of a function
    prototype, char, short-gtint, float-gtdouble
  • What if you want to convert a string of numbers
    to an integer?
  • Example1
  • What if you want to convert an integer into a
    string of numbers?
  • Example2

23
Example 1
  • includeltctype.hgt
  • / atoi convert s to integer /
  • int atoi(char s )
  • int i, n
  • n0
  • for (i0sigt0silt9i) /
    isdigit(si) / n10n(si-0)
  • return n

24
Example 2
  • / itoa convert n to character in s/
  • void itoa(int n,char s )
  • int i,sign
  • if((signn)lt0)
  • n-n
  • i0
  • do
  • sin100
  • while ((n/10)gt0)
  • if (signlt0)
  • si-
  • si\0
  • reverse(s)

25
Precedence and Associtivity
  • Precedence The order in which operators are
    applied in expressions.
  • 9-6/2
  • Associativity The order in which expressions
    involving operators of the same precedence are
    evaluated.
  • 96-4
  • Use parentheses to specify a different order of
    evaluation. It is recommended to use parentheses
    for clear and easy to read programs. But you
    still need to be prepared for the complexity in
    general.
  • Short circuit evaluation
  • and are evaluated from left to right. If at
    any point, the value of expression is determined,
    evaluation stops.
  • Example (xgt0.0)(sqrt(x)gt5.0)
  • A summary of precedence and associtivity
  • http//www.cse.ohio-state.edu/lina/teaching/cse45
    9-21/operators.html
  • Examples
  • printf(d \td\n,n,power(2,n))
  • aii

26
Operations represented by operators
  • A summary of operators
  • http//www.cse.ohio-state.edu/lina/teaching/cse45
    9-21/operators.html
  • Some notes
  • C has no keywords as true or false. They are
    non-zero or zero values, usually 1 or 0.
  • d cgt0clt9
  • What is the value of d?
  • OR', AND' , and NOT' differences between
    bitwise and logical operators.
  • 0x1011 0x0010 ( 0x0010) cgt0clt9
  • Note the difference between assignment and equal
    operators. Don't make the mistake of using ''
    when you meant ''!
  • operator can not be applied to float or double.
  • 53 (2) 5.03(wrong)
  • / Truncation
  • ! Converts a non-zero operand into 0
  • If (! valid)? if (valid 0)




27
Operations represented by operators
  • Be careful using floats with equality. Use
    inequalities whenever possible
  • 4.0 can be stored in memory as 3.9999999 or
    4.0000001
  • float a, b
  • If(ab) ./ Wrong/
  • Array indexing ij, here j can only be integer.
  • Increment and Decrement operators
  • can only be applied to variable (ij) is
    illegal
  • can be prefix or postfix
  • int n3, y
  • yn
  • y n
  • Assignment operators
  • int x3,
  • int y1
  • xy1 / xx(y1) not xxy1 /
  • What is the value of x?
  • Should be 6, not 4

28
Division Truncation
  • include ltstdio.hgt
  • int main( )
  • int fahr
  • for (fahr0 fahrlt300 fahrfahr20)
  • printf(3d 6.1f\n, fahr, 5(fahr-32.0)/9)
  • return 0
  • include ltstdio.hgt
  • int main( )
  • int fahr
  • for (fahr0 fahrlt300 fahrfahr20)
  • printf(3d 6.1f\n, fahr, 5/9(fahr-32.0))
  • return 0



29
Some samples
  • int a, b, c
  • a 2, b 1
  • c 1 a - -- b
  • /c3 b0/
  • c 1 a - b --
  • /c3 b-1/
  • c 1 a - b
  • /c3 b0/
  • c 1 a - b
  • /c3 b1/
  • c (1 a, b)
  • /c1 b1/
  • c 1 (a, b)
  • /c2 b1/
Write a Comment
User Comments (0)
About PowerShow.com