Saturday, 24 June 2023

Data Structure in C

What is Data Structure?

Consider a plan or program to work on the data of students in a school. Each students record would have definite certain fields namely naming or identity number, student name, class etc. From what we have take in or learnt so far we could use a structure to summarize or encapsulate these fields. We store these data or records in files, so we could use structure flexible or variables to read a record from a file or write a data or record to a file.

Data Structure, c language, C program
Data Structure Image-1

If we wanted to operate or manipulate several data or records at once we would need an array of structures. Say if we wanted to show, all the students data or records in the order of their naming or identity numbers we would need to read them from the file they are keep or stored into an array of structures.

This is because the C language does not offer any file company or organization methods other than the uninterrupted or sequential file operation or organization. In other words when you write data or records into a file in the C language you write the data or records in a sequence. The language does not offer any operation or organization such as sign or index File Company or organization where the data or records can be read or accessed at random on a key value.

The fseek function does allow random read or access to file data but only based on the byte balance or offset. Therefore we first have to access or read them into an array of structures, sort the array by the key area or field, and display each element of the array.

Here is where we can see the issue or problem with arrays during coding or programming. The question, which appear or arises, is what should the size of the array? Obviously or clearly our array must have at least as many elements or components as the biggest or maximum number of records we plan or intend reading from the file.

Now, if we utilize or use an array that is too big (say there are only five hundred data or records in the data file and the array size we have put or set, to read or access these data or records into, is one thousand elements or components long) every time we run the plan or program we are going to set aside or reserve memory for one thousand structures but use only five hundred.

Worse or poor would be the case if the number of students in the school grow or increases to over a thousand. Then we would have to change the plan or program code and grow or increase the army size and keep doing so forever.


Computer stuff kit tricks of Topics 66.





Thursday, 22 June 2023

Compilation Stages in C

What is Compilation Stages?

Consider a simple program hello.c

#include<stdio.h>
int main()
{
printf(“hello, computer\n”);
return 0;
}

C language, compilation stages tips, tips and tricks
Compilation stage image-1

The first work or action is to activate a preliminary or opening process called the initialization or pre-processor. In the case of hello.c all it does is to return or replace the line #include <stdio.h> with the file stdio.h from the cover or include files library. The file stdio.h provides or gives us with a suitable or convenient way of telling the author or compiler that all the i/o task or functions exist. There are a few other little item or things in stdio.h but they need not cover or concern us at this stage.

In order to see what the initialization or pre-processor actually outputs, you might like to topic or issue the command:

cc-E hello.c – o hello.i

The ‘cc’ command will operate or activate the ‘C’ compilation or collection system and the –E option will stop the collection or compilation process after the pre-processing stage, and another file will have look or appeared in your directory. Have a look, find hello.i and use the editor in view method or mode to have a look at it. So issue the command:

vi hello.i

You will see that a number of lines of work or text have been added at the front of the hello.c plan or program. What’s all this matter or stuff? Well, have a look in the file called /usr/include/stdio.h again using the view instruct or command.

vi /usr/include/stdio.h

Now the next period or stage of getting from your program text to a perform or executing plan or program is the collection or compilation of your text into an assembler code program. After all that is what a editor or compiler is for – to turn a high level language script into a plan or program.

Let’s see what happens by issuing the instruct or command

cc –C hello.s –o hello.o

Now, yet again there is another file in your index or directory – this time the suffix is “.o”. This file or folder is called the object file. It carry or contains the machine command or instructions keep in touch or corresponding exactly to the help or mnemonic codes in the .s file. The next stage in the collection or compilation process is called by a diversity or variety of names –“loading”, “link editing”, “linking”. What happens is that the tool or machine instructions in the object file (.o) are joined to many more order or instructions selected from a huge or enormous collection of functions in a library.


Computer stuff kit tricks of Topics 65.

What is C Preprocessor in C language?


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?