Introduction to C Programming Language - PowerPoint PPT Presentation

1 / 83
About This Presentation
Title:

Introduction to C Programming Language

Description:

Increment and Decrement Operators. Pre-increment operation variable ... Pre-decrement operation --variable. Post-decrement operation variable. Example. x = 4; ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 84
Provided by: soni143
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming Language


1
Introduction to C Programming Language
Tutor Session I
  • Wongyos Keardsri (PBank)Department of Computer
    EngineeringFaculty of Engineering, Chulalongkorn
    UniversityBangkok, ThailandMobile Phone
    089-5993490E-mail wongyos_at_gmail.com, MSN
    bankberrer_at_hotmail.com

2
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

3
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

4
C History
  • C is a structured computer programming language
  • Appeared in1972 (1970s)
  • Designed and Developed by Dennis Ritchie at the
    Bell Telephone Laboratories
  • Derived from B and BCPL programming language
  • Developed for the UNIX operating system

5
C History
(Cont)
  • Used and Implemented for
  • System programming
  • Operating systems
  • Embedded system applications
  • C standard ANSI C and ISO C
  • Book The C Programming Language, 2nd edition

http//en.wikipedia.org/wiki/C_28programming_lang
uage29
6
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

7
C Language VS Java Language
  • Example C Code (hello.c)

