Posts

Showing posts from April, 2018

for loop in C

Image
for loop in C 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 − for ( init; condition; increment ) { statement(s); } Here is the flow of control in a 'for' loop − The  init  step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the  condition  is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop. After the body of the 'for' loop executes, the flow of control jumps back up to the  increment  statement. This statement allows you to update any loop control variables. This statement can be left blank,

goto statement in C

Image
goto statement in C A  goto  statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. NOTE  − Use of  goto  statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them. Syntax The syntax for a  goto  statement in C is as follows − goto label; .. . label: statement; Here  label  can be any plain text except C keyword and it can be set anywhere in the C program above or below to  goto statement. Flow Diagram Example #include <stdio.h> int main () { /* local variable definition */ int a = 10 ; /* do loop execution */ LOOP : do { if ( a == 15 ) { /* skip the iteration */ a = a + 1 ; goto LOOP ; } printf ( "value of

C - Arrays

Image
C - Arrays 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. Instead of 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. Declaring Arrays To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − type arrayName [ arraySize ]; This is called a  single-dimensional  array. The  arraySize  must be an integer constant greater th

Multi-dimensional Arrays in C

Image
Multi-dimensional Arrays in C C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − type name[size1][size2]...[sizeN]; For example, the following declaration creates a three dimensional integer array − int threedim[5][10][4]; Two-dimensional Arrays The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows − type arrayName [ x ][ y ]; Where  type  can be any valid C data type and  arrayName  will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array  a , which contains three rows and four columns can be shown as follows − Thus, every element in the array  a  is identified by an element name of the form  a[ i ]