Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Tuesday, 11 October 2016

Arrays in C Programming part 11 a

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
               Declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declare an array

Declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array
 This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double

Initialize an array

Initialize an array in C either one by one or using a single statement
 The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

Accessed by indexing

Accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.
     

Variable Scope in C Programming

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed.


  • Inside a function or a block which is called local variables,
  • Outside of all functions which is called global variables.
  • In the definition of function parameters which are called formal parameters.

Local Variable 

Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.

Global variables



Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

Formal parameters

Formal parameters are treated as local variables with-in a function and they take precedence over global variables.

 

 initialized  Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them.

Tuesday, 4 October 2016

Function Types in C Programming

                  A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
                 You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

Defining Function 

Here are all the parts of a function:

 Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
 Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
Function Body: The function body contains a collection of statements that define what the function does. 



Function Declaration

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
A function declaration has the following parts:


For the above defined function max(),the function declaration is as follows:


Parameter names are not important in function declaration, only their type is required, so the following is also a valid declaration:

 Call a Function

When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

Call by Value

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
                 C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. Consider the function swap(),

 Call by Reference

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly, you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
 


Saturday, 1 October 2016

Loop Control Statements in C Programming

Loop Control Statements  

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

The break statement in C programming has the following two usages:

  • When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement.

If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax
 Flow chart
 Example
Continue Statement

The continue statement in C programming works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place,skipping any code in between.

For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests.

Syntax 
 Flow chart

Goto statements:-

A goto statement in C programming provides an unconditional jump from the ‘goto’ to a labeled statement in the same function.

Syntax

Infinite loop

A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the ‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty. 
 

Saturday, 24 September 2016

Loops Types in C Programming

A loop statement allows us to execute a statement or group of statements multiple times.
C programming language provides the following types of loops to handle looping requirements.
  •  while Loop

A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is:



  • FOR LOOP

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is:


  •  do…while Loop

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

Syntax
The syntax of a do...while loop in C programming language is:



  •  

  •  Nested Loops

C programming allows to use one loop inside another loop. The following section shows a few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement in C is as follows:

 The syntax for a nested while loop statement in C programming language is as follows:

           The syntax for a nested do...while loop statement in C programming language is as follows: