Thread: C programming beginner needs help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    C programming beginner needs help

    Hello every one,

    I am just beginning to learn C and I am doing this via X Code in MAC, however some of the exercises I try to input in to the compiler doesn't always work and I don't know how to fix it as I am a beginner.

    here is the exercise or Code im trying to compile along with the error I get.

    PHP Code:
    /* EXER0205.c: Day 2 Exercise 2.5 */

    #include <stdio.h>
    #include <string.h>

    int main(void) {
        
    char buffer[256];
        
        
    printf("Enter your name and press <Enter>:\n");
        
    gets(buffer);
        
        
    printf("\nYour name has %d characters and spaces!",
               
    strlen(buffer));
        return 
    0;
    }

    [
    COLOR="Red"]the error posted by the compiler is ("format '%d' expects type 'int', but argument 2 has 'size_t"")[/COLOR] 
    I have no idea what this means of course and I am at my end googling the answer to this problem.

    please help.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't use gets().Question 12.23
    strlen() return type is size_t. unsigned integeral type.(may be unsigned int,unsigned long int,...).
    C99 format for size_t is %z.
    For C89, use printf("%lu\n",(unsigned long) strlen(s) );

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I would just cast it to int:
    Code:
    printf("\nYour name has %d characters and spaces!", 
               (int)strlen(buffer));
    It's highly unlikely that the length of the string will exceed the data limit imposed by default (int).

    It sounds like your compiler is rather more picky about pointing out potential problems than the exercises anticipated. strlen() used to return (int) in the olden days. And it was perfectly fine then, dagnabbit! Until some wisenheimer put up their hand and said "hey, why can't I have strings that are over 2 gigabytes long? my style is so cramped, whahhh wahhhh!"

    When you become more experienced, you'll thank the compiler later when it discovers hidden little bugs in your code on your behalf.
    Last edited by nonoob; 12-01-2010 at 02:32 PM.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    It works thanks guys really appreciate it a lot awesome, thank you so much.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    there is one other thing that bayint Naung pointed out about "don't use gets", because I am new to all this im a little confused, my compiler runs the program and works, but it says "Warning: this program uses gets(), which is unsafe."


    if you were to rewrite this program how would you lay it out?, I know I am asking for the answer but my online course does not answer these questions or accommodate for them, so if you could show me I would learn from it and not make the same mistake in future.

    thanks again for all your help.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I would lay it out the same, I just wouldn't use gets. I would use fgets instead. The link Bayint Naung sent contains a link to question 7.1 that shows how to use fgets in place of gets. No other changes would be needed.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    I have typed fgets on line 10 of my program instead of gets but I get a error "too few arguments to function fgets" what is needed in this line?


    9 printf("enter your name and press <enter>:\n");
    10 fgets (buffer);

    what am I missing on line 10?

    thanks again, really appreciate it.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the part where you actually pay attention and read the help provided to you.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    ok i am hopeless, I just started learning can some one throw me a bone? sorry about this but the course assumes its correct but my compiler says its wrong.


    i don't have instructors to help its all online so I am reaching out to any one to help, how do I fix this or explain it to me like im 5 years old. :P

    thanks again

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ links provided. Or just look up the function fread and see what it expects you to provide it for arguments.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read the documentation for fgets. If you don't know where to look, you could always google "C reference" and check some of those links.

    Also, for your specific issue (gets->fgets), click the link in Bayint Naung's post (highlighted red, "Question 12.23"). Read through that, and you will find a sentence that starts "See question 7.1". Click on the 7.1 and read that question and example of how to use fgets to read from stdin.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    you just need to change the gets() function to scanf(). i prefer it. can i work fine with me. try it yourself. good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 02:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM