1

I am trying to run this code which seems fine to me but the end results are not much convincing. Here's the code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    unsigned char nT,nF;    
    char extension[5];

    puts("Enter No. of Testcases & Faults");
    scanf("%d %d %s", &nT, &nF, extension);
    printf("%d %d\n",nT,nF);
    getch();
}

Below is the sample input:

Enter No. of Testcases & Faults
50 50 code

Below is the output:

0 50

Below is the Expected output:

50 50

Note: I am bounded to use unsigned char and cannot use unsigned int.

I am using DevCpp 4.9.9.2. Please help if anyone has got the solution or the reason for why this is happening.

Ankit kumar
  • 61
  • 1
  • 5
  • 5
    Undefined behavior for passing a pointer to the wrong type and of the wrong type to `scanf()`. – EOF Mar 04 '17 at 15:33

3 Answers3

3

Look into your scanf clause. You are passing two (pointers to) unsigned char variables to it, yet declare that you are reading two ints in your format specifier (%d specifier). Pass %hhu if you want to read unsigned char.

When you pass &extension to scanf there is no need to use ampersand. The name of the array gets converted to the pointer to its first element, so it's sufficient to pass just extension.

You are also using a very old IDE (Dev-C++), that is no longer developed. Probably the underlying compiler is dated as well. I advise to use newer tools, Code::Blocks is actively developed, quite newbie-friendly and similar to Dev-C++ to a certain extent.

However, on Windows platforms, the MSVCRT library does not support the %hhu format specifier, as has been noted in this question. MingGW compiler relies on this implementation, so it does not support %hhu on windows as well. Cygwin is a project that simulates Linux environment on Windows and is distributed with its own version of C library, that supports, among other things, %hhu. As a sidenote, you can configure Code::Blocks to work with Cygwin (as described here). I don't know if the same can be achieved with Dev-C++.

Community
  • 1
  • 1
lukeg
  • 4,189
  • 3
  • 19
  • 40
  • Using %hhu yields the same result as before. The problem is with compiler I suppose. What do you say? @lukeg – Ankit kumar Mar 04 '17 at 16:10
  • @Ankit Make sure your compiler version supports %hhu format specifier. I'd advise trying a newer one or trying to switch it into C99 mode. However, as you are on Windows, you might face another compatibility problem, see this question for details: http://stackoverflow.com/questions/15825254/why-is-scanfhhu-char-overwriting-other-variables-when-they-are-local – lukeg Mar 04 '17 at 16:23
  • I saw that question and I am facing the same problem. When I declare nT and nF as global variables, the output is as expected but the problem is with declaring them as local variables. This is a windows specific problem I suppose. Anyway thanks for the info :) @lukeg – Ankit kumar Mar 04 '17 at 17:08
  • @Ankit Yes, it's generally a Windows problem. I have just check that you can try using Cygwin (http://cygwin.org), which reimplements the standard Windows libraries and code runs fine there. I'll upate my answer. – lukeg Mar 04 '17 at 17:25
0
    #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    unsigned int nT,nF;    
    char extension[5];

    puts("Enter No. of Testcases & Faults");
    scanf("%d %d %s", &nT, &nF, extension);
    printf("%d %d\n",nT,nF);
    getch();
}
0
    #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    unsigned char nT,nF;    
    char extension[5];

    puts("Enter No. of Testcases & Faults");
    scanf("%hhu %hhu %s", &nT, &nF, extension);
    printf("%hhu %hhu\n",nT,nF);
   // getch();
}

You can see output on below image

enter image description here