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.






Sunday, 11 June 2023

Program counter and Rom space

What is Program counter and Rom space in the 8051?

We inspect or examine the capacity of the program counter (PC) listing in executing or implement an 8051 program. We also discuss or talk over ROM memory space or capacity for various 8051 family members.

8051 program counter, 8051 ROM space, embedded PC
Program counter and ROM space image-1

Program counter in the 8051

Another important or main register in the 8051 is the (program counter) PC. The program counters points or tip to the address of the next instruction or command to be executed. However, not all members or a constituent piece of a complex structure of the 8051 have the entire 64K bytes of on-chip ROM installed or put, as we will see soon.

Where the 8051 wakes

One question that we must ask or query about any microcontroller (or microprocessor) is: At what address does the CPU come to or wake up upon applying power to it? Each microprocessor is different or unlike. The 8051 family the microcontroller come to at memory address 0000 (that is, all members regardless and variation), when it is powered up. By force or powering up we mean applying Vcc to the RESET pin. In other words, when the 8051 is powered or force up, the PC (program counter) has the value of 0000 in it. Looks for the first instruction or command when it is booted. We achieve this by the ORG statement or declaration in the source program as shown earlier.

Placing code in program ROM

To get a better understanding or grip of the role of the program counter in fetching or adorable and executing or perform a program, we examine or survey the action of the program counter as each instruction or command is fetched or get and executed. As we can see, the opcode and operand for each instruction or command are listed on the left side of the list file.

After the program or plan is burned into ROM of an 8051 family member like as AT8951 or 8751, the opcode and operand are placed or set in the ROM memory locations starting at 0000.


Computer stuff kit tricks of Topics 55.









Saturday, 10 June 2023

Assembling and 8051 program

What is assembling and running an 8051 program?

Now that the basic or primary form of an Assembly language program has been given, the next question is: How it is created or generates, assembled, and made ready to run? The steps to create an executable or program Assemblylanguage program are outlined as follows.

8051 C program, c coding, assembling program
Assembling and 8051 program image-1

1.     First we use a computer program enabling the user to enter or editor to type in a program. Many outstanding or excellent editors or word processors are accessible or available that can be used to create and/or edit the program. A widely or range used editor is the MS-DOS EDIT program (or Notepad in Windows), which comes or near with all Microsoft operating systems.

For many assemblers, the file names follow the usual or habitual DOS conventions or compact, but the source or origin file has the extension or addition “asm” or “src”, depending or be controlled on which assembler you are using. Check your assembler for the convention or a way in which something is usually done.

 2.        The assembler converts or changes the instructions into machine code. The assembler will produce or build an object file and a list file.

 3.       Assemblers require or need a third step called linking. The link program takes or gets hold of one or more object files and produces or builds an absolute object file with the extension or addition “abs”. This abs file is used by 8051 trainers or instructor that has a monitor or detector program.

 More about “asm” and “obj” files

The “asm” file is also called the source or origin file and for this reason or cause some assemblers require that this file have the “src” extension.

Check your 8051 assembler or a program for converting instructions to see which extension it requires. As mentioned or bring up earlier, this file is created or produce with an editor such as DOS EDIT or Windows Notepad. In addition to creating or produce the object file, the assembler also produces the 1st file (list file).


Computer stuff kit tricks of Topics 54.





Friday, 9 June 2023

Strings in C language

What is Strings in C language?

There is no unrelated or separate data type for strings in C. They are use or treated as arrays of type char. A character array is a string or typically character if it ends with a null character (‘\0’). This null character is an run off or escape sequence with ASCII value 0. Strings are generally or normally used to store and manipulate or operate data in text form like words or sentences.

Strings in c, c program, string constant
String in C language image-1

1.1   String Constant or String Literal

A string constant or sustained is a sequence of characters enclosed or surrounded in double quotes. It is sometimes from time to time called a literal. The double quotes or repeat are not a part of the string.

Whenever a string constant or sustained is written anywhere in a program, it is stored somewhere or to some place in memory as an array of characters terminated or end by a null character (‘\0’). The string constant or sustained itself becomes a pointer to the first character in the array.

 Each character live in or occupies one byte and compiler automatically or no direct human control inserts the null character at the end. The string sustained or constant “The Mahal” is actually a pointer to the character ’T’. So whenever a string constant or sustained is used in the program it is replaced by a pointer pointing to the string.

 If we have a pointer variable or changing of type char*, then we can assign or allot the address of this, string constant to it as –

Char *p=”The mahal”;

Similarly when we write –

Printf(“computer stuff kit\n”);

Then actually or literally a pointer to character (char*) is passed to the printf() function.

If identical or similar string constants or sustained are used in a program, they will be stored separately or one by one at different memory locations. For example if a string sustained or constant “India” appears three times in a project or program then it will be stored thrice in memory.

We have studied that a string constant or sustained gives the address of first character in it, but there is an exception or deviation to this rule; when the string constant is used as an initialize or set to the value for a character array then it does not represent or appear for any address and it is not stored anywhere in memory. For example –

 char arr [5]=”Keep”

Here the string sustained or constant “Keep” is not stored in memory and hence does not represent or appear for any address. Note or record that the ‘b’ and “b” are different. ‘b’ is a character constant which represents or appear for the ASCII value of character ‘b’ while “b” is a string sustained or constant which consists of character ‘b’ and null character ‘\0’.


Computer stuff kit tricks of Topics 53.



Thursday, 8 June 2023

Pointers and Functions in C

What are Pointers and Functions in C?

The difference of opinion to the functions can be passed in two ways –

1.       Call by Value

2.       Call by Address

In call by value, only the values of arguments are sent to the function while in call by remark about, mark of arguments are sent to the purpose. In call by value procedure, any interchange made to the formal reasoning will not throw back on the real arguments.

Pointers in c language, Function in C language, call by value
Pointers and Functions in C language image-1

C uses only call by value when progress arguments to a function, but we can reproduce call by address by using pointers.

Here a and b are variables announced in the function main() while x and y are announced in the function value(). These an element resides at different addresses in store. Whenever the function value() is called, two variables are produce named x and y and are set to the value with the values of variables a and b.

This type of a limit going past is called call by value since we are only provide the values of real arguments to the calling function. Any working performed on variables x and y in the function value(), will not affect variables a and b.

Even though C does not use call by address, but we can reproduce it by passing addresses of variables as arguments to the function. To receive the addresses internal the function, we will need pointer variables.

Here we are progress mark of variables a and b in the function call. So the be given formal arguments in the function statement should be announced of pointer type. Whenever function ref() is called, two pointer to int variables, named p and q, we will be able to entrance variables a and b from function ref().

1.1   Function returning Pointer

We can have a function that come back a pointer. The syntax of statement of such type of function is –

Type *func(typ1, type2,…….);

While go back a pointer, make sure that the reminder address go back by the pointer will exist even after the the physical end of something of function.

Here we are come back a pointer which points to a regional variable. We know that a local variable live only inside the function. Take for granted the variable x is stored at address 2500, so the value of p will be 2500 and this value will be go back by the function func(). As soon as func() terminates, the local variable x will cause to live.

The address come back or returned by func() is allot or assigned to pointer variable ptr inside main(), so now ptr will carry or contain address 2500. When we dereference or obtain the address of a data item held in another location from ptr, we are the action or typing to entry or access the value of a variable that no longer exists. So never or at no time go back or return a pointer that point to a local variable.


Computer stuff kit tricks of Topics 52.