Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Array and Structures in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 553
    Comment on it

    ARRAYS

    1). An array is a collection of homogeneous elements.
    2. An array is a derived data type.
    3) Any array is like a built-in data types. All we have to do is to declare an array variable so that we can use it.

    Example of Array to find the sum marks of n students

    #include <stdio.h>
    int main(){
         int marks[10],i,n,sum=0;
         printf("Enter number of students: ");
         scanf("%d",&n);
         for(i=0;i<n;++i){
              printf("Enter marks of student%d: ",i+1);
              scanf("%d",&marks[i]);
              sum+=marks[i];
         }
         printf("Sum= %d",sum);
    return 0;
    }
    

    Structures
    1) An array is a collection of hetrogeneous elements.
    2) A structure is a user-defined data type.
    3) In the case of structure, first we have to design and declare a data structure and then we declare the variable of that type.
    Example of Structure which stores record of a student for e.g. name,roll no and marks

    #include <stdio.h>
    struct student{
        char name[50];
        int roll;
        float marks;
    };
    int main(){
        struct student s;
        printf("Enter information of students:\n\n");
        printf("Enter name: ");
        scanf("%s",s.name);
        printf("Enter roll number: ");
        scanf("%d",&s.roll);
        printf("Enter marks: ");
        scanf("%f",&s.marks);
        printf("\nDisplaying Information\n");
        printf("Name: %s\n",s.name);
        printf("Roll: %d\n",s.roll);
        printf("Marks: %.2f\n",s.marks);
        return 0;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: