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.








Wednesday, 7 June 2023

Arrays and Pointers in C

What are Arrays and Pointers?

The variables that we have used till now are efficient of set aside only one worth at a time. Think about a position when we want to store and show the age of 100 employees.  There are three type of following:

arrays, pointers, c language, c program
Pointers and Arrays in C language image-1

1.       Declare 100 unlike variables to store the age of employees.

2.       Assign a value to each variable.

3.       Display the value of each variable.

In spite of the fact that we can perform our task by the above three steps but just visualizes how difficult it would be to pick up so many variables in the program and the program would begin to be very lengthy. The idea of arrays is useful in these types of position where we can group alike kind of data items.

An array is an assembly of similar type of data thing and each data item is called an component of the array. The elements of array part the same variable name but each element has a unlike lead number known as subscript.

For the above sample we can take an array variable age[100] of type int. The size of this array variable is 100 so it is ability of storing 100 integer values. The separate elements of this array are –

Age [0], age[1], age[2], age[3], ………………………………………………………..age[98], age[99]

Arrays can be one dimensional or multidimensional. The number of subscripts decide the dimension of array. A one-dimensional array has one subscript, two dimensional array has two subscripts and so on.

1.1   One Dimensional Array

 

1.1.1          Declaration of 1-D array

Like other easy variables, arrays should also be declared previous to they are used in the program.  The following syntax declaration of an array is –

Data_type array_name[size];

Here array_name indicate the name of the array and it can be any reasonable C identifier, data_type is the data type of the elements of array. The size of the array defines the number of elements that can be set aside in the array.

Some examples of array declarations –

int age[100];

float sal[5];

 char grade[20];

1.1.2          Accessing 1-D Array Elements

Identify the array name followed by subscript in square bracket can entrance the elements of an array. In C, the array start subscripts from 0.

1.1.3          processing 1-D Arrays

For take care of arrays we generally use a for loop and the variable is used at the site of subscript. The beginning value of loop variable is taken 0 since array subscripts from zero. The loop variable is enlarging by 1 each time so that we can entry and processes the next element in the array.


Computer stuff kit tricks of Topics 51.







Tuesday, 6 June 2023

Function in C

What is function in C language?

A function is independent subprogram that is denoting to do some specific, described task. A C program be made up of one or more functions. If a program has only one purpose then it must be the main() function.

Function in C language, function returning value
Function in C language image-1

1.1   Advantages of using functions:

1.       Normally a inconvenient problem is separated into sub problems and then solved. This divided and beat skill is implemented in C through functions. A program can be separated into functions, each of which executes some particular task. So the use of C functions designed or produced in separate piece or parts and divides the work of a program.

2.       When some particular code is to be used more than once and at dissimilar places int eh program, the use of functions keep away from retelling of that code.

3.       The program suit simply comprehensible, modifiable and easy to detect and test. It becomes easy to write the program and interpret what work is done by each part of the program.

4.       Functions can be retaining in a library and the state or quality of  being reusable can be achieved.

C programs have two types of functions:

1.       Library functions

2.       User-defined functions

 

1.2   Library functions

C has the space to prepare library functions for carry out some working. These functions are present in the C library and they are limited. The functions scanf() and printf() are input output library task. Similarly we have functions like strlen(), strcmp() for string unscrupulous.

To use a library function we have to embrace function header file using the hardware or software that processes information before it heads to another program directive #include. For example to use input output task like printf(), scanf() we have to embrace stdio.h, for string library string.h should be included.

 

1.3   User-Defined Functions

Users can produce their own function for execute any particular task of the program. These types o functions are called varied by the user functions. To create and use these functions, we should know about these three things they are:

1.       Function definition

2.       Function call

3.       Function declaration

 

1.4   Function definition

The function definition is made up of the whole illustration and code of a function. It tells what the task is doing and what are its inputs and outputs. A function definition contain of two parts – a function header and a function body.

 

1.5   Function call

The function definition report what a function can do, but to literally use it in the program the function should be called to some place. A function is called by simply writing its name come after by the disagreement list inside the parentheses.

 

1.6   Function Declaration

The calling function needs data about the called function. If explanation of the called function is placed before the calling function, then statement is not needed.


Computer stuff kit tricks of Topics 50.




Monday, 5 June 2023

Control statements in C

What are Control statements?

In C programs, statements are performing sequentially in the organization in which they look in the program. But occasionally we may want to use a state for perform only a part of program. Also many affairs arise where we may want to program some statements some times. Control statements allow us to specify the order in which the many order in the program are to be executed. This control the flow of control. Control statements visibility how the control is move to other parts of the program. C language bear four types of control statements, which are as follows:

If ….else, loop, while, goto, switch, do…. while, for.

C programs, Loop, while, goto, switch, do
Control Statements in C language image-1

1.1   compound statement or block

A compound statement or a block is a set of statements surrounded within a match of wavy braces {}.

The statements inside the slab are implementing sequentially.

A compound statement is syntax similar to a one statement and can look anywhere in the program where a one statement is permit.

1.2   if…..else

If…else is a bi-directional conditional control statement. This statement is used to check a condition and take one of the two viable actions. If the condition is correct then a one statement or a block of statements is (one part of the program) performed, otherwise another single statement or a block of statements is (other part of the program) performed. Remember that in c, any nonzero value is regarded as true while zero is look on as false.

1.3   Nesting of if….else

We can have another if….else statement in the if chunk or the else hunk. This is called nesting of if….else statements. Here is an example of nesting where we have if…..else inside both if block and else block.

1.4   else if Ladder

This is a kind of nesting in which there is an if….else statement in each else part excepting the last else part. This type of nesting is often used in plan and also known as else if ladder. Here each condition is checked, and when a order is found to be true, the statements corresponding to that are programmed, and the control comes out of the nested form without checking still existing conditions. If none of the conditions is correct then the last else part is programmed.

1.5   Loops

Loops are used when we want to programmed a part of the executed or a block of statements some times. For examples, assume we want to print “C is best” 10 times. One way to get the wished output is – we write 10 printf statements, which is not better. Other way out us – use loop. Using loop we can write one loop statement and only one printf statement, and this speak to is definitely better than the first one.


Sunday, 4 June 2023

Operators and Expressions in C

What are an operators and expressions?

An operator states an operation to be performed that relent a value. Several operators to form an expression can fix the constants, variables. An operand is a information item on which an operator behave. Some operators need two operands, while others behave upon only one operand. C includes a big number of operators that fall under several different categories, which are as follows

C language, operators, Expressions in C
C language operators and expressions image-1

1.       Arithmetic operators

2.       Bitwise operators

3.       Comma operator

4.       Logic operators

5.       Relational operators

6.       Sizeof operator

7.       Increment and decrement operators

8.       Assignment operators

9.       Conditional operator

10.   Other operators

 

1.1   Arithmetic operators

Arithmetic operators are used for numeric calculations. They are of two types –

1.       Unary arithmetic operators

2.       Binary arithmetic operators

 

1.2   Assignment operator

A worth can be set aside in a not consistent with the use of assignment operator. This assignment operators “=” is used in assignment statements and assignment expressions.

 

1.3   Increment and Decrement operators

C has two functional operators increment (++) and decrement (--). These are unary operators as they operate on a single operand.

1.4   Relational operators

Relational operators are functional to collate values of two expressions be controlled on their relations. An expression that carries relational operators is called relational expressions. if the relation is false then the value of expression is 0 and If the relation is true then the value of relational expression is 1.

 

1.5   Logical or Boolean operators

An expression that integrates two or more expressions is termed as a logical expression. For merge these expressions we use logical operators.

1.6   Conditional operator

Conditional operator is a ternary operator which requires three operands expressions.

This is written as – TestExpression ? Expression1 : expression 2

Here first the expression a>b is evaluated, if the worth is true then the value of variable as the value of conditional expression otherwise the value of b becomes the value of conditional expression.

1.7   Comma operator

The comma operator (,) is used to permit dissimilar expressions to look in situations where only one expression would be used. The comma operators break up the expressions. The separated expressions are assessing from left to right and the type and value of the rightmost expression is the type and value of the compound expression.

