Thread: Why the compiler is showing strange output for the strncpy function?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Why the compiler is showing strange output for the strncpy function?

    Please check the following program:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main(void){
        char nameB[]="Jerry";
        char nameA[]="Tom";
    
    
        printf("\n nameA: %s",nameA);
        printf("\n nameB: %s",nameB);
    
    
        strncpy(nameA,nameB,4);
    
    
        printf("\n nameA: %s",nameA);
        printf("\n nameB: %s",nameB);
        printf("\n");
        return 0;
    
    
    
    
    }
    Output is following:

    Code:
    nameA: Tom
    nameB: Jerry
    nameA: JerrJerry
    nameB: Jerry
    Any idea why this strange result is coming? I think buffer overflow is a reason for it because the string nameA contains only 3 characters but the strncpy function copied 4 characters in it.

    If it is the case, then I expect a random output from the memory/other errors but here the output for nameA is JerrJerry which suggests the first 4 characters of nameB is copied successfully and nameB is printed again. Any clue?

    The output changes when I change the order of 5th and 6th line in the same program.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main(void){
        char nameA[]="Tom";
        char nameB[]="Jerry";
    
    
    
    
        printf("\n nameA: %s",nameA);
        printf("\n nameB: %s",nameB);
    
    
        strncpy(nameA,nameB,4);
    
    
        printf("\n nameA: %s",nameA);
        printf("\n nameB: %s",nameB);
        printf("\n");
        return 0;
    
    
    
    
    }
    Logically, there should be no reason for a different type of result but the following output comes:

    Code:
    nameA: Tom
    nameB: Jerry
    nameA: JerrH "
    nameB: Jerry
    Now, it looks like a random result is generated for NameA for buffer overflow. But here also, first 4 characters are copied which should not happen.

    What is the reason of strange result of case 1 & 2 and why for the same program, different output is coming? I am using Codeblock with MinGW compiler.

    Any help will be highly appreciated!
    Last edited by Ahmed Tarek; 06-10-2012 at 09:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Promt showing in window programming - Compiler dependent?
    By random_cpp_user in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2010, 04:24 AM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. strange compiler
    By LeonLanford in forum C Programming
    Replies: 20
    Last Post: 04-12-2007, 12:29 PM
  5. Strange function output.
    By Pickels in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 08:57 PM