Wednesday 21 June 2023

Including files in C

What is including files in C?

The preprocessor directive #include is used to include or cover a file into the birthplace or source code. We have already used this command or directive to include header files in our plan or programs. The filename should be within viewpoint or angle brackets or double repeat or quotes. The syntax is –

#include <filename>

#include “filename”

Preprocessor, define, files in C, C language
C program library files Image-1

The preprocessor return or replaces the #include directive or command by the satisfy or contents of the specified file. After including the file, the total or entire contents of file can be used in the plan or program. If the filename is in double repeat or quotes, first it is look or searched in the current directory (where the birthplace or source file is present), if not found there then it is look or searched in the include directory. If the filename is with viewpoint or angle brackets, then the file is searched in the quality or standard include directory only. The statement or specification of standard or quality include directory is execution or implementation defined.

Include files can be fixed or nested i.e. an included file can carry or contain another #include directive.

1.1 The #define and #undef Directives

The #define directive is the most usual or common preprocessor directive, which tells the preprocessor to return or replace every event or occurrence of a particular character string (that is, a macro name) with a define or specified value (that is, a macro body).

The syntax for the #define directive is

                #define macro_name macro_body

Here macro_name is a proof or identifier that can carry or contain letters, numerals, or underscores. Macro_body may be a string or a data item, which is old or used to substitute each macro_name found in the program or plan.

As bring up or mentioned earlier, the running or operation to replace event or occurrences of macro_name with the value define or specified by macro_body is known as macro exchange or substitution or macro expansion.

The value of the macro body define or specified by a #define directive can be any character string or number.

                                #define STATE_NAME “Texas”

Then, during preprocessing, all event or occurrences of STATE_NAME will be replaced by “Texas”.

                #define SUM (12+8)

On the other hand, you can use the #undef directive to separate or remove the definition of a macro name that has been previously defined.

The syntax for the #undef directive is

                                #undef macro_name

Here macro_name is an identifier that has been previously explain or defined by a #define directive.

The #undef directive “undefines” a macro name.

For case or instance, the following segment of code:
#define STATE_NAME “Texas”
printf (“I am moving out of %s. \n”, STATE_NAME);
#undef STATE_NAME


Computer stuff kit tricks of Topics 64.



Tuesday 20 June 2023

The C Preprocessor in C language

What is C Preprocessor?

In writing first C program or plan, you learned or take in how to use the #include preprocessor or initialization directive to include C header files. Since then, the #include instruction or directive has been used in every plan or program.

C preprocessor, c language, c language basics
C Preprocessor image-1

1.1   C Preprocessor

If there is a constant or continuous appearing in several or some places in your program, it’s a good idea to link or associate a figurative or symbolic name to the continuous or constant, and then use the symbolic or figurative name to replace the constant all over or throughout the program. There are two upper hands or advantages in doing so. First, the plan or program will be more clear or readable. Second, it’s effortless or easier to maintain plan or program. For occasion or instance, if the values of the constant needs to be switch or changed, find the declaration or statement that link or associates the constant with the figurative or symbolic name and replace the sustained or constant with the new one. Without using the figurative or symbolic name, you have to look everywhere in your plan or program to replace the constant.

 

C has a particular or special program or plan called the C Preprocessor that allows computer specialist or programmers define and link or associate symbolic names with continuous or constants. In fact, the C preprocessor uses the expressions or terminology macro names and macro body to refer to the figurative or symbolic names and the continuous or constants. The C preprocessor or initialization runs before the compiler. During preprocessing, the working or operation to replace a macro name with its link or associated macro body is called macro substitution or macro expansion or growth.

 

In addition, the C preprocessor or initialization gives you the capacity to include other source files. For instance, we’ve been using the preprocessor command or directive #include to include C header files, such as stdlib.h, stdio.h, and string.h, in the plan or programs. Also, the C preprocessor or initialization enables to compile or organize different sections of program or plan under specified conditions.

1.2   The C Preprocessor Versus the Compiler

One major or important thing need to recollect or remember is that the C preprocessor is not part of the C compiler.

The C preprocessor uses a unlike or different syntax. All directives or instruction in the C preprocessor begin with a pound sign (#). In other words, the pound sign indicate or denotes the beginning of a preprocessor or initialization directive, and it must be the first non space nature or character on the line. The C preprocessor is line place or oriented. Each macro statement or declaration ends with a new line character or nature, not a semicolon. (Only C statements end with semicolons.) One of the most common or usual mistakes made by the programmer is to place a semicolon at the end of a macro statement. Fortunately, many C editor or compilers can catch such errors.


Computer stuff kit tricks of Topics 63.



Monday 19 June 2023

Command line arguments in C

What are command line arguments?

1.1   why we need command line arguments

The plan or program was performing or executing at command prompt or occasion we are giving the input worth or values as a part of execution. Before starting execution or performance we want to give input values or worth along with the executable filename. For this, the instruct or command line args were using while C executable or performance file name is giving at instruct or command prompt.

command line, arguments in C, declaration of main function
Command line arguments in C Image-1

C provides a fairly or equitably simple mechanism or machine for recover or retrieving command line parameters go into or entered by the user. Command-line disagreement or arguments are given after the name of a program in instructs or command-line operating systems like DOS or Linux, and are move or passed in to the program from the operating system. In fact, main can actually receive or accept two arguments: one argument argc variable or parameter is number of command or instruct line arguments. Second argument argv parameter or variable argument is a full list of all of the instruct or command line arguments.

 

1.2   declaration of main function

int main (int argc, char *argv[]);

The digit or integer, argc is the argument count. It is the number of arguments move or passed into the program from the command or instructs line, including the name of the program. *argv[]. The array of nature or character pointers is the listing of all the arguments. You can use each argv component or element just like a string, or use argv as a two size or dimensional array. argv [argc] is a null pointer.

Almost any program or plan that wants its variable or parameters to be set when it is carry out or executed would use this. One common use is to write a task or function that takes the name of a file and outputs the entire text of it onto the screen.

/*Program to Access command line arguments*/

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf(“Argc=%d\n”, argc);
for (int i=1; i<argc; i++)
printf(“argv[%d]=%s\n”, I, argv[i]);
return (0);
}
OUTPUT
at command line: ./a.out hello testing one two
argc=5
argv[0]=./prog9
argv[1]=hello
argv[2]=testing
argv[3]=one
argv[4]=two

In this code, the main plan or program accepts two variable or parameters, argv and argc. The argv parameter or variable is an array of pointers to string that carry or contains the parameters entered when the program or plan was call on or invoked at the UNIX command line. The argc digit or integer contains a count of the number of parameters. This certain or particular piece of code types out the command line variable or parameters.


Computer stuff kit tricks of Topics 62.





Sunday 18 June 2023

Random Access to Files in C

What is Random access to files in C?

We can access or way in the data stored in the file in two ways, randomly or sequentially. So far we have used only sequential or together access in our programs. For example if we want to access the forty-forth record or data then first forty-three records or data should be read sequentially or together to reach the forty-fourth record. In random access, data can be accessed and processed or prepared randomly i.e. in this case the forty-fourth record or data can be accessed directly. There is no need to read each record or data sequentially or together, if we want to access or entry a particular data or record. Random access or entry takes less time than a together or sequential access.

Random access, c language, c language basics
Random access to file image-1

C supports these functions or task for unsystematically or random access files processing:

fseek ()

ftell()

rewind()

1.1   the fseek() and ftell() functions

The file position or place indicator has to point to the desired or want position in a file before data can be read from or written or put down to there. You can use the fseek() task or function to move the file position or place indicator or measure to the spot you want to access in a file.

The syntax for the fseek() function is

        #include <stdio.h>

        Int fseek (FILE *steam, long offset, int whence);

Here stream is the file arrow or pointer associated or related with an opened file. Offset indicates or show the number of bytes from a fixed place, state by whence, that can have one of the following essential  worth or values represented by SEEK_SET, SEEK_CUR, and SEEK_END. If it is victorious, the fseek() function returns 0; otherwise, the function or task returns a nonzero value. You can find the values represented or act for by SEEK_SET, SEEK_CUR and SEEK_END in the header file stdio.h.

 

If SEEK_SET is chosen or pick as the third argument or disagreement to the fseek() task or function, the offset is counted or add up from the beginning or birth of the file and the worth or value of the offset is greater than or equal to zero. If, however, SEEK_END is picked up, then the offset or counterbalance starts from the end of the file; the worth or value of the offset or counterbalance should be negative. When SEEK_CUR is passed or move to the fseek() function, the offset is calculated from the current or present value of the file position indicator.

Some example of usage of fseek() function are:

1.       fseek(p, 10L, 0);

Origin is 0, which means that displacement or expulsion will be relative to birth or beginning of file so position pointer is trip or skipped 10 bytes forward from the beginning of the file. Since the second argument or disagreement is a long integer, so L is attached with it.

2.       Fseek(p, 8L, SEEK_SET);

Position pointer is leap or skipped 8 bytes forward from the beginning of the file.

3.       fseek (p, -5L, 1);

Position pointer is trip or skipped 5 bytes backward from the current position.

4.       Fseek(p, -6L, SEEK_END);

Position pointer is bound or skipped 6 bytes backward from the end of file.

5.       Fseek (p, 0L, 0);

This means 0 bytes are jump or skipped from the beginning of file. After this statement position or place pointer points to the beginning of file.


Computer stuff kit tricks of Topics 61.








Saturday 17 June 2023

Files in C language

What is files in C language?

The input and output operations or running that we have performed or execute so far were done through screen and keyboard only. After closing or termination of program, all the entered or undertake data is lost because main or primary memory is unstable or volatile. If the data has to be used later, then it becomes necessary or required to keep it is eternal or permanent storage device. C supports the theory or concept of files through which data can be stored on the secondary storage device or disk. The stored or keep data can be read whenever required or need. A file is a assembly or collection of related data placed on the disk.

C language, streams, binary logic, files
Files in C language image-1

1.1   Files versus streams

The C language provides or gives a set of rich library functions or task to perform input and output (I/O) operation. Those functions or task can read or write any type of information or data to files. Before we go any far or further in talk over or discussing the C I/O functions, let’s first understand the definitions of files and streams in C.

 

1.1.1          File

In C, a file can refer or mention to a disk file, a final or terminal, a tape drive, or a printer. In other words, a file represents or acts for a concrete or real device with which you want to exchange or interchange information. Before you perform or carry out any communication or links to a file, you have to open the file. Then you need to close the unfold or opened file after you finish exchanging information with it.

 

1.1.2          Stream

The data flow you shift or transfer from your program to a file, or vice versa, is called a stream, which is a sequence or series of bytes. Not like a file, a stream is self-dependent or device-independent. All streams have the same efforts or behavior. To perform or execute I/O operations, you can read from or write to any type of files by simply link or associating a stream to the file.

There are two formats or setup of streams. The first one is called the text stream, which contain or consists of a sequence or series of characters (that is, ASCII data). Depending or turn on the compilers, each character line in a text stream may be finished or terminated by a new line character. Text streams are used for written or textual data, which has a compatible or consistent aspect or appearance from one conditions or environment to another or from one machine or instrument to another.

The second format of streams is calling the binary stream, which is a sequence or series of bytes. The content of an .exe/.obj /a.out file would be one example. Binary streams are first or primarily used for non-textual data, which is need or required to keep the exact contents of the file.


Computer stuff kit tricks of Topics 60.

What are control statement in C language?

What are arrays and pointer in C language?

What is C language basics?



Declaring Structure Variables in C

What is declaring structure variables?

By explain a structure we have only generate a format, the real use of structures will be when we declare variables or flexible based on this format. We can state or declare structure variables in two ways –

C language, structure definition, using the structure tag
Declaring structure variables image-1

1.       With structure definition

2.       Using the structure tag

1.1   With structure definition

