Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Some Special Codes

Posted by ajay on July 16, 2006

I was solving problems some time ago and came to know some good facts about c and c++  and wrote some interesting codes.

1. A C program which can print the file name it is kept in ;) .

#include<stdio.h>

main(){

printf(“the source file name is %s\n”,__FILE__);

}

actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest.

2. Usage of assignment suppression operator in scanf  – Suppose you have some crap input (the input is provided to you but that is not of any use to your program) then what normally we do is take a dummy variable and scan the input in that variable.  Example -

int dummy;

scanf(“%d”,&dummy);

but there is one more method which can save memory and time

what you do is

scanf(“%*d”);

In this method, we provide a character * to the scanf function and by doing that; scanf will scan the input from standard input but it wont assign it to any variable. This is scanned in a buffer of sufficient size and you dont have to worry about that.

3.  My source code for problem KAMIL on spoj -

x=1;main(){for(;scanf(“%*[^DFLT\n]“)+1;x=getchar()<11?printf(“%d\n”,x),1:x*2);}

If anybody has a better idea on this problem then please do comment on the post.

4. Printing something with variable width  -

Suppose you are to print some formatted output in some program and for that sometime you require to print a variable in a fix width ..

for example if you have to pring an integar in width of 5 or more then you do

printf(“%5d”, var);

now if var is 10 it will append 2 leading spaces to complete the atleast 5 width rule.

now suppose you dont want leading spaces but leading zeroes, then here is another method to write the same -

printf(“%05d”,var);

now if var is 10 it will print 00010 and so on.

Another condition comes if the width in which we want to print the variable is not known at the compile time – suppose you want to print variable with a width which is contained in another variable, then we can use -

printf(“%*d”,x,var);

here x is the width length variable. Whenever we do this * operator .. then it looks for the next integar argument provided in the arguments and then  assumes that it is the width in which  the variable is to be printed.

Now if x = 2 then it will b the same as

printf(“%2d”,var); ..

this way we can work with this width thing pretty well.

If any of you guys know some nice facts about c or cpp language, then please post them as comments. Any gud suggestions related to things I posted are also welcome. Have Fun.

16 Responses to “Some Special Codes”

  1. Bharat said

    %*d thing is really cool..

  2. Turbo said

    ya… I didn’t know abt the use of * in printf. I used a way around like this:-

    sprintf(formatStr,"%%%dd",width); //generating the format string on fly
    printf(formatStr,x); //actual printing

    Also __FILE__ sounds really cool.

  3. Turbo said

    And what is that ^DFLT in your solution to KAMIL Problem?

  4. ajay said

    @Turbo ..
    Actually many times when we want to scan something till new line character .. we do
    scanf(“%[^\n]“,str_var);
    now in that square brackets .. we can write whatever character and it will scan till only those characters ..
    suppose if you do
    scanf(“%[DFLT\n]“,var);
    it will scan till these character appear in the input .. if any of the character is eof or not belong to the set mentioned in the square brackets .. then scanf will stop scanning at the moment
    a ^ character reverses the condition exactly .. so that construct which I wrote will scan from the input till any of the character D or F or L or T or \n appears .. in this square bracket construct .. we can also write ranges like suppose you want to scan the input like this ..

    123232341671625+1694356746512645
    and scanf both the numbers in different variables .. then what you do is
    scanf(“%[0-9]%c%[0-9]\n”,num1,operation,num2);
    here num1 and num2 are strings and operation is a variable ..
    this method for scanning can b efficient as well as easy to write ..
    happy coding ..

  5. Turbo said

    ohh.. I thought DFLT is some special keyword.

  6. Roy said

    one thing about %*d how am i to access the buffer

    my id is kickstart530@yahoo.co.in

  7. ajay said

    I’m not sure about that till now .. I will let you know if I come to know about it

  8. Atul said

    a gud set of tips n tricks .. carry on

  9. Hey,
    Answer to the Hello World Problem, where while compiling the C program should as for code to be compiled:

    #include
    #include “/dev/tty”

    YES! Thats it!!!! :D Its really amazing :) heh.. its this simple yet so cool!
    Enjoy
    Sidhant

  10. Anonymous said

    #include “/dev/stdin”

  11. Anonymous said

    x=1;main(){for(;scanf(“%*[^DFLT\n]“)+1;x=getchar()<11?printf(“%d\n”,x),1:x*2);}
    can be changed to
    x=1;main(){for(;~scanf(“%*[^DFLT\n]“);x=getchar()<11?printf(“%d “,x),1:x*2);}

  12. Sudeep said

    How to print Program execution time in C ?

  13. my kamil is just 75 bytes now.. not the best though
    for i in 1…11 do
    s=gets;p=1;
    s.gsub(/[DTFL]/){|s| p=p*2}
    puts p;end

    the ones in perl are great!

  14. more better in ruby -
    10.times do
    s=gets;p=1;
    s.gsub(/[DTFL]/){|s| p*=2}
    puts p;end

  15. cool said

    hii…these things are really cool. n thanks for letting us know these….continue the good work!!

  16. [...] NOTE: source link [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>