Thread: Why is this simple c program not working ?

  1. #1
    Registered User
    Join Date
    Apr 2012

    Why is this simple c program not working ?

    So I wrote this program that asks for your details. The part where it asks for details works , but then it fails to show the details in the last bit of the program.
    It crashes .. why ?

    #include <stdio.h>
    #include <ctype.h>
    int main() {
    char name = '\0';
    char surname = '\0';
    int passport=0;
    printf("Enter name: ");
    scanf("%s", &name);
    printf("Enter surname ");
    scanf("%s", &surname);
    printf("Enter passport: ");
    scanf("%d", &passport);
    printf("Your details are: name:%s , surname:%s , passport:%d", name,surname,passport);

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    1) main returns an int
    2) you are trying to read strings into variables that can store and do store only one char.
    3) you are not calling scanf correctly, remove the & when you are reading char arrays.
    4) in fact, Don't use scanf to read in strings, because you can overflow them. Use fgets().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    so what should I write instead of int ? .. and should I declare char like char[10] ?

  4. #4
    Registered User
    Join Date
    Dec 2011
    > so what should I write instead of int ?
    Huh? No, main is supposed to return an int. You forgot to.

    > and should I declare char like char[10] ?

  5. #5
    Registered User
    Join Date
    Apr 2012
    What do you mean by returning an int ? .. sorry for my ignorance but I am new to C.

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Your main function says it returns an int (which is correct):

    int main {
    But you NEVER return an int from it.

    Literally, at the end of main, you should have something like:

    return 0;
    The others are all valid points too and the REAL cause of your problem. You're obviously NOT compiling this with warnings turned on, so you've missed so many problems before you even start.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 01:56 PM
  2. Simple program not working
    By oobootsy1 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2005, 02:20 AM
  3. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM