Author: admin

  • C – One Dimensional Array

    What is One Dimensional Array in C?

    An array with only one subscript is known as a one-dimensional array.
    Example: int arr[10].

    Syntax:

    data-type arrayName [ arraySize ];

    Declaration of 1D Array in C:

    For declaration, the programmer needs to specify the type of element and number of elements in an array.

    Certain rules for declaring One Dimensional Array

    • The declaration must have a data type(int, float, char, double, etc.), variable name, and subscript.
    • The subscript represents the size of the array. If the size is declared as 10, programmers are allowed to store only 10 elements (index 0 to 9).
    • An array index always starts from 0 as its first element. For example, if an array variable is declared as arr[10], then it ranges from 0 to 9.
    • Each array element stored in a separate memory location.
    type arrayName [ arraySize ];

    The data-type must be a valid C data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for integer array with 10 elements:

    int arr[10];

    Initialization of 1D array in C:

    Array can be initialize by using a single statement or one by one. The Initialization is done within the curly braces {}.

    int arr[5] = {45, 23, 99, 23, 90};
    OR
    int arr[] = {45, 23, 99, 23, 90};

    On second one the array size is not declared so that you can initialize as much value as you want.

    Users can also directly assign the value to the particular element in an array by specifying the index number such as: below shows how to assign the value 23 to the 5th element of an array.

    arr[4] = 23;

    NOTE:
    In the above example number 4 means the 5th element as the array index starts from 0.

    Accessing a 1D Array Elements:

    An array can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:

    arr[4]; //the 5th element is accessed
    arr[7]; //the 8th element is accessed

    We can also assign the value particular index number to other variable but their data type must be same such as:

    int numb = arr[4];

    In the above, the value of 5th element from an array arr[] is assigned to the numb variable.

    Example of 1D Array in C.

    Declaring and assigning 5 elements in an array and display them accordingly.

    #include <stdio.h>
    
    void main()
    {
      int arr[5], i;
    
      printf("Enter 5 Elements.\n");
      for (i = 1; i <= 5; i++)
      {
        printf("Element %d = ", i);
        scanf("%d", &arr[i]);
      }
    
      printf("\nElements in an Array:\n");
      for (i = 0; i <= 5; i++)
      {
        printf("%d ", arr[i]);
      }
    }

    Output:

    Enter 5 Elements.
    Element 1 = 23
    Element 2 = 45
    Element 3 = 66
    Element 4 = 90
    Element 5 = 100
    
    Elements in an Array:
    23 45 66 90 100

  • C – Function

    A function refers to a block of code that performs a specific task. We can divide and create a separate function in a program so that each function performs a different specific task and can be called as many times as needed in the program.

    • At least one pre-defined function is always executed by the C compiler that is the main() function.
    • It allows the user to reuse the code as many times without rewriting the code and therefore act as a time saver.
    • The function is declared within the curly braces {}.

    C function is defined into two categories:

    1. Library functions
    2. User-defined functions

    1. Standard Library functions:

    These functions are built-in function in C programming that is already defined in C Library. Example: printf()scanf()strcat() etc. To use these functions an appropriate header needs to be included in the program.

    2. User-defined functions:

    On the other hand, user-defined functions are the ones that are defined by the users while writing the code and they perform the task assigned by the users.

    User-defined functions are used for code re-usability and for saving time and space.


    Defining a function:

    The general way to define a function in C:

    return_type function_name( parameter_list ) {
       body of the function;
    }

    Each part of a function:

    • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
    • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
    • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
    • Function Body: The function body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.

    Example:

    /* function that returns the minimum value
    between two number passed*/
    
    int min(int num1, int num2) 
    {
       int finalValue;
     
       if (num1 > num2)
          finalValue = num1;
       else
          finalValue = num2;
     
       return finalValue;
      //return_type is int
    }

    Declaring a Function:

    Declaration of function includes the function name and its return type and parameters (if any).

    When the function is called, the execution control passes to the function and does some specific task. When the return statement of the function is executed or when it executes the end braces, it means the program control is returned to the main function.

    The function declaration contains:

    return_type function_name( parameter list );

    In the above example the declaration is stated as:

    int min(int num1, int num2);

    Declaration of function is required when the source is on one file and you call that function from different files.

    Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required

    #include <stdio.h>
    
    //user-defined function
    int sum(int a, int b = 5)
    {
    
       ...........
       ...........
    
     
       return result;
    }
    
    //Main function
    int main()
    {
       .............
       .............
    
       // calling a function
       result = sum(a, b);
       ............
       .............
    
       return 0;
    }

    Functions Arguments

    In a function in c, information is exchanged or passed by the means of argument or parameters. A function is called with an argument or without an argument in C.

    There are two ways in which the argument can be passed through a function:

    • Call by Value.
    • Call by Reference.

    Call by Value in C.

    In this method, the actual argument is copied to the formal parameter of the function. Hence any operation performed on the argument does not affect the actual parameters.

    1. Call by Value: In this method, the actual argument is copied to the formal parameter of the function. Hence any operation performed on the argument does not affect the actual parameters.

    2. Call by Reference: Unlike call by value method, here the address of the actual parameter is passed to a formal parameter rather than a value. Hence any operation performed on formal parameters affects the value of actual parameters.

    By default, C uses call by value to pass arguments.


  • C – Call by Value and Call by Reference with Example

    There are two ways in which the argument can be passed through a function in C Programming.

    1. Call by Value
    2. Call by Reference

    Call by Value in C.

    In this method, the actual argument is copied to the formal parameter of the function. Hence any operation performed on the argument does not affect the actual parameters. By default, C programming uses call by value to pass arguments.

    Since the actual parameter is copied to formal parameters, different memory is allocated for both parameters.

    Example: Swap the values of two variables in C Programming:

    #include <stdio.h>
    
    //declaration
    void swap(int, int);
    
    int main()
    {
      int a = 100;
      int b = 500;
    
      printf("Before swapping the value of a = %d", a);
      printf("\nBefore swapping the value of b = %d\n", b);
    
      //Calling a function
      swap(a, b);
    
      printf("\nAfter swapping the value of a = %d", a);
      printf("\nAfter swapping the value of b = %d", b);
    }
    
    void swap(int a, int b)
    {
      int temp;
      temp = a;
      a = b;
      b = temp;
    
      //The values interchange here inside function
      //foraml parameters
      printf("\nAfter swapping values of a and b inside function: a = %d, b = %d\n", a, b);
    }

    The output of call by Value:

    Before swapping the value of a = 100
    Before swapping the value of b = 500
    
    After swapping values of a and b inside function: a = 500, b = 100
    
    After swapping the value of a = 100
    After swapping the value of b = 500

    Call by Reference.

    Unlike call by value method, here the address of the actual parameter is passed to a formal parameter rather than value. Hence any operation performed on formal parameters affects the value of actual parameters.

    The memory allocation for both actual and formal parameters are same in this method.

    Example: Swap the values of two variables in C Programming:

    #include <stdio.h>
    
    //declaration
    void swap(int *, int *);
    
    int main()
    {
      int a = 100;
      int b = 500;
    
      printf("Before swapping the value of a = %d", a);
      printf("\nBefore swapping the value of b = %d\n", b);
    
      //Calling a function
      swap(&a, &b);
    
      printf("\nAfter swapping the value of a = %d", a);
      printf("\nAfter swapping the value of b = %d", b);
    }
    
    void swap(int *a, int *b)
    {
      int temp;
      temp = *a;
      *a = *b;
      *b = temp;
    
      //The values interchange here inside function
      //foraml parameters
      printf("\nAfter swapping values of a and b inside function: a = %d, b = %d\n", *a, *b);
    }

    The output of call by Reference:

    Before swapping the value of a = 100
    Before swapping the value of b = 500
    
    After swapping values of a and b inside function: a = 500, b = 100
    
    After swapping the value of a = 100
    After swapping the value of b = 500

  • C – goto statement

    goto statement in c allows the user in the program to jump the execution to the labeled statement inside the function. The label (tag) is used to spot the jump statement.

    NOTE: Remember the use of goto is avoided in programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

    goto statement Flowchart:

    goto statement in C
    goto statement in C

    Syntax of goto statement:

    goto label;
    ....
    ....
    label: statement; //label to jump

    Example of goto statement in C Program.

    #include <stdio.h>
    
    void main()
    {
      int i = 1;
    
      label: do {
      if (i == 8)
      {
         //skip the iteration
          i++;
          goto label;
      }
    
      printf("Value of i: %d\n", i);
      i++;
      } while (i <= 10);
    }

    The output of continue statement.

    Value of i: 1
    Value of i: 2
    Value of i: 3
    Value of i: 4
    Value of i: 5
    Value of i: 6
    Value of i: 7
    Value of i: 9
    Value of i: 10
  • C – continue statement

    In C programming, the continue statement works like a break statement, instead of terminating the loop the continue statement skips the code in between and pass it to the next iteration in the loop.

    continue statement Flowchart:

    Continue statement in java
    continue statement in C

    Syntax of continue statement:

    continue;

    In the for loop, continue statement skips the test condition and increment value of the variable to execute again and In the while and do…while loops, continue skips all the statements and program control goes to at the end of the loop for tests condition.


    Example of continue statement in C Program.

    #include <stdio.h>
    
    int main()
    {
      int i = 0;
      while (i < 10)
      {
        i++;
    
       //the 8th iteration is skipped
        if (i == 8)
          continue;
    
        printf("Value of i: %d\n", i);
    
      }
    }

    The output of continue statement.

    Value of i: 1
    Value of i: 2
    Value of i: 3
    Value of i: 4
    Value of i: 5
    Value of i: 6
    Value of i: 7
    Value of i: 9
    Value of i: 10
  • C – break statement

    break statement in C is mostly used in a switch statement to terminate the cases present in switch statement. It terminates the loop and transfers the execution process immediately to statement following the loop.

    The use of break statement in nested loops terminates the inner loop and the control is transferred to the outer loop. If the break statement is used in nested loops (i.e., loop within another loop), the break statement will end the execution of the inner loop and Program control goes back to the outer loop.

    break statement Flowchart:

    break statement in C

    Syntax of break statement:

     break;

    Example of break statement in C Program

    #include <stdio.h>
    
    void main()
    {
       int i = 1;
       while (i < 10)
       {
         printf("Value of i before break: %d\n", i);
    
         if (i == 8)
         {
           printf("Loop terminated.\n", i);
           break;
         }
    
         i++;
       }
    }

    The output of break statement in C Programming.

    Value of i before break: 1
    Value of i before break: 2
    Value of i before break: 3
    Value of i before break: 4
    Value of i before break: 5
    Value of i before break: 6
    Value of i before break: 7
    Value of i before break: 8
    Loop terminated.
  • C- Loop control statement

    Loop control statements are also known as Jump statement. The use of jump statement changes the state of execution. This statement help in exiting the loops statement.

    C supports three control statements.

    • break statement
    • continue statement
    • goto

    break statement in C

    break statement is mostly used in a switch statement to terminate the cases present in a switch statement. It terminates the loop and transfers the execution process immediately to a statement following the loop.

    The use of break statements in nested loops terminates the inner loop and the control is transferred to the outer loop. If the break statement is used in nested loops (i.e., loop within another loop), the break statement will end the execution of the inner loop and Program control goes back to the outer loop.

    break statement Flowchart:

    break statement in C

    Syntax of break statement in C:

     break;

    Click here for example of break statement in C.


    continue statement in C

    In C programming, the continue statement works like a break statement, instead of terminating the loop the continue statement skips the code in between and passes the execution to the next iteration in the loop.

    continue statement Flowchart:

    Continue statement in java
    continue statement in C

    Syntax of continue statement in C:

    continue;

    In the case of for loop, the continue statement skips the test condition and increment the value of the variable to execute again while in the case of while and do…while loops, continue skips all the statements and program control goes to at the end of the loop for tests condition.

    Click here for example of continue statement in C.


    goto statement in C

    goto statement allows the user in the program to jump the execution to the labeled statement inside the function. The label (tag) is used to spot the jump statement.

    NOTE: Remember the use of goto is avoided in programming language because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

    goto statement Flowchart:

    goto statement in C
    goto statement in C

    Syntax of goto statement in C

    goto label;
    ....
    ....
    label: statement; //label to jump

    Click here for example of goto statement in C.


  • C – do while loop

    do-while loop in C is just as same as while loop, the only difference is that in do-while the condition is always executed after the loop as shown in the syntax below.

    Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.

    do-while Flowchart:

    do-while loop in C

    Syntax of do-while Loop.

    do
     {
      statements..
     }while (condition);

    Example of a do-while loop in C program.

    #include <stdio.h>
    
    int main()
    {
      int num = 1;
    
     	//execution of do-while loop
      do {
        printf("Value of num: %d\n", num);
        num++;
      } while (num <= 10);	//condition
    
      return 0;
    }

    The output of do-while loop.

    Value of num: 1
    Value of num: 2
    Value of num: 3
    Value of num: 4
    Value of num: 5
    Value of num: 6
    Value of num: 7
    Value of num: 8
    Value of num: 9
    Value of num: 10


  • C – while Loop

    A while loop is a straightforward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed. The loop continues until the condition stated is found to be false.

    while loop Flowchart:

    While Loop in C

    Syntax of While Loop.

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example of while loop in C Program

    #include <stdio.h>
    
    int main()
    {
     	//varibale
      int num = 1;
    
     	//execution of while loop
      while (num < 10)
      {
        printf("The value of num: %d\n", num);
        num++;
      }
    
      return 0;
    }

    The output of while loop in C programming.

    The value of num: 1
    The value of num: 2
    The value of num: 3
    The value of num: 4
    The value of num: 5
    The value of num: 6
    The value of num: 7
    The value of num: 8
    The value of num: 9


  • C – for loop

    In C programming, for loop is a more efficient loop structure and is an entry-control loop. The iteration continues until the stated condition becomes false.

    It has three computing steps as shown in the syntax below.

    • initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement: The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    for loop Flowchart:

    for loop in C

    Syntax of for loop.

     for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example of for loop in C Program.

    #include <stdio.h>
    
    int main()
    {
      int num;
    
     //execution of for loop
      for (num = 5; num <= 10; num++)
      {
        printf("The values of num: %d\n", num);
      }
    
      return 0;
    }

    The output of for loop in C programming.

    The values of num: 5
    The values of num: 6
    The values of num: 7
    The values of num: 8
    The values of num: 9
    The values of num: 10