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.





Monday 12 June 2023

String Library Functions in C

What are string library functions?

There are several or some library functions used to manipulate or operate strings. The prototypes for these functions or task are in header file string.h. We’ll discuss or talk over some of them below-

string, library functions, c language, c program
String library function in C language image-1

1.1   strlen()

This function or task returns the length or distance of the string i.e. the number of characters or nature in the string excluding or not taking someone the terminating null character. It accepts or receives a single argument or difference of opinion, which is pointer to the first nature or character of the string. For example strlen(“computer”) returns the value 8. Similarly is s1 is an array that contains the name “hyderabad” then strlen(s1) returns the value 9.

1.2   strcmp()

This function or task is used for comparison or contrast of two strings. The comparison or contrast stops when either the end of string is reached or extend or the analogous or corresponding characters in the two strings are not same. The non-zero value returned or go back on mismatch is the difference of the ASCII values of the non-matching or un-matching characters of the two strings-

Strcmp(s1, s2) return a value-

<0 when s1<s2

=0 when s1==s2

>0 when s1>s2

Generally or normally we don’t use the exact or precise non-zero value returned in case of mismatch or discrepancy. We only need to know its sign or indication to compare the alphabetical place of the two strings. We can use this task to sort the strings alphabetically.

1.3   strcpy():

This task is used for copying on string to another string. Strcpy(str1, str2) duplicate str2 to str1. Here str2 is the source string and str1 is something is going or destination string. If str2= “computer” then this function duplicate “computer” into str1. This function takes pointers to two strings as arguments or logic and returns or goes back the pointer to first string.

1.4   strcat()

This task is used for concatenation or series of two strings. If first string is “abcd” and second string is “efgh” then after using this task the first string becomes “abcdefgh”.

Strcat(str1, str2); /*concatenates or series str2 at the end of str1 */

The null character or nature from the first string is removed, and the second, string is added or enhancing quality at the end of first string. The second string remains unchanged.

This task takes pointer to two strings as arguments or logic and returns a pointer to the first (concatenated or series) string.


Computer stuff kit tricks of Topics 57.




8051 Data type and Directives

What is 8051 data type and directives?

The 8051 microcontroller has only one data type or a particular kind of data item. It is the job of the programmer or a device that automatically controls the operation to break down data larger than 8 bits.

8051 microcontroller, data type and directives
Data types and Directives image-1

DB (define byte)

The DB directive is the most extensively or widely used data directive in the program for converting instructions or assembler. It is used to explanation or defines the 8-bit data. For decimal, the “D” after the decimal number is voluntary or optional, but using “H” (hexadecimal) and “B” (binary) for the others is required. Regardless or anyway of which is used, the assembler will change or convert the numbers into hex. To indicate or specify ASCII, simply place the characters or nature in quotation marks (‘like this’). The assembler will assign or allot the ASCII code for the numbers or nature or characters attention.

ORG (origin)

The ORG directive is the beginning of the address used to indicate or specify. After ORG can be either or similarity in hex or in decimal of the number that comes. It is decimal and assembler or instruction will change or convert it to hex form the number is not followed by H,.

EQU (equate)

This is used to define a constant or sustained without occupying or settled a memory location. For a data item but link or associates a continuous or constant value with a data marker or label the EQU directive does not set aside or apart storage so that when the label or marker appears in the program; of something else for the label, its constant value will be substituted or taking the place.

END directive

Another important pseudocode or used in program design is the END directive. This indicates or specify to the computing the end of the source (asm) file. Anything or no matter what after the END directive is ignored or take no notice of by the assembler, the END directive is the last line of an 8051 program or plan, meaning that in the source code. Some assemblers or computing use “.END” (notice the dot) instead of |”END”.


Computer stuff kit tricks of Topics 56.