Struct student{

                                char name [10];

                                int rollno;

                                float marks;

                                }stu1,stu2,stu3;

Here stu 1, stu2 and stu 3 are variables or adjustable of type struct student. When we declare a variable while explain or defining the structure device or template, the tag name is optional.

 

1.2   Using the structure tag

We can also state or declare structure variables using organization or structure tag. This can be written as –

      struct student{

                                char name [10];

                                int rollno;

                                float marks;

                                };

struct student stu1, stu2;

struct student stu3;

Here stu1, stu2, and stu3 are structure or organization variables that are state or declared using the structure tag student. Declaring a structure variable set aside or reserves space in memory. Each structure variable state or declared to be of type struct student has three members viz. rollno, name and marks. The editor or compiler will reserve space for each variable enough or sufficient to hold all the members. For example each variable of type struct student will settled or occupy 28 (20+4+4) bytes.

 

1.3   Initialization of structure variables

The pattern or syntax of establishes structure variables are alike to that of arrays. All the worth is given in wavy or curly braces and the number, order and type of these values should be same as in the structure device definition. The establishes values can only be continuous or constant expressions.

                                sturct student {

                                                char name [10];

                                                int rollno;

                                                float marks;

                                                }stu1={“Gopi”,25,9};

                                                Struct student stu2={“Pasi”, 24,67.5};

Here value of members of stu1 will be “Gopi” for name, 25 for rollno, 98 for marks. The values of members of stu2 will be “Pasi” for name, 24 for rollno, 67.5 for marks.

Note: we cannot establish members while explain the structure.

 If the number of establish is less than the number of members then the carry on members are establishes with zero. For example if we have this establishes –

   struct student stu1 = {“Gopi”};

Here the members rollno and marks of stu1 will be establishes to zero. This is equal to the initialization –

          struct student stu1 = {“Gopi”, 0, 0};


Computer stuff kit tricks of Topics 59.

What are control statement in C language?



Tuesday 13 June 2023

Structures and Unions in C

What is Structures and Unions?

Array is a collection of same type of component but in much real life appeal we may need to group mismatched types of logically related data. For example if we want to generate a record of a person that contains name, age and height of that person, then we can’t use array since all the three data elements are of different types. For example record has three name (string), age (int) and height (int).

structures, arrays, defining a structure, c language program
Structures and Unions in C Image-1

To store these connected fields of different data types we can use a structure, which is able of storing diverse data. Data of mismatched types can be grouped jointly under a single name using structures. The data component of a structure is referred to as members.

1.1   Defining a Structure

Definition of a structure generates a template or format that report the quality of its members. All the variables that would be state of this structure type, will take the form of this format. The general syntax of a structure definition is-

struct tagname{

datatype member1;

datatype member2;

……………………………

datatype memberN;

};

Here struct is a keyword, which tells the accumulator that a structure is being defined. member1, member2, ……………… memberN are known as members of the structure and are communicate inside curly braces. There should be a semicolon at the end of the wavy braces. These members can be of any data type like int, float, array, char, pointers or another structure type. tagname is the name of the structure and it is used far in the program to communicate variables of this structure type.

 

Definition of a structure comes up with one more data type in adding to the built in data types. We can state variables of this new data type that will have the format of the explain structure. It is major to note that definition of a structure format does not put aside any space in memory for the members; space is booked only when actual adaptable of this structure type are declared. Although the syntax of statement of major inside the format is identical to the syntax we use in declaring variables but these members are not variables, they don’t have any alive until they are fond of with a structure variable. The member names inside a structure should be mismatched from one another but these names can be alike to any other variable name state outside the structure. The member names of two different structures also be same.

 

Let us take an example of describe a structure format.

                struct student {

                                                char name [20];

                                                int rollno;

                                                float marks;

                                                };

Here student is the structure label and there are three members of this structure viz rollno, name and marks. Structure format can be explain globally or locally i.e. it can be put down before all functions in the program or it can be locally near in a function. If the format is global then it can be used by all task while if it is local then only the function carry it can use it.


Computer stuff kit tricks of Topics 58.