Monday 8 April 2019

CP : Structure with example


C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.
  • If you want to access structure members in C, the structure variable should be declared.
  • Many structure variables can be declared for the same structure and memory will be allocated for each separately.
  • It is a best practice to initialize a structure to null while declaring if we don’t assign any values to structure members.

DIFFERENCE BETWEEN C VARIABLE, C ARRAY, AND C STRUCTURE:

  • A normal C variable can hold only one data of one data type at a time.
  • An array can hold a group of data of the same data type.
  • A structure can hold a group of data of different data types and Data types can be int, char, float, double and long double, etc.
Example : 

#include <stdio.h>
struct student
{
    char name[50];
    int roll;
    float marks;
} s;

int main()
{
    printf("Enter information:\n");

    printf("Enter name: ");
    scanf("%s", &s.name);

    printf("Enter roll number: ");
    scanf("%d", &s.roll);

    printf("Enter marks: ");
    scanf("%f", &s.marks);


    printf("Displaying Information:\n");

    printf("Name: ");
    puts(s.name);

    printf("Roll number: %d\n",s.roll);

    printf("Marks: %.1f\n", s.marks);

    return 0;
}

Output : 




No comments:

Post a Comment

LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project

  LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project Dear All We will learn how to Connec...