function and array printf question

This is a discussion on function and array printf question within the C Programming forums, part of the General Programming Boards category; two questions: 1. Is it possible to print the result of a function such as if it has an enumeration ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    function and array printf question

    two questions:

    1. Is it possible to print the result of a function such as if it has an enumeration type value. Lets say I invoke function animal() and it returns to the calling environment the word " bird" : Is there a printf command to print bird like:



    Code:
        printf("%f", animal())
    or something like that?


    2. what is the printf command that will display the values of an array such as:


    Code:
        int a[8] = {1,2,3,4,5,6,7,8}


    so that it will look like this on the display:


    1 2 3 4 5 6 7 8


    I know these are probably extremely simple questions, but I'm just getting into C and any info would be great.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Hi,

    Re: your first point...

    It is my understanding that you definately good print out the return value of a function straight away... e.g. if the function getAge() returns an integer, you could use...

    Code:
       printf("Age is %d", getAge());
    Note... that your example wouldn't work... because you were using the %f specifier (which, if I remember correctly, is for displaying floats). Use %s for strings. In the case of strings however, I personally don't think it is best to return a char* and print it out immediately. Assuming your function CREATES memory for the string being returned... if you don't first assign the result to a variable as in...

    Code:
       char* name = getName();
       printf("Name is %s", name);
    then you wouldn't be able to free up the memory after you are finished with it. If you have assigned the result to a variable, you can free the memory when you are done by calling...

    Code:
       free(name);
    I hope that makes sense.

    As far as your second point is concerned...

    I believe your best bet is to loop through each of the items in the array, printing each one...

    e.g.

    Code:
       int length = 20;  /* just an example... you probably won't end up initialising your 'length' variable with a magic number. */
       for (i=0; i<length; i++)
       {
          printf("%d ", int_array[i]);
       }
    I hope that helps and at least puts you on the right track.

    Good luck in your programming.

    Eddie

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Thanks eddie,

    I think I understand what to do on the first point.

    The second point I'm still a little fuzzy on.

    If i have the declaration for the array:

    Code:
    int  a[8] = {1,2,3,4,5,6,7,8}
    and all I want to do is display the elements of the array in the first command of the program, then I should use a loop to do this.

    Is there a command that possibly prints them all out with one printf command or do they all have to be printed seperately?

    Thanks again for the help!

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    also, (sorry i forgot this), if i want to print a label to the characters so it looks like this:

    pass 1: 1 2 3 4 5 6 7 8

    then how should I print "pass 1:" so that it doesn't get printed multiple times while the loop is going?

    thanks again

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Put a printf before the loop?
    Code:
    int x, array[] = {1, 2, 3, 4, 5, 6, 7, 8};
    
    printf("pass 1:");
    for(x = 0; x < sizeof(array); x ++) {
        printf(" %i", array[x]);
    }
    putchar('\n');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    Code:
    for(x = 0; x < sizeof(array); x ++) {
    I think you meant sizeof array / sizeof *array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're right, I did. Sorry.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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