12 funny C language interview questions and answers-Comment on 12 interesting C language Q & A (4)

Source: Internet
Author: User
#include <stdio.h>

int main (void)
{
    char * ptr = "Linux";
    * ptr = 'T';

    printf ("\ n [% s] \ n", ptr);

    return 0;
}



Q: Can you write a program to change its name while it is running? (Can you write a program that changes its own name when run?)

 

#include <stdio.h>

int main (int argc, char * argv [])
{
    int i = 0;
    char buff [100];

    memset (buff, 0, sizeof (buff));

    strncpy (buff, argv [0], sizeof (buff));
    memset (argv [0], 0, strlen (buff));

    strncpy (argv [0], "NewName", 7);

    // Simulate a wait. Check the process
    // name at this point.
    for (; i <0xffffffff; i ++);

    return 0;
}
 

 int i = 0;
// ……
 for (; i <0xffffffff; i ++);

 

for (i = 0; i <0xffffffff; i ++)
   ;


    char buff [100];

    memset (buff, 0, sizeof (buff));

    strncpy (buff, argv [0], sizeof (buff));
    memset (argv [0], 0, strlen (buff));

       strncpy (argv [0], "NewName", 7);
 

Memset (buff, 0, sizeof (buff));
 

Strncpy (buff, argv [0], sizeof (buff));
 

   char s [4];
   strncpy (s, "1", 4);
   printf ("% d% d% d% d \ n", s [0], s [1], s [2], s [3]);

  49 0 0 0


memset (argv [0], 0, strlen (buff));

memset (argv [0], 0, strlen (argv [0]));

 

strncpy (argv [0], "NewName", 7);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.