Interesting Facts in C Programming | Set 2

Below are some more interesting facts about C programming:

1. Macros can have unbalanced braces:

When we use #define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
// Declaring Macro
// with unbalanced brackets
#define P printf(
  
int main()
{
    int a;
    P"Hello World");
    P"%d", a);
  
    return 0;
}

chevron_right


Output:

Hello World0

2. Use main to declare one or more integer variables:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main(int c)
{
    for (; ++c < 28;)
        putchar(95 + c);
    return 0;
}

chevron_right


Output:

abcdefghijklmnopqrstuvwxyz

3. “%m” when used within printf() prints “Success”

m (conversion specifier) is not C but is a GNU extension to printf. The ‘%m’ conversion prints the string corresponding to the error code in errno.

%m only prints “Success” when “errno == 0” (it’s short for a string representation of the last observed error state). For example, if a function fails before the printf, then it will print something rather different.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
    printf("%m");
    return 0;
}

chevron_right


Output:

Success

4. brk(0); can be used as an alternative for return 0;

brk() and sbrk() change the location of the program break, which defines the end of the process’s data segment.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
    printf("%m");
    brk();
}

chevron_right


Output:

Success

5. C program can be written without main()

Logically it 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.

But in reality, it is possible to run a C program without a main function.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdlib.h>
  
// entry point function
int nomain();
  
void _start()
{
  
    // calling entry point
    nomain();
    exit(0);
}
  
int nomain()
{
    puts("Geeksforgeeks");
    return 0;
}

chevron_right


Compilation using the command:

gcc filename.c -nostartfiles
(nostartfiles option tells the compiler to avoid standard linking)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.