Thread: structure in c example

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    structure in c example

    I tried to write program after some reading about structure but I am stuck. how to make proficient program for structure

    Code:
    #include<stdio.h>
    
    int maain (void)
     { 
        // Does we declear structure like this ? //
        
        struct account 
         {
           int profile_number;
           int mobile_number;
           char first_letter_name;
           char last_letter_name;      
         };
     
        // How to inilizelize structure ? //
        
        struct account profile ;
    
           profile.profile_number = 123;
           profile.mobile_number = 456123;
           profile.first_letter_name = 'A';
           profile.last_letter_name = 's';
           
           printf (" \n Print profile number %d ", profile.profile_number); 
           printf (" \n Print mobile_number %d ", profile.mobile_number); 
           printf (" \n Print first letter of name %c ",profile.first_letter_name); 
           printf (" \n Print profile number %c ",profile.last_letter_name); 
        
        return 0;
     }
    What's wrong in program ?

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    It actually looks pretty good, but you have no main function
    maaaaaain typo.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by jack jordan View Post
    It actually looks pretty good, but you have no main function
    maaaaaain typo.
    your are correct. I have done silly mistake

    I have included typedef what's wrong in this program. I want to know the use of typedef
    Code:
    #include<stdio.h>
    
    int main (void)
     { 
    
        
        typedef struct account 
         {
           int profile_number;
           int mobile_number;
           char first_letter_name;
           char last_letter_name;      
         };
     
    
        
        struct account profile ;
    
           profile.profile_number = 123;
           profile.mobile_number = 456123;
           profile.first_letter_name = 'A';
           profile.last_letter_name = 's';
           
           printf (" \n Print profile nmber %d ", profile.profile_number); 
           printf (" \n Print mobile_number %d ", profile.mobile_number); 
           printf (" \n Print first letter of name %c ",profile.first_letter_name); 
           printf (" \n Print profile nmber %c ",profile.last_letter_name); 
        
        return 0;
     }
    Last edited by abhi143; 10-30-2017 at 01:04 AM.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    I refactored your code, here is the result:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct account 
        {
            int profile_number;
            int mobile_number;
            char first_letter_name;
            char last_letter_name;      
        };
    
    int main (void)
     { 
        struct account profile=
        {
            .profile_number = 123,
            .mobile_number = 456123,
            .first_letter_name = 'A',
            .last_letter_name = 's'
            
        };
        
        printf (" \n Print profile nmber %d ", profile.profile_number); 
        printf (" \n Print mobile_number %d ", profile.mobile_number); 
        printf (" \n Print first letter of name %c ",profile.first_letter_name); 
        printf (" \n Print profile nmber %c ",profile.last_letter_name); 
        
        return EXIT_SUCCESS;
     }

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Definitions of struct and typedef should go in global scope.
    You miss the name of your typedef.
    Code:
    // declare a struct and typedef in two steps
    struct account_s
    {
        int profile_number;
        int mobile_number;
        char first_letter_name;
        char last_letter_name;
    };
    
    typedef struct account_s account;
    
    // declare a named struct and typedef in one step
    typedef struct account_s // <-- name of struct
    {
        int profile_number;
        int mobile_number;
        char first_letter_name;
        char last_letter_name;
    } account; // <-- name of type
    In the second case, you can omit the name of struct. Then you can use only the typedef.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    129
    Thanks @WoodSTokk , @ordak

    what is difference between structure and typedef. How to decide what to use structure or typedef ?
    Code:
    #include<stdio.h>
     
    int main (void)
     { 
     
        typedef struct account_s 
        {
           int profile_number;
           int mobile_number;
           char first_letter_name;
           char last_letter_name;
        } account; 
      
     
         
        struct account_s profile ;
     
           profile.profile_number = 123;
           profile.mobile_number = 456123;
           profile.first_letter_name = 'A';
           profile.last_letter_name = 's';
            
           printf (" \n Print profile nmber %d ", profile.profile_number); 
           printf (" \n Print mobile_number %d ", profile.mobile_number); 
           printf (" \n Print first letter of name %c ",profile.first_letter_name); 
           printf (" \n Print profile nmber %c ",profile.last_letter_name); 
         
        return 0;
     }
    Result

    Print profile nmber 123
    Print mobile_number 456123
    Print first letter of name A
    Print profile nmber s

  7. #7
    Banned
    Join Date
    Oct 2017
    Posts
    1
    for complete tutorial for structures in c language Watch Now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  3. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM

Tags for this Thread