include ltstdio.hgt main() printf("Hello
CP\n")
  • Example Java Code (hello.java)

public class hello public static void
main(String arg) System.out.print("Hello
CP\n")
8
C Language VS Java Language
(Cont)
  • C Structured Programming
  • C Compiler
  • Editor Tubo C, vi, etc.
  • Compile (UNIX)
  • cc hello.c
  • After compile
  • a.out
  • Run
  • ./a.out
  • Hello CP
  • Java Object-Oriented Programming
  • Java Compiler
  • Editor JLab, EditPlus, etc.
  • Compile
  • javac hello.java
  • After compile
  • hello.class
  • Run
  • java hello
  • Hello CP

Show an example
Show an example
9
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

10
C Language Structure
  • General format

include ltstdio.hgtinclude ltgtmain()
function-bodytype func()
function-body
Preprocessor / Library include
Begin
Function maindeclaration-list
statement-list
End
. . .
Semicolon
Function funcdeclaration-list
statement-list
11
C Language Structure
(Cont)
  • An example C code

include ltstdio.hgt main() int sum /
Variable declaration / / sum is
a variable hold the sum of two
integer / sum 15 25 / Value
assignment / printf("The sum of 15 and 25 is
d\n", sum)
The sum of 15 and 25 is 40
12
C Language Structure
(Cont)
  • Compile C by UNIX command
  • cc option file1 file2
  • Example 1

cc example.c
Compile
a.out
./a.out
Run
13
C Language Structure
(Cont)
  • Example 2

cc c example.c
Compile
example.o
Object file
Convert to Executable file
cc o example.run example.o
example.run
Executable file
./example.run
Run
14
C Language Structure
(Cont)
  • Example 3

cc o example.run example.c
Compile
example.run
Executable file
./example.run
Run
Wow
15
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

16
Types, Operators and Expressions
Data Types and Sizes
  • char Character (8 bit) Ex. A, a, F
  • int Integer (16 bit) Ex. 12, -2456, 99851
  • float Real single (32 bit) Ex. 23.82,
    567.008
  • double Real double (64 bit) Ex. 0.0002100009
  • short Shot integer (16 bit) Ex. -4, 18
  • long Long integer (32 bit) Ex. 9547604608456
  • unsigned Unsigned integer (16 bit) Ex. 2, 908,
    1392

17
Types, Operators and Expressions
Variable Declaration
type-specifier list-of-variables
  • Example
  • int x, y, x 5
  • float eps 1.0e-5
  • int limit MAXLINE1
  • char s_name A
  • char str Hello CP
  • double dd 0.000000000001

18
Types, Operators and Expressions
Constant Declaration
define VARIABLE constant-value
Noteinclude ltstdio.hgtdefine XX 0main()
// Body program
  • Example
  • define MAX 500
  • define STEP 20
  • define PI 3.14159265
  • define VTAB '\t
  • const char msg "warning "
  • const double e 2.71828182845905

19
Types, Operators and Expressions
Expression and Operations
  • Arithmetic Operators
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Modulation
  • Example
  • fag x y
  • c a (a/b)b
  • sum var1 var2 var3

20
Types, Operators and Expressions
Expression and Operations (Cont)
  • Relational Operators
  • Less than lt a lt 5
  • Less than or equal lt a lt b
  • More than gt a gt b c
  • More than or equal gt a gt b 5
  • Equal a -6
  • Not equal ! a ! 0
  • Logical Operators
  • AND (a gt 0) (b gt 0)
  • OR (a lt 0) (b lt 0)
  • Negation ! !(a c)

21
Types, Operators and Expressions
Expression and Operations (Cont)
  • Bitwise Operators
  • Bitwise AND
  • Bitwise OR (Inclusive OR)
  • Bitwise XOR (Exclusive OR)
  • Left shift ltlt
  • Right shift gtgt
  • One's complement
  • Example
  • x 01001011 y 00101100 x 10110100
  • x y 00001000 x y 01101111
  • x y 01100111 x ltlt 2 00101100

22
Types, Operators and Expressions
Expression and Operations (Cont)
  • Assignment Operators and Expressions
  • op is - / ltlt gtgt
  • If expr1 and expr2 are expressions, then
  • expr1 op expr2
  • Equivalent to
  • expr1 (expr1) op (expr2)
  • Example
  • X 1
  • X X 1

Equivalent
23
Types, Operators and Expressions
Expression and Operations (Cont)
  • Conditional Expressions
  • expr1 ? expr2 expr3
  • If expr1 is true do expr2
  • If expr1 is false do expr3
  • Example
  • a 5
  • b 10
  • min a lt b ? a b

24
Types, Operators and Expressions
Expression and Operations (Cont)
  • Increment and Decrement Operators
  • Pre-increment operation variable
  • Post-increment operation variable
  • Pre-decrement operation --variable
  • Post-decrement operation variable
  • Example
  • x 4
  • y x 5 // x 5, y 9
  • x 4
  • y x 5 // x 5, y 10

25
Types, Operators and Expressions
Expression and Operations (Cont)
  • Type Cast Operator (Casting)
  • (type-specifier) expression
  • Example
  • (double) date
  • fload var1 2.7
  • int var2 (int) var1 //var2 7
  • (char) x
  • (int) d1 d2

26
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

27
Control Flow
Basic Statements
  • printf() is a function for display result to
    standard output (Monitor, Screen)
    printf(format, arg1, arg2, )
  • Example
  • printf(Hello\n)// Hello
  • int num 2printf(d is integer, num)// 2
    is integer

28
Control Flow
Basic Statements (Cont)
  • Conversion Operation printf()
  • d signed decimal conversion of an int or a long
  • u unsigned decimal conversion of
    unsigned
  • o unsigned octal conversion of
    unsigned
  • x, X unsigned hexadecimal conversion of
    unsigned x a f, X A -
    F
  • c single character conversion
  • s string conversion
  • f signed decimal floating point
    conversion
  • e, E signed decimal floating point
    conversion in scientific notation

29
Control Flow
Basic Statements (Cont)
  • Example of printf()

int j 45, k -123 float x
12.34 char c w char m
Hello printf(Hello 55)
Hello 55 printf(Tax is d, j) Tax
is 45 printf(d, jk)
-78 printf(3d d, j, k) 45
-123 printf(f .2f, x, x, x)
12.340000 12.34 printf(c, c)
w printf(s\n, m)
Hello printf(s\n, Hello)
Hello printf(10s\n, Hello)
Hello
Result
30
Control Flow
Basic Statements (Cont)
  • scanf() is a function for read input from
    standard input (Keyboard) scanf(format, arg1,
    arg2, )
  • Example
  • scanf(d d d, day, month, year)
  • int numprintf(Enter integer )scanf(d,
    num)// Enter integer 23

31
Control Flow
Basic Statements (Cont)
  • Conversion Operation scanf()
  • d signed decimal conversion of an int or a long
  • u unsigned decimal conversion of
    unsigned
  • o unsigned octal conversion of
    unsigned
  • x, X unsigned hexadecimal conversion of
    unsigned x a f, X A -
    F
  • c single character conversion
  • s string conversion
  • f signed decimal floating point
    conversion
  • e, E signed decimal floating point
    conversion in scientific notation
  • read only character in , if read
    different character

32
Control Flow
Basic Statements (Cont)
  • Example of scanf()

int d,m,y,x char ch1,ch2 float f scanf(d,
x) 4 // x4 scanf(2d2d4d,
d,m,y) 22062007
// d22, m6, y2007 scanf(d/d/d,
d,m,y) 22/06/2007 // d22, m6,
y2007 scanf(cc, ch1,ch2)
Ab // ch1A, ch2b scanf(f,
f) 2.3 // f2.300000
Result
33
Control Flow
Decision Statements
  • If-Else Statement
  • The if-else statement is used to express
    decisions. Formally the syntax is
  • if (expression) true statement
  • else
  • false statement

34
Control Flow
Decision Statements (Cont)
  • Else-If Statement (Nested If-Else)
  • The sequence of if statements is the most general
    way of writing a multi-way decision. Formally the
    syntax is
  • if (expression)
  • statement
  • else if (expression)
  • statement
  • else if (expression)
  • statement
  • else
  • statement

35
Control Flow
Decision Statements (Cont)
  • Example of If-Else

if (score gt 80) grade A else if (score
gt 70) grade B else if (score gt 50)
grade Celse if (score gt 40) grade
D else grade F
if (a lt b) printf(a less than
b\n) else temp a a b / swap
a / b temp / and b /
printf(Interchange a and b\n)
36
Control Flow
Decision Statements (Cont)
  • Switch Statement
  • The switch statement is a multi-way decision that
    tests whether an expression matches one of a
    number of constant integer values, and branches
    accordingly.
  • switch (expression)
  • case const-expr statements
  • case const-expr statements
  • default statements

37
Control Flow
Decision Statements (Cont)
  • Example of Switch

c getchar() switch (c) case '0'
printf(Zero\n) breakcase '1' case '2' case
'3' case '4'case '5' case '6' case '7' case
'8'case '9' printf(Nine\n) break case '
' case '\n' newln break case '\t' tabs
break default printf(missing char\n) break
38
Control Flow
Iteration Statements
  • While Statement
  • The expression is evaluated. If it is true,
    statement is executed and expression is
    reevaluated. This cycle continues until
    expression becomes false.
  • while (expression)
  • Statement1
  • Statement2
  • ...

39
Control Flow
Iteration Statements (Cont)
  • Example of While

include ltstdio.hgt define PERIOD . main()
char C while ((C getchar())!
PERIOD) putchar(C) printf(Good
Bye.\n) Result?
40
Control Flow
Iteration Statements (Cont)
  • Do-While Statement
  • The do-while, tests at the bottom after making
    each pass through the loop body the body is
    always executed at least once.
  • do
  • statement1
  • statement2
  • while (expression)

41
Control Flow
Iteration Statements (Cont)
  • Example of Do-While

int i 1, sum 0 do sum i i
while (i lt 50) printf(The sum of 1 to 50 is
d\n, sum) Result?
42
Control Flow
Iteration Statements (Cont)
  • For Statement
  • The for statement
  • for (expr1 expr2 expr3)
  • statement
  • Equivalent to
  • expr1
  • while (expr2)
  • statement
  • expr3

43
Control Flow
Iteration Statements (Cont)
  • Example of For

for (i1ilt100i) x i if ((x i)
0) i--
for (i0, jstrlen(s)-1 iltj i,j--) c
si, si sj, sj c
char c int count for (count0 (cgetchar() !
.) count) printf(Number of characters
is d\n, count)
44
Control Flow
Sequential Statements
  • Break and Continue Statement
  • The break statement provides an early exit from
    for, while, and do.break
  • The continue statement is related to break, but
    less often used it causes the next iteration of
    the enclosing for, while, or do loop to
    begin.continue

45
Control Flow
Sequential Statements (Cont)
  • Example of Break and Continue

int c while ((c getchar()) ! -1) if (C
.) break else if (c gt 0 c lt
9) continue else putchar(c) printf(
Good Bye \n)
46
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

47
Functions and Program Structure
  • Functions (Method in Java) break large computing
    tasks into smaller ones, and enable people to
    build on what others have done instead of
    starting over from scratch.
  • function-name (argument declarations)
  • declaration-list
  • statement-list

48
Functions and Program Structure
(Cont)
  • Example of Function

/ Programming in function / main() int
hours, minutes, seconds int total_time
0 int convert()
printf(Enter time in
hours-minutes-seconds ) scanf(d d
d, hours, minutes, seconds) total_time
convert(hours, minutes, seconds)
/ calling point / printf(The
converted time is d seconds\n,
total_time)
49
Functions and Program Structure
(Cont)
  • Example of Function (Cont)

/ Function (called function) / int convert
(hours, minutes, seconds) int time
/ return-value variable / time (60
hours minutes) 60 seconds return
(time) / exiting point /
50
Functions and Program Structure
Non-Return Function
  • Example of Non-Return Function

/ Main / main() void display()
display() / Function Part / void
display() printf( Hello and Good bye
)
51
Functions and Program Structure
Return Function
  • Example of Return Function

