Thread: 'unit8_t' undeclared (first use in this function)

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    'unit8_t' undeclared (first use in this function)

    I get error 'unit8_t' undeclared (first use in this function)

    Code:
    #include<stdio.h>
    #include<stdint.h>
    
    
    int main ()
    
    
    {
        unit8_t int var = 360;
      
        printf("%d", var);
        
        return 0;    
    }
    c - Message "unknown type name 'uint8_t'" in MinGW - Stack Overflow

    I have GCC compiler. I tried all possible idea given in above link but no luck. any help would apricate

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You have a typo. It is uint8_t not unit8_t.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by hamster_nz View Post
    You have a typo. It is uint8_t not unit8_t.
    Thank you
    Code:
    #include<stdio.h>
    #include<stdint.h>
    
    
    int main ()
    {
        uint8_t  var = 60;
      
        printf("%d", var);
        
        return 0;    
    }
    60

    The size of variable var is fixed one byte. It can store integer value from 0-255

    I am surprised when I ran below code
    Code:
    #include<stdio.h>
    #include<stdint.h>
    
    int main ()
    {
        uint8_t  var = 259;
      
        printf("%d", var);
        
        return 0;    
    }
    When code compile I get warning and when It execute I get output 3
    gcc -o hello hello.c
    warning: large integer implicitly truncated to unsigned type [-Woverflow]
    uint8_t var = 259;
    ^~~
    hello
    3

    I don't understand why I get output 3 I should get error because I am trying to store value of integer that need large space
    Last edited by Rahul11; 08-29-2021 at 07:03 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Rahul11 View Post
    Thank you
    Code:
    #include<stdio.h>
    #include<stdint.h>
    
    
    int main ()
    {
        uint8_t  var = 60;
      
        printf("%d", var);
        
        return 0;    
    }
    60

    The size of variable var is fixed one byte. It can store integer value from 0-255

    I am surprised when I ran below code
    Code:
    #include<stdio.h>
    #include<stdint.h>
    
    int main ()
    {
        uint8_t  var = 259;
      
        printf("%d", var);
        
        return 0;    
    }
    When code compile I get warning and when It execute I get output 3
    gcc -o hello hello.c
    warning: large integer implicitly truncated to unsigned type [-Woverflow]
    uint8_t var = 259;
    ^~~
    hello
    3

    I don't understand why I get output 3 I should get error because I am trying to store value of integer that need large space
    If you assign a value to a variable larger than the maximum value it can hold, it rolls over. Basically it does a modulo, 259%256 == 3

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You are seeing 3 because overflows are not errors.

    Think of it like being 'clock' arithmetic, where 11:00 plus 3 hours equals 2:00.

    For 8-bit unsigned numbers 0 follows after 255.

  6. #6
    Registered User
    Join Date
    May 2021
    Posts
    66
    why code doesn't get input character

    Code:
    #include<stdio.h>
    
    int main ()
    {
       int X; char Y; float Z;
       
       printf("\n Enter any digit : ");
       scanf("%d", &X);
       printf("\n Entered digit = %d \n", X);
       
       printf(" \n Enter any character : ");
       scanf("%c",&Y);
       printf("\n Entered character = %c ", Y);
          
        return 0;    
    }
    Enter any digit : 34


    Entered digit = 34


    Enter any character :
    Entered character =



    Following code work perfectly

    Code:
    #include<stdio.h>
    
    int main ()
    {
       int X; char Y; float Z;
       
       printf(" \n Enter any character : ");
       scanf("%c",&Y);
       printf("\n Entered character = %c ", Y);
       
       printf("\n Enter any digit : ");
       scanf("%d", &X);
       printf("\n Entered digit = %d \n", X);
          
        return 0;    
    }
    Enter any character : e


    Entered character = e
    Enter any digit : 34


    Entered digit = 34


    When I have two input, integer and character and if I take first input integer then take character code is getting failed

    When I take first input character then take integer code is working fine

    Can anyone explain to me what's happening ?
    Last edited by Rahul11; 08-29-2021 at 09:19 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The char isn't failing.
    In the former case, the char you're getting is the newline left over from reading the input.

    If you made your printf this, you would see that it wasn't just any old character, or that the input was skipped.
    printf("\n Entered character = >>%c<< ", Y);
    Your screen would show
    Entered character = >>
    <<


    The reason for this is that scanf is not a greedy input stream. It uses only the minimum number of characters to satisfy the current conversion format (say a %d).
    The newbie trap is that this leaves the newline for the next input method to deal with.

    For most other conversions (say another %d), the "skip leading whitespace" comfortably deals with this newline.

    But %c is different, it unconditionally takes the next character.

    You can make scanf skip whitespace by adding a space to the format string, like so.
    Code:
    scanf(" %c",&Y); // leading space in format string
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undeclared first use of function
    By TheXx11 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2010, 01:15 AM
  2. undeclared (first use in this function)
    By chocolatecake in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 09:07 AM
  3. `INPUT' undeclared (first use in this function)|
    By Rocha in forum C Programming
    Replies: 8
    Last Post: 11-21-2008, 09:36 PM
  4. 'NULL' undeclared (first use this function)
    By sirconnorstack in forum C++ Programming
    Replies: 32
    Last Post: 06-21-2008, 04:35 AM
  5. undeclared (first use this function)
    By SC_Gordy in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2005, 05:53 PM

Tags for this Thread