A question of differences: C & C++

This is a discussion on A question of differences: C & C++ within the C Programming forums, part of the General Programming Boards category; Hey Folks, I've been into MUDing off and on now for years and I've begun to learn C in my ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Wink A question of differences: C & C++

    Hey Folks,

    I've been into MUDing off and on now for years and I've begun to learn C in my spare time so I can eventually get under the hood and implement some of my own ideas. I've got some good C/C++ resources and I've been doing the exercises. However, I've yet to receive an articulate explanation of the difference between C and C++. I've read that all C code can be compiled in C++ but not vice versa. I also understand that the focus on OOP in C++ is a main difference.

    Anyway, I'll get to the point: I've spent most of the day curled up by the fire, reading and doing exercises and I just completed a basic program from a C course that was designed to teach nested structures and passing variables between functions. I was pretty happy when I got it working but then I noticed that my compiler (Dev-C++ 4.9.9.2) uses the .cpp file extension by default when creating a new source file. I had accidentally written a C++ program when I was supposed to be learning C! I then tried to compile the source as a C program and I got a number of compiler warnings and errors. I will post the code below along with the errors in the hope that someone can tell me what's wrong and how to turn this working C++ program into a C program. Cheers.

    Code:
    #include <stdio.h>
    
    struct address_tree
    {
           char street[100];
           char city[25];
           char post [25];
           char state [25];
           char country[25];
    };
    
    struct top_tree
    {
           char name[100];
           char email[50];
           char birth[10];
           char phone[15];
           address_tree add;
    };
    
    typedef struct top_tree entry;
    
    entry input ()
    {
          entry  en1;
    
          printf ("Enter a new name.\n\n");
          scanf("&#37;[^\n]", en1.name);
          fflush(stdin);
          printf ("\nEnter a street address.\n\n");
          scanf("%[^\n]", en1.add.street);
          fflush(stdin);
          printf ("\nEnter a city of residence.\n\n");
          scanf("%[^\n]", en1.add.city);
          fflush(stdin);
          printf ("\nEnter a province.\n\n");
          scanf("%[^\n]", en1.add.state);
          fflush(stdin);
          printf ("\nEnter a post code.\n\n");
          scanf("%[^\n]", en1.add.post);
          fflush(stdin);
          printf ("\nEnter a country.\n\n");
          scanf("%[^\n]", en1.add.country);
          fflush(stdin);
          printf ("\nEnter a birthdate in the form dd/mm/yy.\n\n");
          scanf("%[^\n]", en1.birth);
          fflush(stdin);
          printf ("\nEnter a phone number.\n\n");
          scanf("%[^\n]", en1.phone);
          fflush(stdin);
          printf ("\nEnter an email address.\n\n");
          scanf("%[^\n]", en1.email);
          fflush(stdin);
          
          return en1;
    }
    
    void output (entry en1)
    {     
          printf ("\nDatabase Entry\n\n");
          printf ("Name: %s\n\n", en1.name);
          printf ("Address:\n%s\n%s\n%s\n%s\n%s\n\n", 
          en1.add.street, en1.add.city, en1.add.state, en1.add.post, en1.add.country);
          printf ("Phone: %s\n", en1.phone);
          printf ("Email: %s\n", en1.email);
    }
    
    
    main ()
    {
         entry en1;
         
         en1 = input ();
         
         output (entry en1);
         getchar ();   
    }
    When compiled as a .c file this code generated the following errors in Dev-C++4.9.9.2:

    \address practice prog.c:24: error: syntax error before "address_tree"
    \address practice prog.c:24: warning: no semicolon at end of struct or union
    \address practice prog.c:30: error: return type is an incomplete type
    \address practice prog.c: In function `input':
    \address practice prog.c:31: error: storage size of 'en1' isn't known
    \address practice prog.c:61: warning: `return' with a value, in function returning void
    \address practice prog.c: At top level:
    \address practice prog.c:65: error: parameter `en1' has incomplete type
    \address practice prog.c: In function `main':
    \address practice prog.c:77: error: storage size of 'en1' isn't known
    \address practice prog.c:81: error: syntax error before "entry"

    Execution terminated
    Last edited by SiliconHobo; 11-08-2007 at 09:32 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because the C++ language does "typedef struct name name" automagically, whilst in C you have to either perform such a typedef line, or use "struct name" instead of "name". [This applies to unions and enums and classes - the last one obviously not valid in C].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Thanks matsp for the fastest reply ever. I added another typedef line and removed the type declaration in the call to output and it works fine. Cheers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Read the FAQ on why fflush(stdin) is bad.

    For C and C++ differences.
    http://david.tribble.com/text/cdiffs.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And this
    Code:
         scanf("%[^\n]", en1.name);
    is a bad way to write
    Code:
    fgets(en1.name, sizeof(en1.name), stdin)
    Most programmers will at least have to think a bit about what the scanf() line does, and many would also have to look that up in a book - the latter is not only better because it does the same thing in a more readable way, but it's also SAFE for overentering data. [You want to wrap it into another function, as you will need to remove a newline from the end tho']

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21