Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to exclude a declared variable in your program

+4 votes
asked Mar 18 by Oratilwe Seleke (160 points)

1 Answer

0 votes
answered Mar 21 by Peter Minarik (63,140 points)

What programming language are we talking about?

What does "exclude" mean to you?

For instance, most programming languages have a scope for a variable. Outside of that scope, it does not exist.

Let's consider the following C code:

#include <stdio.h>

void PrintName(const char * name)
{
    printf("name: %s\n", name);
}

int main()
{
    const char * myName = "John Doe";
    PrintName(myName);
    return 0;
}

The scope of the variable myName is limited to the main() function. I could not reference it in the PrintName() function for instance.

Similarly, the scope of the variable name (, which is a function argument) is limited to the PrintName() function, it can only be accessed there.

Scopes are bound by opening and closing curly braces: { and }. So the body of every loop and if statement is also a new scope (even if you don't use the curly braces as the statement there is just a single line).

For instance, the following snippet causes a compilation error as the variable i is only visible within the for loop:

for (int i = 0; i < 10; i++)
    printf("i: %d\n", i);

printf("i's final value: %d\n", i);

I hope this helps.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...