Thread: scanf acting weird

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    5

    scanf acting weird

    Code:
    #include<stdio.h>main()
    {
     int a,b,g,ind;
    char c;
     a=0;
    
    
     g=0;
     ind=0;
       for(g=0;g<5;g++)
         {
          printf("insert c\n");
          scanf("%c", &c);
    
    
           if ( c=='1' ) {
    
    
              a=a*10+  (int) (c-'0');
              ind=1;
                        } 
    
    
           if (ind==1 && c!='1' )
                     {
    
    
             printf("%d\n" ,a); 
             ind=0;
            a=0;}
     
          }
    }
    so thats my code and this is what i get from gdb
    Code:
    (gdb) break 1
    Breakpoint 1 at 0x40065e: file kol1.c, line 1.
    (gdb) run
    Starting program: /home/djordje/Desktop/programiranje/kol1 
    
    
    Breakpoint 1, main () at kol1.c:3
    3       {
    (gdb) display c
    1: c = 0 '\000'
    (gdb) next
    6        a=0;
    1: c = 0 '\000'
    (gdb) next
    8        g=0;
    1: c = 0 '\000'
    (gdb) next
    9        ind=0;
    1: c = 0 '\000'
    (gdb) next
    10         for(g=0;g<5;g++)
    1: c = 0 '\000'
    (gdb) next
    12            printf("insert c\n");
    1: c = 0 '\000'
    (gdb) next
    insert c
    13            scanf("%c", &c);
    1: c = 0 '\000'
    (gdb) next
    1
    15             if ( c=='1' ) {
    1: c = 49 '1'
    (gdb) next
    17                a=a*10+  (int) (c-'0');
    1: c = 49 '1'
    (gdb) next
    18                ind=1;
    1: c = 49 '1'
    (gdb) next
    21             if (ind==1 && c!='1' )
    1: c = 49 '1'
    (gdb) next
    10         for(g=0;g<5;g++)
    1: c = 49 '1'
    (gdb) next
    12            printf("insert c\n");
    1: c = 49 '1'
    (gdb) next
    insert c
    13            scanf("%c", &c);
    1: c = 49 '1'
    (gdb) next
    15             if ( c=='1' ) {
    1: c = 10 '\n'
    commands i used in gdb are bold, the output is standard and the red lines are where the strange thing happens so as you see after the scanf gdb doesnt let me insert my own value for c instead it assigns the default( or is it default) value to c, any help is apriciated thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    %c reads every character, including the newline you pressed at the end of input.
    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
    Mar 2015
    Posts
    5
    i see, it works when i enter all of my input when i first read c, but is there a way to read the value of c every time the loop repeats itself?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take a look at why scanf isn't the best and also how to flush the input buffer. Also, if you are learning C then you should become best friend's with Comp.Lang.C FAQ.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Values acting weird.
    By torqu3e in forum C Programming
    Replies: 9
    Last Post: 12-19-2006, 11:12 AM
  2. atof() acting weird?
    By Mellowz in forum C Programming
    Replies: 4
    Last Post: 07-09-2006, 03:48 AM
  3. DirectX 9 materials acting weird
    By Rune Hunter in forum Game Programming
    Replies: 7
    Last Post: 12-27-2005, 12:07 AM
  4. char[] acting weird
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2003, 06:45 PM
  5. n00b needs help with api graphics acting very weird
    By CheeseWeaver in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 03:15 PM