Thread: Program to change the name of a variable

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    Program to change the name of a variable

    Hey Please i wish to know if there is a program which i can use to change the name of a variable in a c program for example if i declare a variable like
    Code:
    int ch;
    printf("%d", ch);
    Can i later change the name of the ch variable to lets say "number" the code now becomes
    Code:
    int number;
    printf("%d", number);
    Last edited by acho.arnold; 06-15-2013 at 01:26 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Generally this is one of the action under code refactoring. Some IDE give you this possibility.

    So you should search for "C code refactoring tools" for your IDE/OS combination
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by vart View Post
    Generally this is one of the action under code refactoring. Some IDE give you this possibility.

    So you should search for "C code refactoring tools" for your IDE/OS combination
    I use Linux operating system and i am not rely an IDE user so i code using Gedit. I tried googling "c code refactoring plugins for gedit" but i found nothing so please is ther a program to do this which is not IDE specific?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Doesn't Gedit should support find and replace like most other text editors?

    Maybe you need to find and study the documentation for your text editor?


    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by jimblumberg View Post
    Doesn't Gedit should support find and replace like most other text editors?
    its not about find and replace condisder the following code
    Code:
    #include<stdio.h>
    int main()
    {
      int i;
    }
    Lets say we want to change the variable i to ch, using find and replace produces
    Code:
    #chnclude<stdcho.h>
    chnt machn()
    {
        chnt ch;
    }
    notice that all the letters including i have been changed to ch even when writing #'i'nclude is changed to #'ch'nclude
    Thanks for understanding
    Last edited by acho.arnold; 06-15-2013 at 02:46 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > I tried googling "c code refactoring plugins for gedit" but i found nothing so please is ther a program to do this which is not IDE specific?
    How many plugins can you add to an editor before it becomes an IDE?

    Anyway, consider this program.
    Code:
    #include <stdio.h>
    
    #define MACRO(ch) ch = 456
    
    void foo ( int ch ) {
      printf("%d\n", ch );
    }
    
    void *ch = NULL;
    
    int main(void)
    {
      int ch = 123;
      MACRO(ch);
      foo(ch);
      return 0;
    }
    You want to rename ch on line 13 - what do you do?

    Even if your editor has a 'match whole word' option, it still needs to know all about the semantic rules of C to be able to identify the correct instances of ch in any given context.

    This is what you can do in eclipse, with it's refactor tool
    Program to change the name of a variable-screenshot-2013-06-15-10-00-14-png
    Notice how only the instances of ch in main have a little highlight box around them when refactoring the desired variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    See if your editor's find and replace includes a "whole word" option, which will solve most of the variable renaming issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to know when a value of a variable change?
    By Hannibal2010 in forum C Programming
    Replies: 12
    Last Post: 11-25-2011, 02:16 PM
  2. Can't change variable from another function...
    By Ryan. in forum C Programming
    Replies: 9
    Last Post: 03-11-2011, 12:10 AM
  3. Replies: 4
    Last Post: 02-28-2011, 07:57 AM
  4. change variable and repeat
    By altf4thc in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2010, 05:27 PM
  5. unwanted Variable Change
    By purebuu in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 08:56 PM