Labels

Sunday 30 July 2017

How to print numbers from 1 to 100 without using loop?

How to print numbers from 1 to 100 without using loop?

With the help of recursion we can achieve the output. Please find below code snippet.

#include <stdio.h>

/* Prints numbers from 1 to n */
void printNos(unsigned int n)
{
    (n > 0 && (printNos(n-1), printf("%d ", n)));
    return;
}

/* program to test printNos */
int main()
{
    printNos(100);
    getchar();
    return 0;
}
 From the above code snippet, one thing need to understand is "Usage of comma operator".

(n > 0 && (printNos(n-1), printf("%d ", n)));

As per the precedence & associativity the above statement gets evaluated from left to right. and the order follows as below

1. checks whether n is greater than 0 or not? i.e., n > 0
2. As the input (1st) for logical AND operator is true and checks for the second input. That's why it is acting as a "if" block.
3. When it comes to second input for && operator, its a set of statements enclosed by parenthesis ( '(' and ')' ). 
Note: Comma ( ,) has the lest precedence, so all statements will call/run inside the parenthesis and the right most value will be used as a final set value.


To understand this please see the below example.

int a = (3, 5); // a gets value as 5. not 3

why because = has high precedence and evaluates from right to left (as per associativity). Right part is parenthesis and has two values separated by comma (,) and comma evaluates from left to right. and the right most value will gets returned to from the set to the variable a.

Saturday 29 July 2017

How to find addition of two numbers without using any arithmatic operator?


To find sum of two numbers without using any arithmetic operator


Write a C program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.
--> We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbers. We can use ‘*’ which indicates the minimum width of output. For example, in the statement “printf(“%*d”, width, num);”, the specified ‘width’ is substituted in place of *, and ‘num’ is printed within the minimum width specified. --> If number of digits in ‘num’ is smaller than the specified ‘wodth’, the output is padded with blank spaces. If number of digits are more, the output is printed as it is (not truncated).
int add (int z, int x)
    return printf ("%*d%*d", z, 1, x, 2);
}
int main (void)
{
    printf ("\nsum=%d\n", add(4,4));
}
Output:
   1   2
sum=8
The output is: number "1" is represented in width (in 4 digit place, as <space><space><space><1>) and the number "2" is also followed the same. Totally first add() printed z+x characters on the console using printf and printf returns number of characters printed on the console. So we will get the addition result as a return value for add().

Saturday 8 July 2017

How to run a c program without using main() function in it?

How to write a C program without "main()" function?

Logically it’s seems impossible to write a C program without using a main() function. Since every program must have a main() function because:-
  • It’s an entry point of every C/C++ program
  • All Predefined and User-defined Functions are called directly or indirectly through the main
This can be achieved by using two methods and are as follows
  • Indirect Method
  • Direct Method

Indirect Method:

 In this indirect method, we will use the main function indirectly and that is with the help of macro's. Please take a look at the code snippet given below

Example1:
#include <stdio.h>
#define fun main
int fun(void)
{
    printf("This is My Main Function");
    return 0;
}
 From the above code, fun is the function which i am using instead of main and fun is a macro which equivalent is "main". While compiling the above program, fun gets replaced by main at the time of preprocessing stage. After preprocessing stage code almost looks same like we used main.

Example 2:
#include <stdio.h>
#define fun m##a##i##n
int fun(void)
{
    printf("This is My Main Function");
    return 0;
}
The example 2 is almost same as example 1. The only difference is that, example 2 uses tokens in macro, which will concatenate the m,a,i,n and forms a string called "main".

Direct Method:

In this method, we will directly use custom main function and there is no main concept at all.
Always compiler uses _start as the starting point for any program. The _start function always looks for an entry point from the user program and that entry point is fixed and named as "main". So whenever we use start files (_start) which always looks for main and that's why main is necessary.

In order to avoid the main function from our program, we shouldn't use the start files (_start). While compiling our program we should tell to the compiler that, "No need to use start files". We can intimate to the compiler that not to use start files by using compiler arguments and is given below.
gcc test.c -nostartfiles -o test_bin
Example 3:
#include <stdio.h>
int fun(void)
{
    printf("This is My Function");
    exit(0); //return statement shouldn't use, cause segmentation fault
}
 If you compile the above program (example 3) with "-nostartfiles" option, then binary will gets created. 
From the above program (example 3) we should note few important things and are listed below.
1. We shouldn't use return statement, why because as we haven't used _start, So no one will be ready to collect our return status. If we try to use return statement, which will leads to segmentation fault.
2. exit(0) statement tells the loader to unload the program from the memory. Why because loader is the one who loads our program into memory.

What if multiple functions definitions present?

If multiple functions (definitions) present in the file, then the compiler will choose one entry point for the program. And the compiler always choose first function which defined in the program.

Example 4:
#include <stdio.h>
int fun(void)
{
    printf("This is My fun Function");
    exit(0); //return statement shouldn't use, cause segmentation fault
}
int print(void)
{
      printf("This is My print Function");
    exit(0); //return statement shouldn't use, cause segmentation fault
}
 If we compile and run the above program (example 4), then we will get output as "This is My fun Function".

If we want to select our function as entry point, then we should intimate to the compiler while compiling program by using option.
From the example if i want to use print as my entry point, then i should compile by specifying entry point option.
gcc test.c -nostartfiles -e print -o test_bin
 "-e" option tells the compiler as an entry point.

Case Study:

We can use customized entry point option even when the main present. See the code below. 

Example 5:
#include <stdio.h>
int main(void)
{
    printf("This is My main Function");
    exit(0); //return statement also works
}
int print(void)
{
      printf("This is My print Function");
    exit(0); //return statement shouldn't use, cause segmentation fault
}
From the above example we can compile and run the program by using entry point option.
gcc test.c -e print -o test_bin
If we runt the test_bin binary, then the output will be "This is My print Function".

***************************** Thanks for Reading ***************************