increment char

This is a discussion on increment char within the C Programming forums, part of the General Programming Boards category; Hi guys, i need some help with C functions, i'm looking for a way to do two things but i'm ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Post increment char

    Hi guys, i need some help with C functions, i'm looking for a way to do two things but i'm struggling a little, was wondering if someone here can point me in the right direction.

    1st problem:
    This is a trivial one i want to join some variables together to make a new one. i know in vb.net it would be somthing along the lines of:

    Code:
     value3 = value1 & value2
    so if value1 was "Hello" and value2 was "World" i then set a label.text to value3 it would read "HelloWorld", can i do this in C some how?

    2nd problem:
    This the code for my test app:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
     int node1 = 1;
     int node2 = 1;
     int node3 = 1;
     int node4 = 1;
    
            while (node4 <= 26){
            if (node1 <= 26){
            printf("%i:%i:%i:%i\n", node1, node2, node3, node4);
    
            node1 = node1++;
                }
                else if (node2 < 26){
                    node1 = 1;
                    node2 = node2++;
                    }
                        else if (node3 < 26){
                        node1 = 1;
                        node2 = 1;
                        node3 = node3++;
                        }
                            else{
                            node1 = 1;
                            node2 = 1;
                            node3 = 1;
                            node4 = node4++;
                            }
            }
         return 0;
    }
    what i'm looking to do is output a string from "AAAA" to "ZZZZ", i have it working for "1:1:1:1" to "26:26:26:26" but am i able to change these numbers in to letters by doing something like this:

    Code:
    int letnum = 10;
    char alphab = "ABCDEFGHIJLMNOPQRSTUVWXYZ";
    
    /* then do somthing like the following */ 
    
    char letter = left(alpha, letnum); 
    
    /* to set "letter" to "J" */
    Sorry if these are stupid questions, still very new to C.

    Okiura

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So do you have a C reference? If not, you should get one rather than asking every question you will ever have on a web forum.

    In any event: 1) strcat, 2) use array indexing.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Okiura View Post
    Hi guys, i need some help with C functions, i'm looking for a way to do two things but i'm struggling a little, was wondering if someone here can point me in the right direction.

    1st problem:
    This is a trivial one i want to join some variables together to make a new one. i know in vb.net it would be somthing along the lines of:

    Code:
     value3 = value1 & value2
    so if value1 was "Hello" and value2 was "World" i then set a label.text to value3 it would read "HelloWorld", can i do this in C some how?
    No. Sorry, but it's that simple, you can't do that.

    C is a language of numbers. It is only trivially aware of strings and text. If you want to manipulate lines (or pages) of text you need to use functions like those in the standard C string library. You would do well to familiarize yourself with these functions early on as you will likely spend most of your time working with them. The example above would be accomplished with the strcpy() and strcat() functions, like this...
    Code:
    char words[24];
    char second[12];
    
    
    // put text in char arrays
    strcpy(words,"Hello ");
    strcpy(second, "World!");
    
    // join them
    strcat(words, second);
    
    printf("%s\n",words);
    Yeah I know,that sucks ... but that's how it's done.

    2nd problem:
    This the code for my test app:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
     int node1 = 1;
     int node2 = 1;
     int node3 = 1;
     int node4 = 1;
    
            while (node4 <= 26){
            if (node1 <= 26){
            printf("%i:%i:%i:%i\n", node1, node2, node3, node4);
    
            node1 = node1++;
                }
                else if (node2 < 26){
                    node1 = 1;
                    node2 = node2++;
                    }
                        else if (node3 < 26){
                        node1 = 1;
                        node2 = 1;
                        node3 = node3++;
                        }
                            else{
                            node1 = 1;
                            node2 = 1;
                            node3 = 1;
                            node4 = node4++;
                            }
            }
         return 0;
    }
    what i'm looking to do is output a string from "AAAA" to "ZZZZ", i have it working for "1:1:1:1" to "26:26:26:26" but am i able to change these numbers in to letters by doing something like this:

    Code:
    int letnum = 10;
    char alphab = "ABCDEFGHIJLMNOPQRSTUVWXYZ";
    
    /* then do somthing like the following */ 
    
    char letter = left(alpha, letnum); 
    
    /* to set "letter" to "J" */
    Sorry if these are stupid questions, still very new to C.

    Okiura
    What you're looking for is the character's offset into the ascii code ... if you do this...
    Code:
    printf("%c %c %c %c", node1 + 'A', node2 + 'A', node3 + 'A', node4 + 'A');
    You'll get characters instead of numbers on your screen...

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Quote Originally Posted by tabstop View Post
    So do you have a C reference? If not, you should get one rather than asking every question you will ever have on a web forum.

    In any event: 1) strcat, 2) use array indexing.
    yes i do, i have google too but was struggling to get a useful answer.


    @CommonTater

    thanks for you response thats exactly what i was looking for! made some changes and now my code does exactly what i want it too, also you explained in a way that was easy to follow

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I don't know which compiler you're working with ... but it should have some kind of library documentation with it... You really should get yourself familiar with it's various functions. Also you would do yourself a favor by spending a bit of time with a good tutorial or book on C... It seems a simple language at first but even 8 years into it, it still surprises me from time to time... Read the pages, do the exercises, one by one. In the end you'll be glad you did.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    498
    For this -

    Code:
    node1 = node1++;
    all you need is

    Code:
    node1++;
    although that does work.

    What you've done is this:

    Code:
    node1 = node1;
    node1 = node1+1;
    The plus plus ( ++ ) operator increments the value of node1;
    no need to assign the value back into node1 with assignment operator (the = sign).
    The location of the ( ++ ) also means that the increment will occur after the variable
    is used in the expression. So what happens in your code is:
    node1 is made equal to node1
    node1 is incremented
    Last edited by megafiddle; 05-27-2011 at 04:50 PM.

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Code:
    node1 = node1++;
    This code actually results in undefined behavior, and is thus not the same as node1++. You cannot modify something at the same time you're assigning to it.

    This problem is not just theoretical. I have compilers that produce different results for the undefined code. Some perform an increment, some leave the value the same.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    498
    Good to know, if that's true.

    Shouldn't it always work though?
    It is a post increment, and should only and always occur after node1 is evaluated.
    consider:
    a = b++;

    Should be well defined, a = b, then b is incremented.

    So,

    a = a++;

    a = a, then a is incremented.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by megafiddle View Post
    Good to know, if that's true.
    It is.
    Quote Originally Posted by megafiddle View Post
    consider:
    a = b++;

    Should be well defined, a = b, then b is incremented.

    So,

    a = a++;

    a = a, then a is incremented.
    Those two are not the same thing.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Quote Originally Posted by megafiddle View Post
    Shouldn't it always work though?
    It is a post increment, and should only and always occur after node1 is evaluated.
    consider:
    a = b++;

    Should be well defined, a = b, then b is incremented.

    So,

    a = a++;

    a = a, then a is incremented.
    The quick and easy answer is that even if it appears to be well-defined, it explicitly is not:
    Quote Originally Posted by C99 6.5p2
    Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.
    But I'll try to explain why there are multiple ways to reasonably interpret how the expression should be evaluated.

    First, ask yourself why the post-increment should necessarily occur after the assignment. Post-increment yields the previous value and then increments the object; but when does that increment occur? Does it have to occur after all other parts of the expression have been evaluated? It does not. It just has to happen by the next sequence point, which in this case is the semicolon.

    So take the expression a=a++, and let's see two ways it might be interpreted:
    Code:
    int tmp;
    tmp = a; /* remember the previous value */
    a = a + 1; /* perform the increment */
    a = tmp; /* perform the assignment (using the stored value) */
    How would this violate the rules of post-increment? It doesn't: the previous value is being used, the increment is happening, and the assignment is happening. Here's another way it could be done:
    Code:
    a = a; /* perform the assignment */
    a = a + 1; /* perform the increment */
    So the first bit of code keeps the old value of a, the second results in an increment.

    There is nothing that guarantees that one part of the expression will happen before the other. You're given special guarantees at sequence points (these are, more or less, places that force evaluations to be completed), but there is no sequence point at = or ++.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    498
    Interesting. Thanks.

    Where are the sequence points in
    Code:
    a = a++;

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As cas said, there aren't any (except the semicolon at the end that completes the statement). The operations inside the statement can be performed in any order.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    498
    Got it.

    I did point out that
    Code:
    node1 = node1++;
    could be replaced with
    Code:
    node1++;
    It should be mentioned to Okiura, that this not only true, but recommended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment a date by 1 day?
    By fekkesh in forum C Programming
    Replies: 4
    Last Post: 03-08-2009, 04:46 AM
  2. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 11:06 PM
  3. Help with increment operator, please
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-18-2008, 07:06 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 04:03 AM
  5. Increment Twice in For loop
    By GaGi in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 12:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21