1

I wrote a code in C to print the id number (could be combination of both numbers and letters) of the employee. The compiler takes the id number as an input but it is printing nothing. At first, I use 'printf' but it is not working so then I googled to end up with that the output is sometimes buffered for performance reasons in many systems. I got many possible answers in some following threads-

However, I tried all of the possibilities (being a beginner, implementation can be wrong), but none worked out for me [given as comments]. I have the code like following. Any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int* ptr;
    int m;
    char n;
    printf("Id number of employee 1:\n");
    printf("Enter your id number length\n");
    scanf("%d", &m);
    printf("%d\n", m);
    ptr = (int*) malloc (m *sizeof(int));
    printf("Enter your id number:\n");
    scanf("%s", &n);

    // fflush( stdout );
    // fprintf(stderr, "The id number of emploee 1 is %s", n);
    // setbuf(stdout, NULL);
    // setvbuf(stdout, NULL, _IONBF, 0);
    // setvbuf (stdout, NULL, _IONBF, BUFSIZ);
    // printf("The id number of employee 1 is %s\n", n);

    printf("The id number of employee 1 is %s", n);
    return 0;
}
  • 2
    A single char can't hold multiple letters. – tkausl Sep 12 '21 at 03:22
  • You have allocated `ptr` but then used a `n` which is a single char. And `ptr` needs to store `char` not `int`. So it should be `char *ptr = malloc(m); scanf("%s", ptr); printf("%s", ptr);` – kaylum Sep 12 '21 at 03:23
  • 1
    Actually probably should be `malloc(m+1)` if `m` represents the id length. Need one extra byte for the string NUL terminator. – kaylum Sep 12 '21 at 03:32
  • You don't check that `malloc()` succeeded; you don't check that `scanf()` succeeded. While they are unlikely to fail, when things go wrong, you need to add the missing error checking to see whether that is causing part of the problem. – Jonathan Leffler Sep 12 '21 at 03:38
  • @kaylum Thanks for your answers. I can realize my fault that I should use char not int and your answer worked out. One more confusion when I am giving the length, actually it has no effect i.e. if I give m = 5 after that I give more than 5 characters or less than 5 character it prints whole the input. How can I fix it to m value? – Adrija Roy Sep 13 '21 at 15:17
  • [How to limit scanf function in C to print error when input is too long?](https://stackoverflow.com/questions/10886594/how-to-limit-scanf-function-in-c-to-print-error-when-input-is-too-long). Please do searches. People are expected to do basic research and you will often find the answers faster that way too. – kaylum Sep 13 '21 at 20:32
  • @kaylum thank you for giving the solution and the suggestion also...Definitely I'll do basic researches from next time... – Adrija Roy Sep 14 '21 at 09:36

2 Answers2

0

As it was said in the comment section "a single char can't hold a string of letters". There were other things here to make a better code, e.g. freeing allocated memory. Here is code example of how it can be done:

int main()
{
     char* ptr;
     int m;

     printf("Id number of employee 1:\n");
     printf("Enter your id number length\n");
     scanf("%d", &m);

     ptr = (char*)malloc(m * sizeof(char) + 1);

     if (ptr == NULL) {
         printf("Allocation faild...");
         return 1;
         //you can call main to try again...
     }

     printf("Enter your id number:\n");
     scanf("%s", ptr);

     //scanf is not reliable, you can use a loop to enter all characters 
     //to the char array or validate scanf.
     

     printf("The id number of employee 1 is %s",ptr);
     // dont forget to free the allocated data

     free(ptr);
     
     return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

One possible solution which works is (without dynamic memory allocation) -

Change char n to char n[m] directly, no need for pointers

Another solution that works is (with dynamic memory allocation) -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int m = 5;
    printf("Id number of employee 1:\n");
    printf("Enter your id number length\n");
    scanf("%d", &m);
    char x[m];
    char *n = x;
    n=(char*)malloc(sizeof(char)*m);
    printf("Enter your id number:\n");
    scanf("%s", n);
    printf("The id number of employee 1 is %s\n", n);
    free(n);
    return 0;
}
NIKITA RATH
  • 354
  • 1
  • 3
  • 12