C Program to count number of lines in a file Last Updated : 29 Dec, 2022 Comments Improve Suggest changes Like Article Like Report C /* C Program to count the Number of Lines in a Text File */ #include <stdio.h> #define MAX_FILE_NAME 100 int main() { FILE *fp; int count = 0; // Line counter (result) char filename[MAX_FILE_NAME]; char c; // To store a character read from file // Get file name from user. The file should be // either in current folder or complete path should be provided printf("Enter file name: "); scanf("%s", filename); // Open the file fp = fopen(filename, "r"); // Check if file exists if (fp == NULL) { printf("Could not open file %s", filename); return 0; } // Extract characters from file and store in character c for (c = getc(fp); c != EOF; c = getc(fp)) if (c == '\n') // Increment count if this character is newline count = count + 1; // Close the file fclose(fp); printf("The file %s has %d lines\n ", filename, count); return 0; } Output: Enter file name: countLines.c The file countLines.c has 41 lines Time complexity: O(N) where N is total number of characters in given file Auxiliary space: O(1) because it is using constant space for variables and array filename Comment More infoAdvertise with us Next Article C Program to count number of lines in a file K kartik Follow Improve Article Tags : C Language cpp-file-handling Similar Reads C Program to count the Number of Characters in a File Counting the number of characters is important because almost all the text boxes that rely on user input have certain limitations on the number of characters that can be inserted. For example, the character limit on a Facebook post is 63,206 characters. Whereas, for a tweet on Twitter the character 2 min read Lex program to count the number of lines, spaces and tabs Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.Let's the how to count the number of lines, spaces and tabs 1 min read LEX program to add line numbers to a given file Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Examp 2 min read C program to count number of vowels and consonants in a String Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac 4 min read How to count set bits in a floating point number in C? Given a floating point number, write a function to count set bits in its binary representation. For example, floating point representation of 0.15625 has 6 set bits (See this). A typical C compiler uses single precision floating point format. We can use the idea discussed here. The idea is to take a 2 min read Like