Interesting C Programming Tricks You didn’t know before | Exam-Lib
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice
Welcome to our Education website, plz like our page facebook to support us. Thank You and wish you good navigation

Interesting C Programming Tricks You didn’t know before

abdelouafiNov 21, 2017

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      632
      Likes Received:
      13
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      upload_2017-11-21_17-57-23.png
      Interesting C Programming Tricks
      1. Using the return value of “scanf()” to check for end of file:
      Code:
      while(~scanf(“%d”,&n)) {
      /* Your solution */
      }
      Very useful at online judges where inputs are terminated by EOF.

      2. “%m” when used within printf() prints “Success”:
      Code:
      printf(“%m”);
      upload_2017-11-21_17-59-30.png
      3. Implicit initialization of integer variables with 0 and 1.
      Code:
      main(i){
      printf(“g = %d, i = %d \n”,g,i);
      }
      4. brk(0); can be used as an alternative for return 0;:
      5. Print Numbers 1 to 200 wihout using any loop, goto, recursion
      Code:
      #include<stdio.h>
      #define STEP1 step();
      #define STEP2 STEP1 STEP1
      #define STEP4 STEP2 STEP2
      #define STEP8 STEP4 STEP4
      #define STEP16 STEP8 STEP8
      #define STEP32 STEP16 STEP16
      #define STEP64 STEP32 STEP32
      #define STEP128 STEP64 STEP64
      #define STEP256 STEP128 STEP128
      
      int n = 0;
      
      int step()
      {
      if (++n <= 200)
      printf(“%d\n”, n);
      }
      
      int main()
      {
      STEP256;
      return 1;
      }
      7. C++ Sucks?
      Code:
      #include <stdio.h>
      double m[]= {7709179928849219.0, 771};
      int main()
      {
      m[1]–?m[0]*=2,main():printf((char*)m);
      }
      You will be amazed to see the output: C++Sucks

      8. Do you know “GOES TO-->” operator in C?
      upload_2017-11-21_18-1-50.png
      Code:
      int _tmain(){
      int x = 10;
      while( x –> 0 ) // x goes to 0
      {
      printf(“%d “, x);
      }
      printf(“\n”);
      }
      Output:
      9 8 7 6 5 4 3 2 1 0
      Press any key to continue . . .

      9. Scanf Magic
      Code:
      scanf(“%[^,]”, a); // This doesn’t scrap the comma
      
      scanf(“%[^,],”,a); // This one scraps the comma
      
      scanf(“%[^\n]\n”, a); // It will read until you meet ‘\n’, then trashes the ‘\n’
      
      scanf(“%*s %s”, last_name); // last_name is a variable
      10. Add numbers without ‘+’ operator
      Code:
      int Add(int x, int y)
      {
      if (y == 0)
      return x;
      else
      return Add( x ^ y, (x & y) << 1);
      }







       
      Loading...

Share This Page

Share