Thread: Reading const char array via scanf

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    14

    Reading const char array via scanf

    Hello, I am trying to read an input which has "cont char" type. Once I enter the first input, it gives the error at the end of the thread below my code. What am I missing here?

    Code:
      #include <stdio.h>
        int main (void)
        {
            int d;
            int ChnCount =4;
                    const char physicalChannel[ChnCount];
            for (d=0; d<ChnCount; d++)
              {
            printf(" Enter the ID no. (0 to 7) for channel %d of %d \n: ",d+1, ChnCount);   /*------------*/
               scanf("Dev1/ai %s", &physicalChannel[d]); 
            }
            for (d=0; d<ChnCount; d++)
            {
            printf ("%s\n", physicalChannel[d]);
            }
        return 0;
        }
    I see this on the screen after entering the first input. The errors are:
    1. it doesnt ask me for channels 2,3 and 4.
    2. it doesnt print physicalChannel[d].

    Code:
     Enter the ID no. (0 to 7) for channel 1 of 4: 1
     Enter the ID no. (0 to 7) for channel 2 of 4:  Enter the ID no. (0 to 7) for channel 3 of 4:  Enter the ID no. (0 to 7) for channel 4 of 4:
    Cheers!
    Last edited by cyln; 10-09-2017 at 12:41 PM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    what loops does what? inner loops runs how many times before the outer loop runs again?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int a,b, something;
    
     for ( a = 0; a<4; a++)
    {
       for (b = 0; b < 1; b++)
        {
          printf("enter something\n");
          scanf("%d", &something);
        }
       printf("something = %d\n", something);
    }
    
    return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    char a1, a2, a3, a4;
    printf("enter 4 somethings\n");
    scanf("%c %c %c %c", &a1,&a2,&a3,&a4);
    printf("something = %c  %c %c %c\n",a1,a2,a3,a4);
    return 0;
    }
    Last edited by userxbw; 10-09-2017 at 01:18 PM.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    14
    Sorry this doesnt answer my question

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by cyln View Post
    Sorry this doesn't answer my question
    Code:
    char d[3];
    how every you write that to give it is length. will only be good for one word size of length - 1 for end char '\0' .

    your scanf is only good for one input it will not stop and wait for another one. chars are tricky, you cannot put 4 separate answers in that one char like that. it is length of a word.
    Code:
    printf(" inpout 4 chanl\n")
     scanf("%s", ch1);
     scanf("%s", ch2);
     scanf("%s", ch3);
     scanf("%s", ch4);
    I see your logic but it is not going to work like that.
    loops are pretty much out.
    that is why I showed you how to get 4 off the command line off the bat or write it 4 times.

    Code:
    #include <stdio.h>
      int main (void)
      {
         
      char d[] = "hello";
       printf ("%s\n", d); 
       printf("%c\n",d[2]);
     
      return 0;
      }
    output
    Code:
    [userx@void bin]$ gcc loopy.c
    [userx@void bin]$ ./a.out
    hello
    l
    C Strings (Arrays vs. Pointers)


    Last edited by userxbw; 10-09-2017 at 02:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare unsigned char array with const char*
    By Lazar in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2015, 08:36 AM
  2. const char* to char array.
    By kevinawad in forum C++ Programming
    Replies: 13
    Last Post: 08-05-2008, 02:25 PM
  3. Reading in char for float with scanf
    By ramparts in forum C Programming
    Replies: 3
    Last Post: 11-05-2006, 01:05 AM
  4. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM
  5. How do I cast a char Array[] to a const char* ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 08:43 AM

Tags for this Thread