Category: Learn C Online

Scope of Local and Global Variables in C Programming 1

Scope of Local and Global Variables in C Programming

The scope of variables refers to that portion of the program where the variables can be accessed. They are accessible in some portion of the program and in the other they are not accessible. Scope of a variable defines the portion of the program in which the set of variables can be referenced and manipulated. When a variable is required in a program, it can be declared as: Local variable Global variable Local Variables in C Programming Language The variables that are declared inside a function are called as local variables The scope is only within the function in which...

Coding Standards for Writing Functions 1

Coding Standards for Writing Functions

Whether its C programming language or C++ programming language or PHP, we should have a coding standard in place before starting with the code development. Coding standard is a predefined standard which should be followed during the code development which will improve the quality of the code. The coding standards varies from organization to organization. Each organization define their own coding standard and follow them to achieve the desired code quality. Here, we will define a coding standard for writing functions. Coding Standards for Writing Functions A function name should be preceded by fn The first character in the function...

How does using functions make writing large programs easier? 1

How does using functions make writing large programs easier?

Consider a program with 1000 lines. It is much easier to write a properly designed program consisting of 20 50-line functions than it is to write a program consisting of one 1000-line main program. Functions can be written, tested, and understood independently of one another. You can write a function, test it, and convince yourself that it is correct without worrying about the other functions in your program. It is much easier to get your mind around a 50-line thing than a 1000-line thing. Another benefit is that code can be reused between projects. If someone has previously written a...

Closing the File in C 2

Closing the File in C

Once we open a file, finished reading from the file, we need to close the file. This is done using the function fclose() through the statement: fclose(fp); Once we are done with closing the file, we can no longer read, write or perform any operation on the file unless we reopen it.  Note that to close the file, we don’t use the filename but the file pointer fp. On closing the file, the buffer associated with the file is removed from memory. While performing a write operation on a file, when we try to close the file using fclose(), three...

Reading from a File in C 2

Reading from a File in C

Once the file has been opened for reading using fopen(), as we have seen, the file’s contents are brought into buffer and a pointer is set up that points to the first character in the buffer. This pointer is one of the elements of the structure to which fp is pointing. To read the file’s contents from the memory, there exists a function called fgetc(). This has been used in our program as: ch = fgetc(fp); fgetc() performs the following operations: Reads the characters from the current pointer position Advances the pointer position so that it now points to the next character...

Opening a file in C 2

Opening a file in C

We can perform read and write operations on a file in C. But, before that we must open a file. To open a file in C, we have to call the function fopen() as shown below: FILE *fp; fp = fopen(“file1.C”,”r”); The above statement would open file named “file1.C” in “read” mode. It tells the computer that the file being opened would be used for reading purpose only. fopen() performs three important tasks when you open the file in “r” mode: Firstly, it searches the file to be opened on the disk Then it loads the file from the disk into...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet