Q:

C-Server - first 3 characters of a string

a test string comes in
 123-eee

name of it: "inn"
I want to print only first 3 characters of it.
all the action happens here:
strncpy(inn2, inn, 3);
printf(inn);
printf(inn2);

this actually works. it prints both inn and inn2 but if I remove
printf(inn);

this wont do it's job:
printf(inn2);

What's the problem ?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
#include <string.h>

char outt[15];
char inn[100];
char inn2[100];

int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = sizeof(cli_addr);

  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    err(1, "can't open socket");

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

  int port = 88;
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(port);

  if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
    close(sock);
    err(1, "Can't bind");
  }

  listen(sock, 5);
  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);

    ssize_t bytesRead = read(client_fd,inn,sizeof(inn) - 1);
    if ( bytesRead > 0 )
    {
    inn[ bytesRead ] = '\0';
    strncpy(inn2, inn, 3);
    printf(inn);
    printf(inn2);
    }

    snprintf(outt, sizeof outt, "%s\n", inet_ntoa(cli_addr.sin_addr));

    if (client_fd == -1) {
      perror("Can't accept");
      continue;
    }

    write(client_fd, outt, sizeof(outt) - 1); /*-1:'\0'*/
    close(client_fd);
  }
}

A:

Remember that the standard output stream is line buffered, so output will not show up until you print a newline character, or the buffer becomes full. Add a printf("\n"); after your output statements and see if it works.

