i need some help here,

This is a discussion on i need some help here, within the C Programming forums, part of the General Programming Boards category; Hey all, I have just started taking my first programming class , intro to the Language C. We are writing ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Smile i need some help here,

    Hey all, I have just started taking my first programming class , intro to the Language C. We are writing a program on the quadratic equation. I am little confused on the whole placeholder thing. I dont understand when to use them, how to use them and so on. If someone could lend a helping hand it would be greatly appreciated! thanks alot...

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm afraid that the term 'placeholder' isn't one that I am familiar with. Could you perchance provide an example of what a placeholder looks like in code?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    actually, I have no idea what they are either. These are the examples the instructor gave in class.
    for integers use %d
    for real #'s use %lf
    for characters use %c
    example of one of his programs:
    {
    int price, c , q , d , n , p ;
    FILE*inp;
    inp=fopen("prog.dat","r");
    fscanf(inp,"%d",&price);

    in the fscanf, i dont understand what the %d is for, how do you know when to use those??
    Forgive me if I wrote this incorrect..I have no clue on how to do this stuff. Thanks so much for your reply!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ...
    Oh. The placeholders basically tell scanf how to interpret the information it recieves from inp. Suppose you're running fscanf on the following input...

    1234.567

    fscanf (inp, "%c", &aChar);
    would read the first ASCII character, '1'

    fscanf (inp, "%d", &anInt);
    would read the first int, which is 1234

    fscanf (inp, "%f", &aFloat);
    would read the first float (real #), which is 1234.567

    So suppose I had to read a file that had a person's initials (HJ), his age (26), and how much cash he has in his wallet ($33.49). This is what the file would look like:
    person.dat:
    HJ 26 33.49

    And this is what the code would look like:
    Code:
    main()
    { 
     char First, Last; // first and last initials
     int age;
     float cash;
    
     FILE*inp; 
     inp=fopen("person.dat","r"); 
     fscanf(inp,"%c", &First); // First is a char, so I use %c
     fscanf(inp,"%c", &Last); // Last is a char, so I use %c
     fscanf(inp,"%d", &age); // age is an int, so I use %d
     fscanf(inp,"%f", &cash); // cash is a float, so I use %f
    }
    Or I could just use one big fscanf command instead of the 4 little ones...
    Code:
    fscanf(inp,"%c%c%d%f", &First, &Last, &age, &cash);
    Basically, fscanf isn't clever enough to recognize what kind of variable it's being given, so you have to tell it what kind of variable it's being given, using a placeholder.
    And of course, make sure that the variable you tell it to write to is the same type as the placeholder specifies.

    As a bit of trivia, the technocal term term for a placeholder is "conversion specifier", albeit a term that doesn't come up very much.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    Thank you so much for your help! It is alot clearer to me now, you should consider teaching.....you are very good at it! As of now, I think I understand this stuff...but its really tricky to me. Again, you were a tremendous help , thank you !

Popular pages Recent additions subscribe to a feed

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