0

so in the last printf, i wanted to take 2 first characters from user input using fgets. I did that but it seems there is a problem with these 2 digits outputs. There's an unexpected character in the last word, how do i remove that? Im using code block C Languange and with GNU GCC Compiler.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
 char nama[20],alamat[30],kota[20],jenis_kelamin[2];
 char nama2[4],alamat2[4],kota2[4];
 int nomor;

 printf("Enter your name                                   = ");
 fgets(nama,20,stdin);
 printf("Nama                                              = %s\n",nama);

 printf("Enter your address                                = ");
 fgets(alamat,30,stdin);
 printf("Alamat                                            = %s\n",alamat);

printf("Enter your city name                               = ");
fgets(kota,20,stdin);
printf("Kota                                              = %s\n",kota);

printf("Masukkan Jenis Kelamin Anda Dengan Huruf L atau P = ");
fgets(jenis_kelamin,2,stdin);
printf("Kelamin                                           = %s\n",jenis_kelamin);

printf("\nMasukkan Nomor NIM                                = ");
scanf("%d",&nomor);
printf("Nomor                                             = %d\n",nomor);

//strncpy(nama2,nama,2);
//strncpy(alamat2,alamat,2);
//strncpy(kota2,kota,2);

printf("\n*******************************************************\n");
printf(" Your name is                   * %s",nama);
printf(" Your address is                * %s",alamat);
printf(" Your cith name is              * %s",kota);
printf(" Jenis Kelamin anda adalah      * %s\n",jenis_kelamin);
printf(" Nomor NIM anda adalah          * %d ",nomor);
printf("\n*******************************************************\n");

strncpy(nama2,nama,2);
fflush(stdin);
strncpy(alamat2,alamat,2);
fflush(stdin);
strncpy(kota2,kota,2);

printf(" 2 First Character of your Name* %s\n",nama2);
printf(" 2 First Character of your Address* %s\n",alamat2);
printf(" 2 First Character of your City* %s",kota2);
printf("\n*******************************************************\n");

return 0;

}

However when i run this the output is not what i expected:

*******************************************************
Your name is                   * Mark
Your address is                * GPA
Your city name is              * New York
Jenis Kelamin anda adalah      * L
Nomor NIM anda adalah          * 13
******************************************************
2 First Character of your Name          * Ma√⌂L
2 First Character of your Address       * GPå¬Ma√⌂L
2 First Character of your City          * Ne
*******************************************************

There is a weird character in the last word of name and city. Like å¬Ma√⌂L and √⌂L

It should be like this:

******************************************************
2 First Character of your Name          * Ma
2 First Character of your Address       * GP
2 First Character of your City          * Ne
*******************************************************

How to solve this? What did i miss? Thank you.

Lundin
  • 195,001
  • 40
  • 254
  • 396
FinnnIT
  • 27
  • 4
  • `strncpy` is one of those dangerous functions that nowadays should never be used for any purpose. You seem to be picking up bad habits from bad learning material or a bad compiler. Use `strcpy` instead. For details see [Is strcpy dangerous and what should be used instead?](https://software.codidact.com/posts/281518) Also `fflush(stdin)` is undefined behavior, see https://stackoverflow.com/questions/2979209/using-fflushstdin – Lundin Nov 03 '21 at 09:27
  • Also, I'm not sure why someone down voted. This is a decent question and the OP is obviously suffering from harmful learning material. When that happens, it's a good idea to bring the code to SO since their current source of learning can't be trusted. – Lundin Nov 03 '21 at 09:33

1 Answers1

1

You did not copy zero terminating character. strncpy does not copy it when the length of string exceeds the limit.

strncpy(alamat2,alamat,2);
alamat2[2] = '\0';
// etc...

Let's say fflush(stdin); is invalid. Do not use it.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Oh man, thanks it really works using your solution! Can I ask? what '\0' stand for? and what this called? – FinnnIT Nov 03 '21 at 09:23
  • @FinnnIT If you don't know that, you need to study the chapter regarding strings in your C programming book. Seriously. – Lundin Nov 03 '21 at 09:28