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 ;).



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;


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

what you do is


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


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 –


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 –


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.


46 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.

    • Kacy said

      Les couples hé©tÃros font des enfants par caprice ou par accident, les homos uniquement par caprice. Je ne suis pas sûr que ce soit un mal…

  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
    now in that square brackets .. we can write whatever character and it will scan till only those characters ..
    suppose if you do
    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 ..

    and scanf both the numbers in different variables .. then what you do is
    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.

    • Mellie said

      Reibellousvanilla,I call it soccer so I don't have to constantly differentiate between American football and futbol. I agree that the best thing about soccer is the lack of commercials. I abhor commercials.

    • I guess you have produced a few really fascinating points. Not as well many others would really think about this the way you just did. I am very impressed that there is so much about this subject that has been revealed and you made it so nicely, with so considerably class. Superior one, man! Really special things right here.

  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 “/dev/tty”

    YES! Thats it!!!! 😀 Its really amazing 🙂 heh.. its this simple yet so cool!

  10. Anonymous said

    #include “/dev/stdin”

  11. Anonymous said

    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.gsub(/[DTFL]/){|s| p=p*2}
    puts p;end

    the ones in perl are great!

  14. more better in ruby –
    10.times do
    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 […]

    • Julz said

      Was having a tough wee18#&2k1;feeling very weighted down with “purpose of life” issues and couldn’t shake it. You lifted the weight and helped me get some clarity–a lightness of spirit and peace of mind followed. Permission to let go and not have to figure it all out TODAY! Thank you friends

  17. Sudeep said

    Hello,I stared at couple of problems in codechef..and I am absolutely clueless how to solve them.Please give tips for a beginner.what references can we use..?what are the prerequisites..?

  18. Hi there! Do you know if they make any plugins to help
    with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Thanks!

  19. superdietadaproteina.wordpress.com said

    While the ability to lose weight all while shoveling various meats down your throat seems heavenly, a good dose of commons sense can fire some warning shots to your system.
    The conclusions were reached after a thirty two (32) week long study and would be published in the
    journal Steroids. A good Hypnotherapist for Weight Loss should have a background in Nutrition,
    Exercise, or both.

  20. The most popular program for monitoring operating temperatures is Speed
    – Fan, a program that provides information about both temperature
    and fan speed. This is a great way to find out what a particular
    group wants and is willing to buy. Allows the user to create a customized user interface in order to organize, find,
    and rate templates.

  21. Every weekend i used to pay a visit this website, because i want enjoyment, as this this web site
    conations genuinely good funny information too.

  22. This utility overwrites your data thirty-five times, scrambles
    the original file name, and truncates the file size
    to nothing before Permanent Eraser finally unlinks it from the system.
    Though it may seem like what we do online is private, there
    are actually many ways in which we lose privacy online.

    It’s no longer necessary to buy from a local video production company, when every video producer is only a webcam call away.

    • Rosa said

      But most of the top most positions people belong to this and they will wear all the time and how will you remove from th.a.e.mll you can remove is only from poor people only…all high officials are always with this thread all time…..

  23. Fascinating blog! Is your theme custom made or did you
    download it from somewhere? A design like
    yours with a few simple tweeks would really make
    my blog shine. Please let me know where you got your theme.
    Appreciate it

  24. When it comes to companies, it is more favorable to invest in
    ones that have better returns than management.
    The management of a company is subject to change more quickly than
    its economic nature. Companies that result in high returns in the market will usually stay this way for
    a while, meaning better opportunities for you.


    My favorites best stock trading software reviews

  25. hey there and thank you for your information – I have certainly picked up anything new from right here.
    I did however expertise several technical points using this site, as I experienced
    to reload the web site lots of times previous to I could get it to load properly.
    I had been wondering if your web hosting is OK? Not that I
    am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if ads
    and marketing with Adwords. Well I am adding this RSS to my email and could look out for much more of your respective interesting content.
    Ensure that you update this again soon.

  26. Thanks for sharing your thoughts. I truly appreciate your efforts and I am waiting for your next post thank you once

  27. Maple said

    If you are going for most excellent contents like me, simply visit
    this web page daily for the reason that it presents feature contents, thanks

  28. Fabulous, what a weblog it is! This webpage provides useful facts to us, keep it

  29. hey there and thank you for your info – I’ve definitely picked up anything new from right here. I did however expertise several technical points using this website, since I experienced to reload the website a lot of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Well I’m adding this RSS to my
    e-mail and can look out for much more of your respective intriguing content.
    Make sure you update this again soon.

  30. If you wish for to improve your familiarity simply keep visiting this web page and be
    updated with the most recent information posted here.

  31. Does your site have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
    I’ve got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time.

  32. Hello mates, nice post and good urging commented here, I am
    really enjoying by these.

  33. ӏts like you red my mind! You seem to know ssо much about this,
    like you wrotе the book iin it or something. I think that you
    can dο with a few ρics to dfive the message home a bit, but other than that, this is wonderful blog.
    A fantastic read. I will certainly be back.

  34. Ӏtss like you read my mind! You appear to know so much about this, like yοu wrote the book in it or something.
    I think that yoou could doo wіth a few pics tο dгive
    the message home a bit, but other than that, thhis is great blog.
    A fantastіc гead. I’ll certainlу be back.

  35. Deloras said

    I hаѵе read so many posts about the blogger lovers except this post is in
    fact a pleasant post, keep it up.

  36. I enjoy what you guys are usually up too. Such clever work and reporting!
    Keep up the very good works guys I’ve incorporated you guys to my personal blogroll.

  37. Astrid said

    Hello my family member! I want to say that this
    post is awesome, nice written and include approximately all
    significant infos. I would like to peer extra posts like this .

    • Laticia said

      Hard Hitting stuff. Having lived in Africa, during a short but genocidal moment… This is all too real and heartbreaking. Excellent writing. Thank you bad blue for bringing this and so many other great articles to my conscious. Everyone copy the adress and submit this story to drudge– more people need to connect the dots between socialism and Nazi style exiemrtnations. And to this sorry, lying POS president and the global caliphate. I feel we are indeed in the days of Revelations. Call me nequelquepart.

  38. Fantastic post however I was wanting to know if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little
    bit further. Many thanks!

  39. Theresa said

    That really capuerts the spirit of it. Thanks for posting.

  40. 천안오피

    Some Special Codes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: