Topic 2: C Overview - PowerPoint PPT Presentation

About This Presentation
Title:

Topic 2: C Overview

Description:

C Language Elements. Preprocessor Directives # (sign for preprocessor directive commands) ... C Language Elements. Reserved Words ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 24
Provided by: aabh
Category:

less

Transcript and Presenter's Notes

Title: Topic 2: C Overview


1
Topic 2 C Overview
  • C Language Elements
  • Variable Declaration and Data Types
  • Statement Execution
  • C Program Layout
  • Formatting Output
  • Interactive Mode, Batch Mode and Program
    Redirection

2
C Language Elements Preprocessor Directives
  • (sign for preprocessor directive commands)
  • include ltstandard header filegt
  • include ltstdio.hgt
  • include ltmath.hgt
  • Standard header file (.h)
  • Library

3
C Language Elements Preprocessor Directives
  • Constant Macros
  • Program does not change their values
  • define Name value
  • define KILOS_PER_POUND 0.45359
  • define MILES_PER_KM 0.62137
  • define PI 3.141593

4
C Language Elements Function main
  • Every C program has main function
  • Main function has heading and body
  • A function body has declarations and executable
    statements
  • int
  • main(void)
  • printf("This is a C program\n")
  • return (0)

The control returns back to OS
Program was executed without
error
5
C Language Elements Reserved Words
  • They have special meaning and can not be used for
    something else
  • All are lowercase
  • some of the reserved words are
  • void case switch
  • return for signed
  • else long if
  • do int while

6
C Language Elements Identifiers
  • Standard identifiers For example scanf or printf
    are the names of defined operations
  • User-defined identifiers (should begin with a
    letter and only can contain digit or _ )
  • Invalid Valid
  • 1rate rate1
  • int Rate1
  • joes joe
  • Uppercase lowercase User-defined identifiers
    are considered different identifiers by C
    compiler
  • Meaningful name should be used for User-defined
    identifiers

Different identifiers
It is a reserved word
7
C Language Elements Variable and data types
  • Variables are the name (identifiers) for memory
    cells that can be changed
  • It should start with a data type
  • int count
  • double x,y,z
  • char first_initial

8
C Language Elements Data types
  • int integer between -32767 to 32767
  • double for real numbers
  • 3.14, 0.34
  • 1.23e5, 1.23E5 1.23 10 to the power 5
  • 0.34e-4 0.000034
  • char for showing individual character
  • A, a, ,...

