Thread: Concatenating Data of Different Types

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

    Concatenating Data of Different Types

    How to to concatenate a string data and a number?

    I know there is a function called strcat in C but it works for two strings. The problem I am facing now is I have a string and a number that I want to concatenate so that they become one string. Say that I define the three variables as the following:

    char studentName[] = "Student";

    int studentNo = 1;

    char combineData[];

    Assuming that I want the the combined data to be stored in the variable combineData, how I am going to do that in C? In this case, the variable combineData should have the value "Student1";

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always make a fourth buffer, and sprintf them all into it.


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

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by quzah View Post
    You could always make a fourth buffer, and sprintf them all into it.


    Quzah.
    This I find to be the most reasonable solution. It works for any type you'd ever want to use it with, too!
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data types
    By Govalant in forum C Programming
    Replies: 2
    Last Post: 06-04-2007, 08:53 PM
  2. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  3. data types help
    By Bud in forum C Programming
    Replies: 2
    Last Post: 06-27-2002, 12:35 PM
  4. data types
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-10-2001, 12:12 PM
  5. data types
    By wazilian in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 01:52 PM