C Programming Language #3

  • Published on
    13-Sep-2014

  • View
    771

  • Download
    2

DESCRIPTION

 

Transcript

  • PROGRAMMINGLANGUAGE #3

  • Input & Output

    Keyboard

    Monitor

  • User Interfacee

    User interface is the space where interaction

    between humans and machines occurs.

  • 3.1 3.2 3.3 3.4 3.5 scanf()3.6 *3.8

    Chapter 3

  • 3.1 CONVERSION SPECIFICATION

  • %c

    %d

    %e

    %E

    %f

    %o

    %u

    %x

    %X

  • Example 3-1/* ch3 printfs.c */

    #include #include int main(){

    char ch1 = 'a', ch2 = 'A';int i = 31, j = -1;float num = 123.456;double num1 = 123.456;char str[30]= "This is a car ... SAAB";

    /* output character */printf(" Format conversion...\n\n");printf(" Character : %c %c\n\n", ch1, ch2);

  • /* output integer number */printf(" Decimal : %d %d\n", i, j);printf(" Unsigned : %u %u\n", i, j);printf(" Octal : %o \n", i);printf(" Hexdecimal : %x \n", i);printf(" Hexdecimal : %X \n\n", i);

    /* output float point number */printf(" Float : %f %e %E\n", num, num, num);printf(" Double : %f %e %E\n\n", num1, num1, num1);

    /* output string */printf(" String : %s\n\n", str);

    /* output % symbol */printf(" Percent : 100%%\n\n");system("PAUSE");return 0;

  • Console Output

    Format conversion...

    Character : a A

    Decimal : 31 -1Unsigned : 31 4294967295Octal : 37Hexdecimal : 1fHexdecimal : 1F

    Float : 123.456001 1.234560e+002 1.234560E+002Double : 123.456000 1.234560e+002 1.234560E+002String : This is a car ... SAAB

    Percent : 100%

  • 3.2 CONVERSION SPECIFIERS

  • Flags +-#

    Width

    Precision

    Size shortlongdouble

    Conversion Specidiers

  • 3.3 FLAG AND WIDTH

  • Example

    printf(%d, 100);

    printf(%5d, 100);

    001

    001

  • Flag

    -

    + '-'

    # 00x0X

    0 0

  • /* ch3 flag2.c */

    #include #include int main(){

    int decimal = 31;

    printf("Flags...\n\n");printf("|%d|\n", decimal);printf("|%8d|\n", decimal);printf("|%#8o|\n", decimal);printf("|%#8x|\n", decimal);printf("|%08d|\n", decimal);system("PAUSE");return 0;

    }

  • Console Output

    Flags...

    |31|| 31|| 037|| 0x1f ||00000031|

  • 3.4 PRECISION

  • Example

    printf(%8.5f, 1.4567);

    07654.1

  • /* ch3 width3.c */

    #include #include int main(){

    double fl = 2.5e5;

    printf("|%15f|\n", fl);printf("|%-15.0f|\n", fl);printf("|%-#15.0f|\n", fl);

    printf("|%-15.4f|\n", fl);printf("|%-15.4e|\n", fl);system("PAUSE");return 0;

    }

  • Console Output

    | 250000.000000||250000 ||250000. ||250000.0000 ||2.5000e+005 |

  • 3.5 SCANF()

  • scanf

    Reads data from stdin and stores them into the locations pointed by the arguments.

  • printf

    Writes a formatted string to stdout.

  • #include

    int main( )

    {

    int num1;

    int num2;

    int sum;

    printf(Enter your number");

    scanf(%d, &num1);

    scanf(%d, &num2);

    sum = num1 + num2;

    printf(The Sum is %d, sum);

    getch();

    return 0;

    }

  • User Interface?

  • User Interface: Keyboard

  • User Interface: Monitor

  • Types of user interfaces in computer

  • DOS & UNIX

  • Mac OS & Windows 7

  • User Interface: Mouse

  • Other UI?

  • stdio.h = Standard Input & Output

    Standard Output?

    Standard Input ?

  • /* ch3 scanfs.c */

    #include #include int main(){

    int num1, num2;double fl;char str[20];

    printf("Input two number :\n");scanf("%d %d", &num1, &num2);printf(" ===> %d + %d = %d\n\n", num1, num2,

    num1+num2);

    printf("Input a floating point :\n");scanf("%lf", &fl);printf(" ===> %f is %e\n\n", fl, fl);

    printf("Input a string :\n");scanf("%s", str);printf(" ===> %s \n\n", str);printf("Input at most 10 chars :\n");scanf("%10s", str);printf(" ===> %s\n", str);system("PAUSE");return 0;

    }

  • Input two number :14 5060===> 14 + 5060 = 5074

    Input a floating point :119.117===> 119.117000 is 1.191170e+002

    Input a string :abcdefghijklmnopq===> abcdefghijklmnopq

    Input at most 10 chars :abcdefghijklmnopq===> abcdefghij

    Console Output

  • 3.6 * SPECIFIER

  • 37

    /* ch3 star1.c */

    #include #include int main(){

    int width, precision;double d_num = 1234.56789;

    printf("Source Number : %f\n\n", d_num);printf("Input the width : ");scanf("%d", &width);

    printf("\nInput the precision : ");scanf("%d", &precision);

    printf("\nFormat ===> \"%%-%d.%df\"\n", width, precision);printf(" Formatted Number:|%-*.*f|\n", width, precision, d_num);system("PAUSE");return 0;

    }

  • Source Number : 1234.567890

    Input the width : 15

    Input the precision : 4

    Format ===> "%-15.4f"Formatted Number : |1234.5679 |

    Console Output

  • 3.8 TYPE CONVERSION

  • /* ch3 cut.c */

    #include #include int main(){

    short int num = 321;

    printf("Decimal : %d\n", num);printf("Character : %c\n", num);system("PAUSE");return 0;

    }

  • 65 (A)

    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 12 bytes

    0 1 0 0 0 0 0 11 bytes

    Binary expression

  • 0 0 0 00 0 0 1 0 1 0 0 0 0 0 1

    num = 321

    Low byteHigh byte

    Decimal : 321

    Character : A

    num = 321;

  • 3.10 EXERCISE

  • #include

    #include

    int main( )

    {

    double f = 678.90;

    printf(%%f.| %f|\n, f);

    printf(%%3.2f.| %3.2f|\n, f);

    printf(%%7.2f.| %7.2f|\n, f ");

    printf(%%7.0f.| %7.0f|\n, f);

    printf(%%*.1f.| %*.1f|\n, 7, f);

    printf(%%*.*f.| %*.*f|\n, 7, 1, f ");

    system(PAUSE);

    return 0;

    }

  • Homework #3

    3.11 No.5 (P.29)

    Write or Print in A4Paper

    Name & I.D.

  • C Programming is Easy!

    Any Question?

    aragornyeh@gmail.com

    Programming Language #3Input & OutputUser InterfaceeChapter 33.1 Conversion specificationSlide Number 6Example 3-1Slide Number 8Console Output3.2 conversion specifiersConversion Specidiers3.3 Flag and widthExampleSlide Number 14Slide Number 15Console Output3.4 precisionExampleSlide Number 19Console Output3.5 scanf()scanfprintfSlide Number 24User Interface?User Interface: KeyboardUser Interface: MonitorTypes of user interfaces in computerDOS & UNIXMac OS & Windows 7User Interface: MouseOther UI?stdio.h = Standard Input & OutputSlide Number 34Console Output3.6 * SpecifierSlide Number 37Console Output3.8 type ConversionSlide Number 40Binary expressionnum = 321;3.10 ExerciseSlide Number 44Homework #3Any Question?