7 Secrets of C language Every Programmer Should Know
12 Jun, 2019
2061 views
Facebook
LinkedIn
Twitter
7 Secrets of C language Every Programmer Should Know
Are you a Computer science student or a C programmer?
Are you also seeking C programming assignment help?
If so, then these secrets will help you a lot in completing your projects and assignments easily.
The experts of Global Assignment Help have discovered these secrets after a lot of research on different areas of C. All these secrets are based on real-time use cases that will help you understand the concepts of C easily.
7 Secrets of C language Every Programmer Should Know
OK!!! So, let’s have a look into these 7 secrets…
Secret #1: You can add any number without using ‘+’ operator
Simply write a function Add() that will return a sum of two integers. Here the Bitwise operation can be used to perform the addition operation. Go through the following code to see how:
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1); }
Secret #2: You can call functions when a program is about to terminate
For this atexit() API is used. The function pointed by atexit() is automatically called without arguments when the program terminates normally. See the below example: #include #include void ABC(void) { printf(“Goodbye ABC!n”); } void XYZ(void) { printf(“Goodbye XYZ!n”); } int main(int argc, wchar_t* argv[]) { atexit(XYZ); atexit(ABC); return 0; } OK! So, now we can see that ABC and XYZ haven’t been called but are registered to be called when the program is terminated.
Secret #3: Use Macro to get an array size of any kind of data type
A sizeof operator is used in such case to compute the size of any of the object. This operator is used in two forms. 1.To get the object’s size that can be a variable, array, integer 2.To get the size of a type name like int, double, structure, pointer, etc For Example: #define NUM_OF(x) (sizeof (x) / sizeof (*x)) int _tmain(){ int number[10] = {1,1,1,1,1,1}; char *teststr[20] = {“”,””,””,””,””,””,””,””,””}; printf(“Size of number[10] is %dn”, num(number)); printf(“Size of teststr[20] is %dn”, num(teststr)); } OUTPUT: Size of a number[10] is 10 Size of teststr[20] is 20 Press any key to continue . . .
Secret #4: Simply swap two variables without using any Temp variable
Now you can simply swap two variables without using the arithmetic operators. All you have to do is use XOR operator. Go through the following example to know how: a = a ^ b; b = a ^ b; a = a ^ b; // OR a = a + b –(b=a); // OR a ^= b ^= a ^= b;
7 Secrets of C language Every Programmer Should Know
Secret #5: Know the difference between array and pointers
Most of the students tend to do this mistake that is they usually get confused between array and pointers. Pointers are basically used for storing addresses of dynamically allocated arrays. Pointers are variables holding the address of the same location, whereas, an array is a contagious sequence of a memory location. At the time of compilation, an array always remains an array.
While during run-time an array develops a pointer. To get this concept more clear, let’s have a look at the following example: // Program to show that array and pointers are different – #include int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr = arr; // sizof(int) * (number of element in arr[]) is printed printf(“Size of arr[] %dn”, sizeof(arr)); // sizeof a pointer is printed which is same for all type // of pointers (char *, void *, etc) printf(“Size of ptr %d”, sizeof(ptr)); return 0; } OUTPUT: Size of arr[] 24 Size of ptr[] 8
Secret #6: Always put the constant as the first term while making comparisons
Students usually get confused between ‘=’ operator and ‘==’ operator. So, to resolve this issue our experts have suggested to use the defensive programming approach that is: Use ‘0==x’ instead of using ‘x==0’ Doing so will help you to solve the problem as the compiler will always flag an error for the miswritten that is x==0. For Example: Suppose, you have written if ( 0==x ) NOTE: The compiler will definitely show an error and refuse to compile the program
7 Secrets of C language Every Programmer Should Know
Secret #7: Know how “goes to –>” operator work
In the C programming, -> it is not an operator. In fact, this operator is formed by combining two main operators that are ‘–’ and ‘>’.
To understand it better go through the following example:
int _tmain()
{
int x = 10;
while( x –> 0 ) // x goes to 0
{
printf(“%d “, x);
}
printf(“n”);
}
Here the conditional code decrements ‘X’ while returning to X’s original (not decremented) value, and then it compares the original value with ‘0’ using the ‘>’ operator.
OUTPUT:
9 8 7 6 5 4 3 2 1 0
Press any key to continue…
Bonus Tip: You can even log off your computer using C programming
int main()
{
System(“shutdown -l -f -t 00”);
}
So, these are the 7 unknown secrets of the C language that every programmer should know while doing programming assignments. These secrets are not only restricted to C language, you can also apply these tips for completing different programming assignments like java assignment, C++ assignment, etc.
In case of any query, you can easily reach our experts to avail C programming assignment help.
7 Secrets of C language Every Programmer Should Know
Welcome to academic-answers.net
Our professional essay writing service is
renowned for being the best there is.
Our Commitment:
*100% Original work, Authentic papers, 0% plagiarism
*Affordable prices and great discounts.
*24/7 Support
*Free revisions
*Quality work within your set deadline.
Whatsapp: +1(951)901-6064
https://wa.me/19519016064