C Programming

Is there any way to find the variables declared or defined in a c program and print those variables??

plz help

You mean from an binary/executable? Not unless you have debug info compiled in or have access to the source.

After the program is complied i want to print all the variables used inside main() as well as variables declared or defined in any function as my output..

Check the symbol table created during compilation.

Sir can u plz elaborate on dis i dont have any idea abt dis..

#include<stdio.h>
int main()
{
 int a,b ,c;
printf("hiii");

}

For Eg this is my code
Output i want is :

a
b
c

Please spell.

Unlike languages like shell, C does not have this kind of metadata available at runtime. The best you can do is scrape the source code with a text processor, or analyze the executable's debugging info.

In the example you provide, it is also most likely that your compiler will optimize away a, b and c, since these variables are not used anywhere in your code.

Sir I just want to find the variables being declared in the program..

Can you suggest ...

Netspeak is not permitted here. You know how to spell 'you'.

Do you need this information inside the C program, or not?

Yes Sir may be a file program which could take a C program and in output give me the variables been declared..

Your reply contradicts itself, but whatever.

You will have to scrape the program source code for variable definitions. I'm trying to find if I had a program to do this before, but I know it won't be easy. Something that understands C well enough to tell between a function definition and a variable definition is halfway there to a proper C parser.

Symbol table doesn't help, since local variables don't appear in the symbol table.

What have you tried?

Sir i was trying it using (gdb) info locals but its not giving the output properly..

gdb cannot do that, it can only list global variables and currently instantiated variables.

What precisely is this for? The doxygen application can extract information on variables if you wish. It can do a lot more than that as well.

So Sir other way would be search for word like int, float etc and store the words after int,float till I get a semicolon.

---------- Post updated at 12:52 AM ---------- Previous update was at 12:40 AM ----------

Sir I am working with naming convention in c.. where in a user writes his/her program and i want to check if conventions are followed or not.. like if program is to find sum of two numbers so variables declared be atleast num1, num2 like that rather then a,b..

The problem is, that's also valid syntax for declaring a function:

float myfunction(int val1, int val2);

In fact, you can mix them up. Here's a function, variable, pointer, and function pointer in one declaration:

float myfunction(int val1, int val2), myvar1, *myvar2, (*myfp)(int a, int b);

...so it's really not that simple, unless you only need it to work for a very simplified subset of C.

Thanhs for your suggestions Sir, I have started coding for this problem..