9
  • / Convert the weight from pounds to kilograms /
    comment
  • include ltstdio.hgt standard header file
    preprocessor
  • define KILOS_PER_POUND 0.45359 constant macro
    directive
  • int reserved word
  • main(void)
  • double pounds, variable
  • kilos
  • printf(" Enter the weight in pounds")
    standard identifier
  • scanf(lf, pounds)
  • kilos pounds KILOS_PER_POUND
  • printf(" That equals f kilos. \n, kilos)
    punctuation
  • return (0)
  • special
    symbol

10
Executable Statements
  • Data can be stored/changed in memory cells by
  • Assignment statement
  • variable expression () is not equal
    sign
  • x x z / 6.9
  • x -9
  • new_x -x
  • Input operations such as scanf function that
    requires
  • include ltstdio.hgt

11
Output Operation - printf
  • printf( format string, printlist)
  • printf( Hi c - your age is d\n, na,age)
  • printf (It is 1th line\n)
  • printf (\n and 2th line)
  • printf( Enter the weight in pounds)
  • scanf( lf , pounds)
  • printf( f pounds was entered, pounds)

12
Input Operation-scanf
  • scanf (format string, input list)
  • scanf (cd, initial, age)
  • is using for each int, double and char
    variables (it means address of memory)
  • The order of placeholders correspond to the order
    of variables in the input list
  • When reading the numbers, scanf scans the digits
    and characters until it reaches non-digits, blank
    or cr. For characters only first character before
    cr is considered

13
  • / This program calculates ? /
  • include ltstdio.hgt
  • define PI 3.14159
  • int main(void)
  • double r,a,c
  • scanf(lf, r)
  • a PI r r
  • c 2 PI r
  • printf( ? is f cm2. \n, a)
  • printf( ? is f cm. \n, c)
  • return (0)

14
Formating Values of Type int
  • printf ( Result is 3d meters,
  • Examples
  • Value format display
  • 234 6d 234
  • 234 1d 234
  • -234 6d -234
  • -234 1d -234
  • 234 d 234

15
Formating Values of Type double
  • printf ( Result is 6.2f meters,
  • Examples
  • Value format display
  • -99.42 6.1f -99.4
  • 99.999 6.2f 100.00
  • -.006 8.5f -0.00600
  • -.003 .3f -0.003
  • -3.15 .1f -3.2
  • 99.67 f 99.67

16
  • include ltstdio.hgt
  • int main(void)
  • double x 123.456
  • int y12345
  • printf( f .3f .1f \n,x,x,x )
  • printf( 3d 5d 8d \n\n,y,y,y )
  • return (0)
  • 123.456 123.456 123.5
  • 12345 12345 12345

17
Batch Mode
  • In batch mode the program gets its input from a
    data file instead of the user and sends the
    result to a data file instead of the screen. It
    is also called redirection. For example the
    following program can be run by weightconvert
    ltdata gtoutput
  • / weightconvert program /
  • include ltstdio.hgt
  • define KILOS_PER_POUND 0.45359
  • int main(void)
  • double pounds, kilos
  • scanf(lf, pounds)
  • printf(Weight in pounds is .2f. \n, pounds)
  • kilos pounds KILOS_PER_POUND
  • printf(" That equals f kilos. \n, kilos)
  • return (0)

18
Program-controlled Input and Output files
  • File pointer variable
  • include ltstdio.hgt
  • define KILOS_PER_POUND 0.45359
  • int main(void)
  • double pounds, kilos
  • FILE inp, / pointer to input file /
  • outp / pointer to output file /

19
Os preparing the files for access
  • / open the input and output files /
  • inp fopen (aweight.txt, r)
  • outp fopen (adistance.out, w)
  • / get the input from file /
  • fscanf(inp,lf, pounds)
  • fprintf(outp, Weight in pounds is .2f. \n,
    pounds)

20
  • kilos pounds KILOS_PER_POUND
  • / Display the result in output file /
  • fprintf(output, That equals f kilos. \n,
    kilos)
  • / Close files /
  • fclose(inp)
  • fclose(outp)
  • return (0)
  • We will discuss program-controlled input and
    output files in more details later.

21
Common programming Errors
  • Syntax Error
  • Missing or variable definition
  • double pounds
  • instead of
  • double pounds, kilos
  • Last comment is not closed
  • / Close files instead of / Close files /
  • Correct the errors in declaration part first

22
Runtime Errors and Undetected Errors
  • If we use scanf for numbers and then after that
    again we use scanf for the characters, the first
    character gets different value than what we
    entered. For example in a program
  • scanf (lf , radius)
  • scanf (ccc, a, b, c)
  • is used instead of
  • scanf (ccc, a, b, c)
  • scanf (lf , radius)
  • If we run this program with these inputs
  • I 50
  • ABC
  • The values stored in the variables are
  • ? a b c radius
  • \n A B 50
  • One of the way for fixing this problem is placing
    space before first C
  • scanf (lf , radius)
  • scanf ( ccc, a, b, c)

23
Logic Errors
  • Logic errors is shown by the wrong result of the
    program. For example forgetting in the
    following statement
  • scanf( dd , a, b)
  • produces gt incorrect results
  • Preventing logic errors is done by
  • Deskchecking the algorithm and
  • Debugging the program
Write a Comment
User Comments (0)
About PowerShow.com