Thread: What is wrong with that easy program ?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    29

    What is wrong with that easy program ?

    Hello,

    I would like to know where is my mistake on that program I made :

    Code:
     #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %c",argv[1]);}
        else 
            if {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w");
    fprintf(f1,"%c",argv[2]);
    fclose(f1);
    return 0;}
    when I try to compile it it says :

    Code:
    test.c:7:3: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    test.c:12:1: warning: format ‘%c’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Oop, my prior links were not the warning your were getting

    read dmh2000 answer below.

    Tim S.
    Last edited by stahta01; 06-05-2012 at 09:49 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    argv[n] is a char *, not a char. if you want to print the whole argument, use argv[n] and %s. if you want to print only the first character of the argument, use argv[n][0] and %c

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    oh right I am stupid... thanks a lot

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Thank you, this tutorial will still be usefull. (stahta01)

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    %s , s if for string right ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Well so I tryed to modify my program so that I create a file with all of the arguments and not only the first one, and once again it doesn't work. can you help me again pease ?

    Code:
    #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    int i=0;
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %s",argv[1]);}
        else 
            {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w+");
    for (i=1;i<=argc;i++){
    fprintf(f1,"%s",argv[i]);
    fclose(f1);}
    return 0;}

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    sorry I modified my post.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    oh I think I know

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    So I changed it to that :
    Code:
    #include <stdio.h>
    
    int main (int argc, char*argv[]) {
    int i=0;
        if (argc > 1) {
            printf ("there are %d elements",argc); 
            printf ("the first element is %s",argv[1]);}
        else 
            {printf("there is one element");}
    FILE*f1;
    f1 = fopen(argv[1], "w+");
    for (i=1;i<=argc;i++){
    fprintf(f1,"%s",argv[i]);}
    fclose(f1);
    return 0;}
    now I have a segmentation falt
    Last edited by dekl; 06-06-2012 at 09:55 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code:
    Code:
    #include <stdio.h>
     
    int main(int argc, char *argv[]) {
        int i = 0;
        if (argc > 1) {
            printf("there are %d elements", argc);
            printf("the first element is %s", argv[1]);
        } else {
            printf("there is one element");
        }
        FILE *f1;
        f1 = fopen(argv[1], "w+");
        for (i = 1; i <= argc; i++) {
            fprintf(f1,"%s", argv[i]);
            fclose(f1);
        }
        return 0;
    }
    Now it becomes obvious that you are calling fclose prematurely.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Code:
    #include <stdio.h>
    
      
    int main(int argc, char *argv[]) {
        int i = 0;
        if (argc > 1) {
            printf("there are %d elements", argc);
            printf("the first element is %s", argv[1]);
        } else {
            printf("there is one element");
        }
        FILE *f1;
        f1 = fopen(argv[1], "w+");
        for (i = 1; i <= argc; i++) {
            fprintf(f1,"%s", argv[i]);
            }
        fclose(f1);
        
        return 0;
    }
    thanks, I need to work on that. Now this has a segmentation falt.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    argv[argc] does not exist, yet you are printing it at the end of your loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2009, 01:48 PM
  2. Help me ! easy c program
    By DINORS in forum C Programming
    Replies: 21
    Last Post: 06-27-2008, 10:59 PM
  3. Help with very easy program
    By plshelpme in forum C Programming
    Replies: 9
    Last Post: 11-02-2005, 12:11 PM
  4. Tell me a easy program to make PLEASE!! PLEASE PLEASE!!!!
    By oobootsy1 in forum C++ Programming
    Replies: 49
    Last Post: 08-11-2003, 11:30 PM
  5. Easy Program.....won't compile Help!!!!!!!!!!!!
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2001, 08:08 PM