What is difference between static const and const in C with Example Code



#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

int testa =  100;
int testb =  101;

int test()
{   
    if(testa > testb)
        return 0;
    else
        return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    for(int k=0; k<5; k++)
    {   
        static const int tbit = test();
        printf("Result = %d\n", (tbit));
        testa++;
        testb--;   
    }
   
    getch();
    return 0;
}

O/P:


Result = 1
Result = 1
Result = 1
Result = 1
Result = 1


#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

int testa =  100;
int testb =  101;

int test()
{   
    if(testa > testb)
        return 0;
    else
        return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    for(int k=0; k<5; k++)
    {   
        const int tbit = test();
        printf("Result = %d\n", (tbit));
        testa++;
        testb--;   
    }
   
    getch();
    return 0;
}


O/P:

Result = 1
Result = 0
Result = 0
Result = 0
Result = 0

Related Post:

Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault ?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in linux with c example code

Share on Google Plus

About Kapil

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...