% things please elaborate

This is a discussion on % things please elaborate within the C Programming forums, part of the General Programming Boards category; Alright I am reading my book and I am going along fairly well untill I came to something like this ...

  1. #1
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    % things please elaborate

    Alright I am reading my book and I am going along fairly well untill I came to something like this

    printf("%c", ctr")

    now I know what printf is, and ctr is the variable that I had to make before the confusing part is the %c thingy.
    I am not sure but I think that it does something special(this program displays all of the letters of the alphabet, woo big thing here, lol)anyways if I am right(which I probably am not)does anyone have an explanation or a list that I could have that lists the different ones and their uses if you don't mind.
    Please and thanks in advance.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    %c signifies that your variable is character specific. It will be a character.

    printf("%s", "Hello World")
    The string, is Hello World.

    printf("%s", HelloWorld)
    The string is a string of text stored in the variable HelloWorld.
    The world is waiting. I must leave you now.

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The %c is a place holder, and tells printf to read the next argument into that place and print it. It performs this function in order. There are a number of place holders for different data types.
    %s for strings
    %i for ints
    So something like the following should work

    Code:
    int main()
    {
    char []mystring={"Hello"};
    int myint=15;
    printf ("My string is %s, but my int is %i",mystring,myint);
    return 0;
    }
    The arguments are taken in order, and your compiler should generate an error or warning if you try to pass a string to a %i placeholder say.

    You can also expand upon these place holders with formatting information. Look here for more info;
    http://www.cplusplus.com/ref/cstdio/printf.html
    Demonographic rhinology is not the only possible outcome, but why take the chance

  4. #4
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    OK so if I can "call" an interger or character by using those symbols like:

    int mine=10
    printf("%i, My interger is:, mine")

    would something along this line(not exactly this line)would work though??
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Not quite.

    The variable you pass needs to be outside the quote marks

    printf("My interger is: %i", mine)

    would more likely give the result you're looking for.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Alright so if I am doing something like you example what is the point, like why not just type it(unless this acts like a variable)and how does the compiler or whatever know which intergers are which and which ones to place with %s??
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I'm not sure of your question but heres a bigger example, hope this helps:

    Code:
    int main()
    {
      int money=5;
      char name[5];
      int average=15;
    
      strcpy(name,"me");
      printf("My name is %s I have %d dollars and average of %d",name,money,average);
      return 0;
    }
    ok now %d means an integer, %s means a string, the compiler will go through and see that the first variable that is out of the "" part will be a string followed by an integer, followed by another integer, etc. so whatever the order is in your string to print to the screen must be the same as what you put in the printf statement.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Here is a small program displaying what's been discussed:
    Code:
    #include <stdio.h>
    
    int Again ( char * NewLine );
    
    int main ( void )
    {
    	#define MESSAGE(x) printf("This is line 1\n%s\nThis is line 3\n\n", x);
    	MESSAGE("This is a new line");
    	MESSAGE("This is another new line");
    	Again("This is a new line again");
    	Again("This is another new line again");
    	return 0;
    }
    
    int Again ( char * NewLine )
    {
    	printf("This is line 1\n%s\nThis is line 3\n\n", NewLine);
    	return 0;
    }
    A variable, is sort of like a wildcard. It's sort of like a joker in a deck of playing cards.

    In the function Again(), NewLine is the variable. %s in the printf statement says that it will be a string. The NewLine variable at the end of the printf statement, is the "container" which holds your string of text. This can be changed. In the function title or definition of Again, you see the variable NewLine. Well, when we call Again in the main program, we can always change what would normally be line 2. In the main function there is another example by using a define statement.
    The world is waiting. I must leave you now.

  9. #9
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Alright I will keep trying so thanks.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  3. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-10-2001, 11:47 PM
  4. What is your favorite things about programming?
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-21-2001, 12:13 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM

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