Professional Documents
Culture Documents
File I/O in C
Files in C
In C, each file is simply a sequential stream of bytes. C
imposes no structure on a file.
Whenever you are to read from or write to the file, you must first
open the file and assign the address of its file descriptor (or
structure) to the file pointer variable.
Algorithms
Example 1
if ( grade >= 60 )
{
printf( "Passed\n" );
}
Notice that the C code corresponds closely to the
pseudocode. This is one of the properties of pseudocode
that makes it such a useful program development tool.
Flowcharting the single-selection if
statement.
The generic form of if…else
The current year and the year in which the employee joined the
organization are entered through the keyboard. If the number of
years for which the employee has served the organization is greater
than 3 then a bonus of $2500 is given to the employee. If the years
of service are not greater than 3, then the program should do
nothing.
The if…else Selection Statement
if ( grade >= 60 )
{
printf( "Passed\n" );
} /* end if */
else
{
printf( "Failed\n" );
} /* end else */
The flowchart of if…else
Nested if…else statements
if ( grade >= 90 )
printf( "A\n" );
else
if ( grade >= 80 )
printf("B\n");
else
if ( grade >= 70 )
printf("C\n");
else
if ( grade >= 60 )
printf( "D\n" );
else
printf( "F\n" );
Forms of if
The if statement can take any of the following forms:
Note: A leap year has 366 days, as opposed to a common year, which
has 365. Nearly every 4 years is a Leap Year, and we add a Leap
Day, an extra – or intercalary – day on February 29
Loops
int i=1;
while(i<=10)
{
printf("%d\n",i);
i++;
}
Formulating Algorithms: Counter-
Controlled Repetition
main()
{
inti;
for(i=1;i<=10;i++)
printf(“%d\n”,i);
}
All the following increment can be used:
i=i+1
i++
i+=1
Do- while loop
do
statement
while ( condition );
Example
main()
{
int counter=1;
do{
printf(“%d”,counter);
} while(++counter<=10);
for(x=1;x<=10;x++)
{
if(i==5)
{
break;
}
printf ("%d\n",x);
}
The continue Statement
Exercise 1
a) Obtain two numbers from the keyboard, compute their sum and
display the result.
b) Obtain two numbers from the keyboard, and determine and display
which (if either) is the larger of the two numbers.
c) Obtain a series of positive numbers from the keyboard, and
determine and display their sum. Assume that the user types the
sentinel value -1 to indicate “end of data entry.”
Q &A