char constants vs. hard-coding


 
Thread Tools Search this Thread
Top Forums Programming char constants vs. hard-coding
# 1  
Old 08-20-2008
char constants vs. hard-coding

This might be a silly question, but I thought I'd ask anyway. If I'm writing in C, isn't it more efficient to, for instance, use constant character variable set to 'A' instead of hard-coding a character 'A'? Since it's only a single character instead of a string, it might not matter much.
# 2  
Old 08-20-2008
You would still need to somehow initialize the const char variable...unless I'm not reading you correctly. Best if you explained by an example.
# 3  
Old 08-20-2008
const char at_sign = '@';
char str[] = "fred@email.com;
char *ptr;
.
.
.

ptr = strrchr (str, '@');

vs.

ptr = strrchr (str, at_sign);
# 4  
Old 08-20-2008
The statement below is better only due to its generality.
Code:
ptr = strrchr (str, at_sign);

Later on if you wanted to set the at_sign variable to '~' then the change has to made only at one place...saving you a lot of typing; but in the first form all instances of '@' need to be replaced by '~' in all the source files.
Code:
ptr = strrchr (str, '@');

Other than that there is nothing to say that one approach is better or more efficient than the other.
# 5  
Old 08-20-2008
The only other point - char to int promotion. For example, when a char is passed to a function by value, it actually is promoted to an int behind the scenes. There are other instances during arithmetic and boolean operations when a char is promoted to an int, then truncated in the result. See the C99 standards document for more information.
Or see the prototypes in ctype.h for any of the character functions - you'll note the arguments are int.

This means that char may undergo type change behind the scenes - so I'm not sure what you are gaining - if anything - other than as shamrock points out, some generality.

Being careful with const values and writing functions as true 'black boxes' that return nothing but copies of arguments, changing no argument, give rise to idempotent functions. These are FAR easier for the compiler to optimize. Stdc lib example: strlen(). Takes a const char * argument returns size_t.
# 6  
Old 08-21-2008
Following on from shamrock,, I think it's a trade-off and depends on the context and intentions.
If the variable is likely to vary, then
Code:
ptr = strrchr (str, at_sign);

is "better" because it is more general, but means I have to go and look up what the variable is assigned to. So from a readability prespective,
Code:
ptr = strrchr (str, '@');

is "better" because it is clearly self-documenting and sufficient since, in the email address parsing example given, one wouldn't expect to change '@' to anything else.

There is always an element of subjectivity in these arguments, though. Take your pick
# 7  
Old 08-21-2008
Quote:
Originally Posted by cleopard
This might be a silly question, but I thought I'd ask anyway. If I'm writing in C, isn't it more efficient to, for instance, use constant character variable set to 'A' instead of hard-coding a character 'A'? Since it's only a single character instead of a string, it might not matter much.
Whether it's more efficient depends on the compiler and architecture. If you wanted to find out which is which, you could generate code to execute each code 100000 times, and then see which is more efficient.

Unter Intel architecture, hard-coding is more efficient because the compiler generates machine code in which the character @ is actually embedded into the instruction (implicit data). With the constant character, the compiler might also do this, or it might generate machine code using direct (absolute address) or indirect (relative to the stack) data addressing, both of which are less efficient. However, due to code caching and such, it's very unlikely to make any difference, unless this code is in many different places throughout the program.

Just for kicks, I wrote such programs and here are the run-times. When they finish, I'll post the run-times.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Web Development

Using LWP without hard coding password

Hi, I would like to read content from a https site. I have decided to use LWP module in perl. but it throwed 401 Authorization required error. i dont want to hard code the password in my perl code. Is there any way to achieve the authentication without hardcoding the password. Thanks,... (1 Reply)
Discussion started by: pandeesh
1 Replies

2. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

3. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

4. UNIX for Dummies Questions & Answers

Operation with real variables and constants

Hello there, I'd like to define a variable b equal to 0.5/a where a=0.001, so I wrote something like that: a=0.001; let 'b=0.5/$a'; but it doesn't work... maybe because the variable a has a real value??? Any help will be appreciated!!!:D (1 Reply)
Discussion started by: Giordano Bruno
1 Replies

5. UNIX for Dummies Questions & Answers

Sending mails to various users without hard coding the email IDS

Hi Can any one help me out ? I am trying to send an autogenerated mail with an attachment to bulk of users using 'MAILX' and 'UNENCODE' . I have used it as follows X " ( cat /sastemp/body.txt; uuencode Test.xls.gz Test.xls.gz ) | mailx -s 'Testing' ' abcd@yahoo.com , efgh@gmail.com ' " ... (9 Replies)
Discussion started by: manas6
9 Replies

6. Shell Programming and Scripting

I wanted to update a script, more dynamic (just say no to hard coding)...

currently it has the following: bdumpN=`ll /home/apps/oracle/admin/DBprod/bdump/DBprod_j* | grep "$Cdate" | wc -l` If I pass the DBname, I would not have to hardcode it in the script... I can capture the database name by adding the following: DBname=$1 The problem is, I have been unable... (2 Replies)
Discussion started by: mr_manny
2 Replies

7. Shell Programming and Scripting

perl: eval and constants

is it possible to use eval to create constants in perl? i cannot seem to get anything to work, and my searches are turning up little to nothing. an example of what i am trying to do is this: 2 arrays: array 1: 'FOOD','NUMBER','OS' array 2: 'pizza','two','unix' loop through the arrays and... (5 Replies)
Discussion started by: effigy
5 Replies

8. Programming

constants in C/C++

Hi all My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour) The code: #include <stdio.h> int main() { const int x = 10; int *y; const int * const z = &x; y = (int *)&x;... (2 Replies)
Discussion started by: Shobhit
2 Replies
Login or Register to Ask a Question