Coding Tips and Tricks


C Programming Ideas and Methods Each Programmer Ought to Know

Top C Programming Tips and Tricks.
High C Programming Ideas and Methods.

Tip#1) – Macro to Get Array Measurement of Any Knowledge Sort

The next macro will enable you to in getting the dimensions of an array of any knowledge sort.  It really works by dividing the size of the array to the dimensions of its area.

#outline NUM_OF(x) (sizeof (x) / sizeof (*x))

Tip#2) – Calculate Elapsed Time

Buddies, have you ever ever wanted to calculate the time handed between two occasions? Or hold a test on some operate which is spuriously taking further execution time than anticipated?

Right here is the code snippet carried out utilizing a set of macros that will help you determine how lengthy one thing will take to run.

Tip#3) – Sensible Random Quantity Generator

Now we have this rand() operate outlined within the stdlib.h for the random quantity era. Did you utilize it and realized that each time you run your program, nevertheless it returns the identical end result.

It’s as a result of, by default, the usual (pseudo) random quantity generator will get seeded with the #1. To have it begin anyplace else within the collection, name the operate srand (unsigned int seed).

For the seed, you should use the present time in seconds.

Annexure: On your word, the above code seeds the generator from the present second of time. This reality implies that when you count on your program to re-run greater than as soon as a second, the given code might not fulfill your requirement. A potential workaround is to retailer the seed in a file (that you’ll learn later out of your program), and you then increment it each time this system is run.

Tip#4) – Heard of “goes to–>” Operator?

In C programming, the image (–>) doesn’t symbolize an operator.

As an alternative, it’s a mixture of two separate operators, i.e., -- and > generally known as the “goes to.”

To grasp how “goes to” operator works, undergo the under code snippet.

Within the instance, there may be conditional’s code which decrements variable x, whereas returning x’s unique (not decremented) worth, after which compares it with 0 utilizing the > operator.

Tip#5) – Some Cool SCANF Methods

Discover out a few of the unheard scanf methods that you need to know.

Tip#6) – Name Capabilities at Program Termination

Do you know concerning the atexit() API? This C API is used to register capabilities which might get routinely referred to as when this system finishes its execution.

For instance –

Discover that foo and bar capabilities haven’t been referred to as however are registered to get referred to as when this system exits.

These shouldn’t return something nor settle for any arguments. You may register as much as 32 such capabilities. They’ll get referred to as within the LIFO order.

Tip#7) – Initialize a 2-D Array with a Lengthy Record of Values

It may be simply achieved by preserving the record values right into a file after which retailer the file content material into the 2-D array with the next line of code.

Tip#8) – Add Any Numbers with out “+” Operator

Bitwise operators can be utilized to carry out the addition (+) operation as talked about in under instance:

Tip#9) – Swapping Two Variables with out Any Temp Variable

There are 3 ways to do that which I’ve talked about under.

To swap two variables with out utilizing further house or arithmetic operators, you may merely use the xor operator.

Tip#10) – Put the Fixed Because the First Time period Whereas Making Comparisons

Typically, we are likely to confuse “=” operator with “==” operator. To keep away from this, use the defensive programming strategy. 0==x as an alternative of x==0 in order that 0=x could be caught by

It means it’s best to write “1==x” as an alternative of “x==1” in order that the compiler will at all times flag an error for the miswritten “1=x”.

So everytime you mistakenly write the next.

The compiler will complain and refuse to compile this system.

Whereas it’s not potential if you’re evaluating two variables. For instance, the expression

could be miss written as

Tip#11) – Fast Commenting

Typically it’s possible you’ll end up attempting to remark blocks of code which have feedback inside them. As a result of C doesn’t enable nested feedback, it’s possible you’ll discover that the */ remark finish is prematurely terminating your remark block.

You may make the most of the C Preprocessor’s #if directive to avoid this:

Tip#12) – Use of Conditional Operator

The Conditional operator is often known as the Ternary operator. We largely use it within the following kind:

However in C++, you may also use it within the following method:

Tip#13) – Arrays and Pointers not Completely the Identical

Many people are likely to misunderstand an idea that pointers and arrays are the identical. They aren’t.

Pointers are merely variables holding the handle of some location whereas an array is a contiguous sequence of reminiscence places.

Pointers may help to create heterogeneous knowledge constructions corresponding to a hyperlink record or hash desk. Whereas the arrays are homogenous which might maintain solely values of comparable sort corresponding to numbers and strings.

Pointers get allotted dynamically on the heap whereas the arrays are static allocations on the stack.

At compile time, an array is an array. Solely throughout run-time, an array devolves to a pointer.

To show this reality, let me present you an instance.

Tip#14) – Pointer to array and Array of Pointers

Let’s take a look at an attention-grabbing comparability between the next three declarations.

Right here in int *ptr1[5], ptr1 is an array of 5 integer pointers (An array of int pointers).

And in int (*ptr2)[5], ptr2 is a pointer to an array of 5 integers (A pointer to an array of integers).

It’s identical as ptr1 (An array of int pointers).


Getting Began

Open the Welcome web page to get began with the fundamentals of VS Code. Assist > Welcome.

Consists of the Interactive Playground.

Add JSON Validation

Enabled by default for a lot of information. Create your personal schema and validation in settings.json

or for a schema outlined in your workspace

or a customized schema

See extra within the documentation.

Very quick random quantity generator

Xorshift random number generators can be utilized to create sequences of pseudorandom numbers very, in a short time. They’re helpful while you want randomness however don't wish to endure a efficiency penalty.

Right here is an implementation taken from Aristide on StackOverflow:

Whether or not a quantity is divisible by one other quantity

If you wish to know if 10 is divisible by 2, you should use the modulo operator to seek out out the rest while you divide 10 by 2. If it's , the quantity is divisible by that quantity.

That is how one can remedy the FizzBuzz downside.

Standing Bar

Errors and warnings

Keyboard Shortcut: ⇧⌘M (Home windows, Linux Ctrl+Shift+M)

Rapidly bounce to errors and warnings within the challenge.

Cycle via errors with F8 or ⇧F8 (Home windows, Linux Shift+F8)

You can filter problems either by type ('errors',

You may filter issues both by sort (‘errors’, ‘warnings’) or textual content matching.

Change language mode

Keyboard Shortcut: ⌘Ok M (Home windows, Linux Ctrl+Ok M)

If you want to persist the new language mode for t

If you wish to persist the brand new language mode for that file sort, you should use the Configure File Affiliation for command to affiliate the present file extension with an put in language.


Create customized snippets

File > Preferences > Person Snippets (Code > Preferences > Person Snippets on macOS), choose the language, and create a snippet.

See extra particulars in Creating your own Snippets.

Abstract C Programming Ideas and Methods

Whereas beginning to write this text, we thought to current you with ten better of the C programming suggestions however ended up in delivering 15 great suggestions.

We tried to cowl these suggestions which you may relate to and are usable in your manufacturing atmosphere. On your data, we’re deeply impressed by the daddy of C “Dennis Ritchie.” You can too observe him @ his wiki page.

Earlier than we conclude, a humble request to share this submit with your pals. And additionally depart your priceless suggestions within the remark field on the finish of this submit.

You are most welcome to ask questions on this submit which we’ll be very happy to reply.

Nevertheless, under are some tutorials and posts which we advocate so that you can learn and construct a greater understanding of the C/C++ programming.

Really useful Posts: