November 2019

Variables in C Programmings:-

"Variable in C" variables names are case sensitive whose value can change any time. It is a memory location used to store data value. A variable name in C variables should be carefully chosen so that its use is reflected in a useful way in the entire program. 


Rules for declaring variables in C Programming:-

  1. They must always begin with a letter, although some systems permit underscore as the first character.
  2. The length of a variable must not be more than 8 characters.
  3. White space is not allowed and
  4. A variable should not be a Keyword
  5. It should not contain any special characters. 
In C variables A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program. A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use the keyword ex-tern to declare a variable at any place. Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.

  • A variable is a value that can change any time
  • It is a memory location used to store a data value
  • A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program.
  • Variable names are case sensitive. 

Examples of valid variable names are:
                a              A
                Sun        SUN       sUN       
                number               
                Emp_name        
                average1
                _Salary

Examples of Invalid Variable names are:
                123
                (area)
                6th
                %abc

    // Variable declaration:

    extern int a, b;


    extern int c;


    extern float f;


    int main () {


       /* variable definition: */


       int a, b;


       int c;


       float f;


       /* actual initialization */


       a = 10;


       b = 20;


       c = a + b;


       printf("value of c : %d \n", c);


       f = 70.0/3.0;


       printf("value of f : %f \n", f);


       return 0;


    }


    When the above code is compiled and executed, it produces the following result −

    • value of c : 30
    • value of f : 23.333334


    Sr.No.
    Type & Description
    1
    char
    Typically a single octet(one byte). This is an integer type.
    2
    int
    The most natural size of integer for the machine.
    3
    float
    A single-precision floating point value.
    4
    double
    A double-precision floating point value.
    5
    void
    Represents the absence of type.



    C Program To Accept The String That Ends With 01

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    int main()
    {
        char s[100];
        int i = 0, q = 0, l;
        printf("Enter stringover {0,1}:\n");
           scanf("%s",s);
        l = strlen(s);
        for (i = 0; s[i] != '\0'; i++)
        {
            if (s[i] == '0' && q == 0)
            {
                if (i == (l - 2))
                    q = 1;
                else
                    q = 0;
            }
            else if (s[i] == '1' && q == 0)
                q = 0;
            else if (s[i] == '1' && q == 1)
                q = 2;
        }
        if (q == 2)
        {
            printf("Accepted.\n\n");
        }
        else
            printf("Rejected.");
        return 0;
    }

    OUTPUT #1

    C Program To Accept The String That Ends With 01

    OUTPUT #2

    C Program To Accept The String That Ends With 01





    Constants in C Programming: -

           Constants in C refers to fixed valued that do not change during the execution of a
    program. const is the keyword and its used to declare constants.

    A constant value is the one which does not change during the execution of a program.
    C supports several types of constants.
    ·        Integer Constants
    ·        Real Constants
    ·        Single Character Constants
    ·        String Constants

    A constant value is the one which does not change during the execution of a program.  C supports several types of constants.     
    • Integer Constants   
    • Real Constants   
    • Single Character Constants   
    • String Constants     


    Integer Constants 

    An integer constant is a sequence of digits. There are 3 types of integer's namely decimal integer, octal integers and hexadecimal integer.     

    Decimal Integers: 
    Consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas and non-digit characters are not permitted between digits. Examples for valid decimal integer constant are:      
    • 123   
    • -31   
    • 0   
    • 562321  
    • + 78    

    Some examples for invalid integer constants are:    
    • 15 750   
    • 20,000   
    • Rs. 1000     


    Octal Integers: 

    Constant consists of any combination of digits from 0 through 7 with an O at the beginning. Some examples of octal integers are:    
    • O26   
    • O   
    • O347  
    • O676     


    Hexadecimal integer:
    Constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refer to 10 to 15 in decimal digits. Examples of valid hexadecimal integers are:       
    • OX2  
    • OX8C  
    • OXbcd   
    • Ox     


    Real Constants     

    Real Constants consists of a fractional part in their representation. Integer constants are inadequate to    represent quantities that vary continuously. These quantities are represented by numbers containing    fractional parts like 26.082. Examples of real constants are:    
    0.0026    -0.97    435.29    +487.0   

    Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign.     
    A Single Character constant represent a single character which is enclosed in a pair of quotation    symbols.   

    Example for character constants are:    

    '5'    'x'    ';'    ' '    

    All character constants have an equivalent integer value which is called ASCII Values.     

    String Constants     

    A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be an alphabet, number, special character and blank space. Example of string constants are   
    "1234"   
    "God Bless"   
    "!.....?"     

    Backslash Character Constants [Escape Sequences]     
    Backslash character constants are special characters used in output functions.  Although they contain    two characters they represent only one character. Given below is the table of escape sequence and them meanings.                
    Constant                 Meaning               
    '\a'                         Audible Alert (Bell)              
    '\b'                         Backspace               
    '\f'                          Form feed              
    '\n'                         New Line              
    '\r'                         Carriage Return              
    '\t'                         Horizontal tab             
    '\v'                         Vertical Tab             
    '\''                          Single Quote             
    '\"'                         Double Quote              
    '\?'                         Question Mark              
    '\\'                         Back Slash             
    '\0'                         Null  


    C Program To Accept The String Starting With 01

    #include <stdio.h>
    #include <string.h>
    int main()
    {
        char str[10];
        int len, i, q = 0;
        printf("enter the string which only contain 0 and 1\n");
        scanf("%s", str);
        len = strlen(str);
        for (i = 0; i <= len; i++)
        {
            if (str[i] == '0' && q == 0)
                q = 1;
            else if (str[i] == '1' && q == 0)
                break;
            else if (str[i] == '1' && q == 1)
                q = 2;
            else if (str[i] == '0' && q == 1)
                break;
            else if (str[i] == '0' && q == 2)

                q = 2;

            else if (str[i] == '1' && q == 2)
                q = 2;
        }
        if (q == 2)
            printf("Given string is accepted\n");
        else
            printf("Given string is not accepted\n");
    }

    OUTPUT#1

    C Program To Accept The String Starting With 01

    OUTPUT#2


    C Program To Accept The String Starting With 01

    Related Posts





    C Program For Bubble Sort by Generating Random Numbers and Using time.h Function


    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    void bubble_sort(int[],int);
    int main()
    {
        int list[100],n,i;
        time_t t;
         printf("Enter the max number \n");
         scanf("%d",&n);
         srand((unsigned) time(&t));
         for(i=0;i<n;i++)
         {
             list[i]=rand()%100;
         }
         for(i=0;i<n;i++)
         {
             printf("%d\t",list[i]);
         }
        bubble_sort(list,n);
        printf("\n\nTime taken to complete the bubblesort %u\n",clock()/CLOCKS_PER_SEC);
        printf("\nThe sorted list is:");
        for(i=0;i<n;i++)
            printf(" %d\t",list[i]);
        return 0;
    }
    void bubble_sort(int list[],int n)
    {
        int temp,i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n-i-1;j++)
            {
                if(list[j]>list[j+1])
                    if(list[j]>list[j+1])
                    {
                        temp=list[j];
                        list[j]=list[j+1];
                        list[j+1]=temp;
                    }
            }
        }
    }

    Output

    C Program For Bubble Sort by Generating Random Numbers and Using time.h Function

    C Program To Accept The String Having Length At Least Two

    #include <stdio.h>
    #include <string.h>
    int main()
    {
        char str[10];
        int len, i, q = 0;
        printf("Enter the string which only contain a and b\n");
        scanf("%s", str);
        len = strlen(str);
        for (i = 0; i <= len; i++)
        {
            if (str[i] == 'a' && q == 0)
                q = 1;
            else if (str[i] == 'b' && q == 0)
                q = 1;
            else if (str[i] == 'a' && q == 1)
                q = 2;
            else if (str[i] == 'b' && q == 1)
                q = 2;
            else if (str[i] == 'a' && q == 2)
                q = 3;
            else if (str[i] == 'b' && q == 2)
                q = 3;
            else if (str[i] == 'a' && q == 3)
                q = 3;
            else if (str[i] == 'b' && q == 3)
                q = 3;
        }
        if (q == 0 || q == 1 || q == 2)
            printf("Given string is accepted\n");
        else
            printf("Given string is not accepted\n");
    }

    OUTPUT #1


    OUTPUT #2


    OUTPUT #3



    Bubble Sort


    1. Bubble sort is a very simple sorting technique. However, this sorting algorithm is not efficient in comparison to other sorting algorithms.
    2. The basic idea underlying the bubble sort is to pass through the file sequentially several times. Each pass consists of comparing each element in the file with its successor (x[i] with x[i+1]) and interchanging the two elements if they are not in proper order.
    3. Example: Consider the following file,
    25          57          48          37          12          92          86          33
    In first pass, following comparisons are made:
    x[o] with x[1] (25 with 57) No interchange
    x[1] with x[2] (57 with 48) Interchange
    x[2] with x[3] (57 with 37) Interchange
    x[3] with x[4] (57 with 12) Interchange
    x[4] with x[5] (57 with 92) No interchange
    x[5] with x[6] (92 with 86) Interchange
    x[6] with x[7] (92 with 33) Interchange

    Thus, after the first pass, the file is on the order:
    25          48          37          12          57          86          33          92

    After first pass, the largest element (in this case 92) gets into its proper position within the array.
    In general, x[n-i] is in its proper position after iteration i. The method is thus called bubble sort because each number slowly "bubbles” up to its proper position after each iteration.
    Now after the second pass the file is:
    25          37          12          48          57          33            86         92
    Thus, after second pass, 86 has now found its way to the second highest position.
    Since each iteration or pass places a new element into its proper position, a file of n elements requires no more than n-1 iterations.
    The complete set of iterations is the following:
    Original file:  25                57         48           37          12          92         86          33
    Iteration 1:    25                48          37          12          57          86          33          92
    Iteration 2:    25                37          12          48          57          33          86          92
    Iteration 3:    25                12          37          48          33          57          86          92
    Iteration 4:    12                25          37          33          48          57          86          92
    Iteration 5:    12                25          33          37          48          57          86          92
    Iteration 6:    12                25          33          37          48          57          86          92
    Iteration 7:    12                25          33          37          48          57          86          92

    Algorithm for Bubble Sort

    This algorithm sorts the array list with n elements
    Step1:    Initialization,
    Set i=0
    Step2:    Repeat steps 3 to 6 until i<n
    Step3:    Set j=0
    Step4:    Repeat step 5 until j<n-i-1
    Step5:    If list[j]>list[j+1]
    Set temp = list[j]
    Set list[j] = list[j+1]
    Set list[j+1] = temp
    j=j+1
    End if
    Step6:    i=i+1
    Step7:    Exit


    Efficiency of Bubble Sort

    Sorting algorithms are analyzed in terms of the number of comparisons required fie. the major operation).
    In bubble sort, the first pass requires (n-1) comparisons to fix the highest element to its location, the second pass requires (n-2)
    comparisons, ..., kth pass requires (n-k) comparisons and the last pass requires only one comparison to be fixed at its proper position.
    Therefore total number of comparisons:
    f(n) = (n-1) + (n-2) +…+ (n-k) + … +3 + 2 + 1 = (n-1)*n/2
            <1 *n^2
    Thus ,f(n) = O(n^2) with g(n)=n^2 and C=1 whenever n>1.
    In case of bubble sort,
    Worst case complexity = Best case complexity = Average case
    complexity = O(n^2) because the comparisons will always take place.

    C Program For Bubble Sort

    #include<stdio.h>

    void bubble_sort(int[],int);

    int main()

    {

        int list[100],n,i;

        printf("\nHow many elements in the array:");

        scanf("%d",&n);

        printf("\nEnter %d values to sort:",n);

        for(i=0;i<n;i++)

            scanf("%d",&list[i]);

        bubble_sort(list,n);

        printf("\nThe sorted list is:");

        for(i=0;i<n;i++)

            printf(" %d\t",list[i]);

        return 0;

    }

    void bubble_sort(int list[],int n)

    {

        int temp,i,j;

        for(i=0;i<n;i++)

        {

            for(j=0;j<n-i-1;j++)

            {

                if(list[j]>list[j+1])

                    if(list[j]>list[j+1])

                    {

                        temp=list[j];

                        list[j]=list[j+1];

                        list[j+1]=temp;

                    }

            }

        }

    }

    Related Posts

    Insertion Sort

    Merge Sort

    Selection Sort

    Quick Sort

    C Program For Selection Sort | C Programming

    C Program For Quick Sort | C Programming

    C Program For Merge Sort | C Programming

    C Program For Bubble Sort | C Programming

    C Program for Insertion Sort | C Programming