1.8   Sizeof operator

Sizeof is a unary operator. This operator gives the size of its operand it terms of bytes.

1.9   Bitwise operators

C has the capacity to support the manipulation of data at the bit level. Bitwise operators operate on integers only.


Computer stuff kit tricks of Topics 48.




Friday, 2 June 2023

Input and Output in C

What is Input – Output in C?

There are three main task of any program – it takes data as input, procedure this data and gives the output. The input operation require motion of data from an (generally keyboard) input device to computer memory, while in output function the data moves from computer reminder to the (generally screen) output device. The input output is executed through a set of library operation that is give with every C compiler.

C compiler, computer memory, header files
Input and Output in C language image-1

There are some header files that supply mandatory details in support of the many library functions. These header files are set foot in the program using the #include instruction at the starting point of the program. For example if a project uses any function from, the quality I/O library, then it should involve the header file stdio.h as-

#include <stdio.h>

Likewise there are other header files like stdlib.h, string.h that should be added when certain library functions are used.

An easy method for taking the data as input is to give the usefulness to the variables by task statement. For example-

int basic = 2000;

Char ch=’y’;

But in this way we can give only specific data to the variables. The second procedure is to use the input function scanf(), which takes the input facts from the keyboard. In this procedure we can give any value to the change at run time.

Conversion specifications

The functions scanf() and printf() make use of change stating to identify the type and size of data. Each transformation specification must begin with a percent sign(%). Some altering specifications are as given below-

%d – a decimal integer, %x – a hexadecimal integer, %f – a floating point number, %c – a single character, %o – an octal integer, %s – a string and %u – an unsigned decimal integer

Reading input data

Input data can be beginning into the memory from a worth input device (key board). C provides the scanf() library purpose for beginning input data. This task can take all types of utility as input. The scanf() function can be written as – scanf(“control string”, address1, address2,so on);

This task should have at least two variables. A first variable is a authority string, which carry conversion specification characters. It should be within double restate. The conversion specification nature may be one or more; it be able to trust on the number of change we want to input. The other variable are mark of variables. In the scanf() function at least one mark should be present. The mark of a variable is found by prior the changing name by an ampersand (&) sign. A string variable is not lead up to by & signs to get the mark.


Computer stuff kit tricks of Topics 47.





Thursday, 1 June 2023

8051 Assembly language

What is an assemble language code?

We look at the inside of the 8051. We reveal some of the more used registers of the 8051 with easy command (instructions) such as MOV and ADD. We inspect assembly language and machine language coding and definition terms such as opcode, mnemonics, operand, etc.  The process of collect and produce a ready to run code for the 8051.

8051 microcontroller, embedded assembly, coding
8051 Assembly language image-1

Step by step implementation of an 8051 and the part of the program counter are look into. We look at some interval between used assembly language pseudocode, directives, and data types connected to the 8051. We talk about the flag bits and how they are influenced by arithmetic command. Allotment of RAM memory core the 8051 plus the heap and roll banks of the 8051.

Registers

In the CPU, directories are used to store information for the moment. Those see to could be a byte of data to be processed, or a mark pointing to the data to be get. The huge seniority of 8051 registers are 8-bit registers.

In the 8051 there is only one data type: The eight (8) bits of a register are shown in the diagram from the MSB (most significant bit) D0. With an (eight) 8-bit data type, any data larger than (eight)8 bits must be splintered into (eight) 8-bit block before it is processed.

The accumulator, is used for all arithmetic and logic command register A. The most widely used index of the 8051 are (accumulator) A, B, R0, R1, R2, R3, R4, R5, R6, R7, (data pointer) DPTR and (program counter) PC (. To understand the use of these registers, we will show them in the conditions of two simple command, MOV and ADD.

Simply state, the MOV instruction copies data from one location to another location. It has the following format:

MOV destination, source ;copy source to destination.

This command tells the (in reality, copy) CPU to move the source operand to the landing place operand.

After this command is executed, register A will have the same value as register R0. The MOV command does not affect the source operand.


Computer stuff kit tricks of Topics 46.