Thread: Quick question on sprintf and memcpy

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Question Quick question on sprintf and memcpy

    Hello ppl,,

    I would like to know the basic diff between sprintf and memcpy tho i know the syntax of each of these functions....I am interested on functional behaviour of both of these,,,thks in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    memcpy is a function that copies bytes from one place to another.
    sprintf is a functions that copies a format string into a string buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    thks for quick reply Elysia!!

    Just to get my understanding right i have an eg here...

    Code:
    char abc_ca[10];
    char def_ca[15];
    
    memset(abc_ca,0x00,strlen(abc_ca);
    memset(def_ca,0x00,strlenf(def_ca);
    sprintf(def_ca, "%s", abc_ca); 
    
    so in def_ca[10],[11],[12],[13],[14] i would have values '\0' dince we would copy only 10 characters from abc_ca right?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    considering the fact that abc_ca is filled with some data for first 10 characters say "ABCDEFGHIJ"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have some misunderstanding of strings.
    I must remind you that you need a buffer of sizeof("BACDEFGHIJ") + 1 to store that string. Furthermore, strlen will only get the length of a string, not the buffer, and if the buffer is unitialized, then using strlen on it is undefined.
    In your example, sprintf will copy abc_ca into def_ca, though. But normally you would use strcpy for that. sprintf is more useful for formatting a string, say using numbers and the like.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Wink

    got it now...thks elysia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting an int into hex and memcpy
    By puppy in forum C Programming
    Replies: 10
    Last Post: 07-03-2006, 11:39 AM