Thread: second print statement goes wrong

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    72

    second print statement goes wrong

    I am looking help to perform following operation

    Data_Byte = 0000 0000 | 0000 0001 = 0000 0001 << 1 = 000 00010 = 2

    Data_Byte = 000 00010 | 0000 0001 = 0000 0011 << 1 = 000 00110 = 6

    User can modify LSB bit

    My last print statement should print value 6 as expected. I don't understand why I don't get 6. What I need to print 6 ?

    Code:
    #include<stdio.h>
    
    #include<stdint.h>
    
    int main ()
    {
        uint8_t Data_Byte = 0;
        uint8_t LSB_BIT;
        
        printf("Set/Reset LSB Enter 1 /0 : ");
        scanf("%d", &LSB_BIT );
     
        Data_Byte =  ( Data_Byte | LSB_BIT );
        printf(" %d\n",  Data_Byte );
     
        Data_Byte = Data_Byte << 1;
        printf("Byte =  %d \n",  Data_Byte );
        
        printf("Set/Reset LSB Enter 1 /0 : ");
        scanf("%d", &LSB_BIT );
     
        Data_Byte =  ( Data_Byte | LSB_BIT );
        printf("%d\n",  Data_Byte );
     
        Data_Byte = Data_Byte << 1;
        printf("Byte =  %d \n",  Data_Byte );
     
       return 0;
    }
    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2
    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,123
    Well here's the first problem.
    Code:
    foo.c: In function ‘main’:
    foo.c:11:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘uint8_t *’ {aka ‘unsigned char *’} [-Wformat=]
       11 |     scanf("%d", &LSB_BIT );
          |            ~^   ~~~~~~~~
          |             |   |
          |             |   uint8_t * {aka unsigned char *}
          |             int *
          |            %hhd
    foo.c:20:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘uint8_t *’ {aka ‘unsigned char *’} [-Wformat=]
       20 |     scanf("%d", &LSB_BIT );
          |            ~^   ~~~~~~~~
          |             |   |
          |             |   uint8_t * {aka unsigned char *}
          |             int *
          |            %hhd
    Your scanf with %d is going to trash memory outside of the intended variable.

    Using the correct scanf format of %hhd, I get
    Code:
    $ gcc foo.c
    $ ./a.out 
    Set/Reset LSB Enter 1 /0 : 1
     1
    Byte =  2 
    Set/Reset LSB Enter 1 /0 : 1
    3
    Byte =  6
    Enable as many warnings as you can on your compiler, and use a compiler with lots of diagnostics.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    Well here's the first problem.

    Using the correct scanf format of %hhd, I get
    .
    I don't get correct value even after using %hhd,

    Code:
     #include<stdio.h>
    
    #include<stdint.h>
    
    
    int main ()
    {
        uint8_t Data_Byte = 0;
        uint8_t LSB_BIT;
        
        printf("Set/Reset LSB Enter 1 /0 : ");
        scanf("%hhd", &LSB_BIT );
     
        Data_Byte =  ( Data_Byte | LSB_BIT );
        printf(" %d\n",  Data_Byte );
     
        Data_Byte = Data_Byte << 1;
        printf("Byte =  %d \n",  Data_Byte );
        
        printf("Set/Reset LSB Enter 1 /0 : ");
        scanf("%hhd", &LSB_BIT );
     
        Data_Byte =  ( Data_Byte | LSB_BIT );
        printf("%d\n",  Data_Byte );
     
        Data_Byte = Data_Byte << 1;
        printf("Byte =  %d \n",  Data_Byte );
     
       return 0;
    }
    warnings

    hello.c: In function 'main':
    hello.c:11:14: warning: unknown conversion type character 'h' in format [-Wformat=]
    scanf("%hhd", &LSB_BIT );
    ^
    hello.c:11:11: warning: too many arguments for format [-Wformat-extra-args]
    scanf("%hhd", &LSB_BIT );
    ^~~~~~
    hello.c:20:14: warning: unknown conversion type character 'h' in format [-Wformat=]
    scanf("%hhd", &LSB_BIT );
    ^
    hello.c:20:11: warning: too many arguments for format [-Wformat-extra-args]
    scanf("%hhd", &LSB_BIT );

    output

    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2
    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,123
    I thought you were using GCC.

    Which version?

    I have
    Code:
    $ gcc --version
    gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Maybe start with
    int Data_Byte = 0;
    int LSB_BIT;

    and regular vanilla %d to make sure it's OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by Salem View Post
    I thought you were using GCC.

    Which version?

    I have
    [code]
    $ gcc --version
    gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    .
    I have
    Code:
     gcc --versiongcc (MinGW.org GCC-6.3.0-1) 6.3.0
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    963
    inttypes.h header gives us the proper format conversion strings:
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main ()
    {
      uint8_t Data_Byte = 0;
      uint8_t LSB_BIT;
    
      printf ( "Set/Reset LSB Enter 1 /0 : " );
      scanf ( "%" SCNu8, &LSB_BIT );
    
      Data_Byte =  ( Data_Byte | LSB_BIT );
      printf ( "%" PRIu8 "\n",  Data_Byte );
    
      Data_Byte = Data_Byte << 1;
      printf ( "Byte =  %" PRIu8 "\n",  Data_Byte );
    
      printf ( "Set/Reset LSB Enter 1 /0 : " );
      scanf ( "%" SCNu8, &LSB_BIT );
    
      Data_Byte =  ( Data_Byte | LSB_BIT );
      printf ( "%" PRIu8 "\n",  Data_Byte );
    
      Data_Byte = Data_Byte << 1;
      printf ( "Byte =  %" PRIu8 "\n",  Data_Byte );
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    72
    Quote Originally Posted by flp1969 View Post
    inttypes.h header gives us the proper format conversion strings:
    I ran your code but I get same result as previous

    Code:
      hello.c: In function 'main':hello.c:11:11: warning: unknown conversion type character 'h' in format [-Wformat=]
       scanf ( "%" SCNu8, &LSB_BIT );
               ^~~
    hello.c:11:11: warning: too many arguments for format [-Wformat-extra-args]
    hello.c:20:11: warning: unknown conversion type character 'h' in format [-Wformat=]
       scanf ( "%" SCNu8, &LSB_BIT );
               ^~~
    hello.c:20:11: warning: too many arguments for format [-Wformat-extra-args]
    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2
    Set/Reset LSB Enter 1 /0 : 1
    1
    Byte = 2

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Since you're using MinGW, the issue here might be that the Visual C runtime is used, or something like that. It is why instead of the standard %lld you may have to use %I64d or something like that to print long long, and something like that may be coming into play here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    817
    I don't see any strong reason to use uint8_t for LSB_BIT. You could change it to unsigned and then use %u for the scanf format. You wouldn't even have to make any other changes in your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why the result of print statement for a.x isn''t 1?
    By skyr6546 in forum C Programming
    Replies: 4
    Last Post: 04-03-2020, 11:54 PM
  2. Why does this statement print a 0?
    By lilywest in forum C Programming
    Replies: 3
    Last Post: 11-22-2013, 10:29 AM
  3. Print statement won't print
    By Sakari in forum C Programming
    Replies: 17
    Last Post: 06-20-2012, 06:41 PM
  4. Having troubles with a simple print statement.
    By Tails in forum C Programming
    Replies: 4
    Last Post: 09-22-2011, 06:14 PM
  5. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM

Tags for this Thread