/ Main Function / main() float
sq,x float square() sq
square(x) printf(The square of x is f\n,
sq) / Function square / float square(x)
return(x x)
52
Functions and Program Structure
External Variable
  • Example of External Variable

main() int k 15 func1()
func2() printf(k d\n, k)
int
k 5 / External Variable /

func1() printf(k d\n, k)
/Function 1/ func2() printf(k d\n, k)
/Function 2/
k 5 k 5 k 15
53
Functions and Program Structure
Recursive Function
  • Example of Recursive Function

main() / Main Part / int j long
int factorial() for (j 0 j lt 10 j)
printf(2d! is ld\n, j, factorial(j)) lo
ng int factorial(n) / Function Part / int n
if (n 0) return (1) else return (n
factorial(n-1))
54
Functions and Program Structure
The Standard C Library Functions
  • include ltstdio.hgt
  • clearerr
  • fclose
  • feof
  • ferror
  • fflush
  • fgetc
  • fgets
  • fopen
  • fprintf
  • fputc
  • fputs
  • Fread
  • freopen
  • fscanf
  • fseek
  • ftell
  • fwrite
  • getc
  • getchar
  • gets
  • printf
  • putc
  • putchar
  • puts
  • rewind
  • scanf
  • sprintf
  • sscanf
  • ungetc

55
Functions and Program Structure
The Standard C Library Functions (Cont)
  • include ltmath.hgt
  • acos(x)
  • asin(x)
  • atan(x)
  • ceil(x)
  • cos(x)
  • cosh(x)
  • exp(x)
  • fabs(x)
  • floor(x)
  • log(x)
  • log10(x)
  • sin(x)
  • sinh(x)
  • sqrt(x)
  • tan(x)
  • tanh(x)
  • atan2(x,y)
  • power(x,y)

56
Functions and Program Structure
The Standard C Library Functions (Cont)
  • include ltctype.hgt
  • isalnum
  • isalpha
  • isascii
  • iscntrl
  • isdigit
  • isgraph
  • islower
  • isprint
  • ispunct
  • isspace
  • isupper
  • tolower
  • toupper

57
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

58
Array, Pointer and String
Array
  • Array is a group of related data items.
  • In C, there is a strong relationship between
    pointers and arrays, strong enough that pointers
    and arrays should be discussed simultaneously.
  • The declaration of one-dimensional arraytype
    array-namenumber-of-elements
  • int a15

a0, a1, a2, , a14 15 Element
59
Array, Pointer and String
Array (Cont)
  • In main memory

Value Address
a a0 5501 a1 5502 a2
5503
a14 5514
int a15
60
Array, Pointer and String
Array (Cont)
  • Example of Array

/ Read five integers from the standard input
and display them in reverse order / define
MAX 5 main() int j int aMAX for
(j 0 j lt MAX j) scanf(d,
aj) for (j MAX - 1 j gt 0 j--)
printf(d\n, aj)
61
Array, Pointer and String
Pointer
  • A pointer is a variable that contains the address
    of a variable.
  • Example of pointer

Value Address
int x,y,pxx 25px xy px//y x
25
x 25
x 25 7960

px x
px 7960 8132
y
y px
62
Array, Pointer and String
Pointer (Cont)
  • Example

int x int px, py px x py px
px
x
py
main() int t10 int pt for (pt
t pt lt t10 pt) scanf(d, pt) for
(pt t10 pt gt t pt--) printf(d\n,
pt)
63
Array, Pointer and String
Pointer (Cont)
  • Example (Cont)

int x 1, y 2, z10 int ip / ip is a
pointer to int / ip x / ip now points to x
/ y ip / y is now 1 / ip 0 / x is
now 0 / ip z0 / ip now points to z0 /
Help me!!
64
Array, Pointer and String
String
  • A string constant, written as "I am a string" is
    an array of characters. In the internal
    representation, the array is terminated with the
    null character '\0' so that programs can find the
    end.
  • Example

char amessage "now is the time"/ an array
/ char pmessage "now is the time"/ a
pointer /
65
Array, Pointer and String
String (Cont)
  • Example of String

char s1 xyz char s2 x, y, z,
\0 char s3 xyz
x
y
z
\0
66
Array, Pointer and String
String (Cont)
  • Example

/ COUNT THE LENGTH OF STRING / main()
char w good morning int len()
printf(Lengths of string is d\n,
len(w)) / FUNCTION len() for counting the
length of string / int len(string) char
string // char string int count 0
while (stringcount ! NULL) count return
(count)
67
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

68
Structures
  • A structure is a collection of one or more
    variables, possibly of different types, grouped
    together under a single name for convenient
    handling.
  • Structures are called "records'' insome
    languages, notably Pascal.
  • struct structure-name type-specifier
    variable
  • ...

Members
69
Structures
(Cont)
  • Example of Structure

struct personnel int code char
name30 char address40 float salary
Record Node
int code
char name
char address
float salary
struct point int x int y
int x
int y
70
Structures
(Cont)
  • A struct declaration defines a type.
  • struct structure-name variable-name,
  • Or declare in
  • struct structure-name type-specifier
    variable
  • ...
  • variable-name

71
Structures
(Cont)
  • Example of Structure Declaration

struct personnel p
or
struct personnel int code char
name30 char address40 float
salary p
72
Structures
(Cont)
  • A member of a particular structure is referred to
    in an expression by a construction of the form
  • variable-name.member-name
  • Example
  • p.code
  • p.name
  • p.address
  • p.salary

73
Structures
(Cont)
  • Example of Struct Program

/ ASSIGN AND PRINT A DATE / main()
struct date int dd 22 int
mm int yyyy struct date today
today.mm 6 today.yyyy 2007
printf(Date is 2d/2d/4d\n, today.dd,
today.mm, today.yyyy)
//ResultDate is 22/ 6/2007
74
Tutor Outline
  • C History
  • C Language VS Java Language
  • C Language Structure
  • Types, Operators and Expressions
  • Control Flow
  • If-Else
  • Else-If
  • Switch
  • While
  • DoWhile
  • For
  • Functions and Program Structure
  • Non-Return Function
  • Return Function
  • The Standard C Library Functions
  • Array, Pointer and String
  • Structures
  • File Operations

75
File Operations
  • The input and output functions, types, and macros
    defined in ltstdio.hgt represent nearly one third
    of the library.
  • The following functions deal with operations on
    files
  • fopen(filename, mode)
  • fclose(file pointer)
  • feof(file pointer)
  • getc()
  • putc()

76
File Operations
(Cont)
  • fopen() - opens the named file, and returns a
    stream, or NULL if the attempt fails. Legal
    values for mode include
  • "r" open text file for reading
  • "w" create text file for writing discard
    previous contents if any
  • "a" append open or create text file for writing
    at end of file
  • "r" open text file for update (reading and
    writing)
  • "w" create text file for update, discard
    previous contents if any
  • "a" append open or create text file for
    update, writing at end

77
File Operations
(Cont)
  • Example of fopen() to open file

FILE file FILE fopen() if ((file
fopen(person.dat, r)) NULL)
printf(Cannot open a data file\n) else ...
File Pointer
78
File Operations
(Cont)
  • fclose() close file, flushes any unwritten data
    for stream, discards any unread buffered input,
    frees any automatically allocated buffer, then
    closes the stream. It returns EOF if any errors
    occurred, and zero otherwise.
  • Example

int fclose()...if (fclose(file) EOF)
printf(Cannot close file\n else printf(Now
file is closed\n)
79
File Operations
(Cont)
  • feof() check for end of file.
  • getc() is equivalent to fgetc except that if it
    is a macro, it may evaluate stream more than
    once.
  • int getc(file pointer)
  • putc() is equivalent to fputc except that if it
    is a macro, it may evaluate stream more than
    once.
  • int putc(int c, file pointer)
  • putchar(c) is equivalent to putc(c,stdout).
  • int putchar(int c)

if (!feof(file)) printf(It is not the end of
file now\n)
80
File Operations
(Cont)
  • fprintf() converts and writes output to stream
    under the control of format. The return value is
    the number of characters written, or negative if
    an error occurred.
  • int fprintf(file pointer, format, ...)
  • fscanf() reads from stream under control of
    format, and assigns converted values through
    subsequent arguments, each of which must be a
    pointer.
  • int fscanf(file pointer, format, ...)

81
File Operations
(Cont)
  • Example of File Operations Program

/ A PROGRAM TO WRITE ONE LINE TO sample.txt
/ include ltstdio.hgt main() FILE fp
char c fp fopen(sample.txt,
w) printf(Enter text below\n) while
(c ! \n) c getchar()
putc(c, fp) fclose(fp)
82
File Operations
(Cont)
  • Example of File Operations Program (Cont)

/ A PROGRAM TO READ TEXT FROM sample.txt AND
DISPLAY ON A MONITOR / include
ltstdio.hgt main() FILE fp char c fp
fopen(sample.txt, r) if (fp NULL)
printf(The file cannot be opened\n)
else while ((c getc(fp) ! EOF)
putchar(c) fclose(fp)
83
End
Question ?
Answer
Write a Comment
User Comments (0)
About PowerShow.com