not sure why this doesn't compile

This is a discussion on not sure why this doesn't compile within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <cs50.h> #include <stdlib.h> int main(int argc; char *argv[]) { int i = atoi(argv[1]); if (argc != ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    not sure why this doesn't compile

    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <stdlib.h>
    
    int
    main(int argc; char *argv[])
    {
        int i = atoi(argv[1]);
        if (argc != 2 || i < 1)
        {
            printf("You must enter only 1 argument, it must be a positive integer.");
            return 1;
        }
    }
    I'm running Fedora and compiling with gcc. I get these errors when compiling:

    gcc -ggdb -std=c99 -Wall -Werror ceasar.c -lcrypt -lcs50 -lm -o ceasar
    ceasar.c:6:10: error: parameter 'argc' has just a forward declaration
    ceasar.c:6:1: error: first argument of 'main' should be 'int' [-Werror=main]
    ceasar.c:6:1: error: 'main' takes only zero or two arguments [-Werror=main]
    ceasar.c: In function 'main':
    ceasar.c:9:9: error: 'argc' undeclared (first use in this function)
    ceasar.c:9:9: note: each undeclared identifier is reported only once for each function it appears in
    cc1: all warnings being treated as errors

    make: *** [ceasar] Error 1
    Last edited by Dave Couture; 08-17-2012 at 06:25 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    Code:
    main(int argc; char *argv[])
    Check this line again very carefully.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    EDIT: OP fixed formatting, related comments redacted.

    Some things I see:

    1. You need a comma to separate function parameters, not a semicolon
    2. Not technically wrong, but you should be explicit and return 0 at the end of main for success

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Quote Originally Posted by anduril462 View Post
    EDIT: OP fixed formatting, related comments redacted.

    Some things I see:

    1. You need a comma to separate function parameters, not a semicolon
    2. Not technically wrong, but you should be explicit and return 0 at the end of main for success
    Thanks a lot, it's my first time using argc and argv so I didn't know. It seems to work fine except for one thing, if I run the program with no arguments, it gives me:

    jharvard@appliance (~/Desktop/cs50 projects): ./ceasar
    Segmentation fault (core dumped)

    And I thought that by saying "if (argc != 2)", when the user doesn't input arguments, the program would say the line "You must enter only 1 argument, it must be a positive integer." It works when I enter more than 1 argument, but not if I don't enter any arguments. Why is that?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    The check is correct, but you're using argv[1] before you do the check.

    Code:
    int
    main(int argc; char *argv[])
    {
        int i = atoi(argv[1]);   <------- !
        if (argc != 2 || i < 1)
    If you pass 2 arguments, then the atoi() call has a valid piece of data as an argument. The program will then exit anyway.
    However with no arguments, there is nothing in argv[1] -- it is NULL, which will cause the crash. You should check for the correct number of arguments before you do anything with them.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Dave Couture View Post
    And I thought that by saying "if (argc != 2)", when the user doesn't input arguments, the program would say the line "You must enter only 1 argument, it must be a positive integer." It works when I enter more than 1 argument, but not if I don't enter any arguments. Why is that?
    True, but before that you already access argv[1] and that doesn't exist in this case.
    Kurt

    EDIT: Typing slow. smokeyangel already told everything
    Last edited by ZuK; 08-18-2012 at 05:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the simple code doesn't compile?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2008, 03:28 AM
  2. exp function doesn't compile
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 09-16-2006, 02:02 PM
  3. Doesn't Compile
    By Justin C in forum C++ Programming
    Replies: 12
    Last Post: 04-29-2005, 06:40 PM
  4. Replies: 3
    Last Post: 03-07-2003, 08:06 PM

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