IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Input / Output - Discussion

@ : Home > C Programming > Input / Output > Point Out Correct Statements - Discussion

1. 

Which of the following statement is correct about the program?

#include<stdio.h>

int main()
{
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n')
            i++;
    }
    fclose(fp);
    return 0;
}

[A]. The code counts number of characters in the file
[B]. The code counts number of words in the file
[C]. The code counts number of blank lines in the file
[D]. The code counts number of lines in the file

Answer: Option E

Explanation:

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.


Koushik said: (Sat, Jan 8, 2011 01:49:22 AM)    
 
Any one know - How to find space between words in that file?

Bill Roberts said: (Fri, Sep 16, 2011 02:30:55 AM)    
 
It appears that i++ is over one line below the if statement, if it is, than it counts the characters in the file, else it counts the number of newline characters. Newline characters are not always indicative of a line, on windows \r\n is a line where on *nix it is \n.

Hemendra said: (Fri, Jan 20, 2012 03:23:20 PM)    
 
Any one explain the answer in detail.

Ram said: (Fri, Mar 9, 2012 11:47:21 AM)    
 
Some explanations are well, but some more are not to be understood, will you explain shortly.

Mahi said: (Mon, Jun 11, 2012 11:13:49 PM)    
 
Firstly we are opening the file fp using fp = fopen("myfile.c", "r");
This also reads the file("r")
Now whatever the value equated to ch is counted
And we know that '\n' indicates every new lines arrival
By keeping track of \n in the statements
If(ch == '\n')
i++;
It results in number of new lines accessed

Sindhika K said: (Fri, Aug 31, 2012 11:29:45 PM)    
 
Really good explanation. Thank you. @mahi.

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!