tags:

views:

152

answers:

5
#include <stdio.h> 

void main(void) 
{ 
  int i; 
  char c; 
  for (i=0;i <5;i++) 
  { 
    scanf("%d",&c); 
    printf("i=%d\r\n",i); 
  } 
  printf("The loop is finished!\r\n"); 
}

Especially the scanf function... Can you give detailed explanation?

A: 

What answer do you want?

Tony Lambert
Fair question - but probably best as a comment to the question than as an answer.
Jonathan Leffler
+4  A: 

The types you are using in the scanf() call do not match: the type in the format string is int (which expects a pointer to int argument) and the following argument is a pointer to char (which should match a "%c" conversion specification).

What do you want your code to do?

  • Do you want to read 5 characters from the input, one each time through the loop?
    What do you want to do with those characters once they have been read?

  • Do you want to read 5 characters once and display them one by one?

Get your compiler to show as many warnings as it can. The compiler can tell you about these kinds of type mismatch.

pmg
A: 

When you do the scanf using %d it will take sizeof(int) bytes from address &c at &c you only have a single sizeof(char) typically one byte, so it will take the other following (3?) bytes as well to produce some invalid int value.

Use:

   int numOfValuesScanned = Scanf("%c",&c);
Tony Lambert
A: 

I am not sure what you were hoping to see but, I will take a guess from your code. There are a couple of problems here. The fist is that your printf statement is printing out the loop counter i not the variable c, so it is only going to show the i being iterated. The scanf only removes one character at a time from the input buffer(there are a couple of ways to solve this, I only gave one). Also, the format specifier is for an integer, this could be 16 or 32 bits depending on the operating system and the compiler settings. You are storing it to a character which could be 8 bits (unless you have a risc chip then everything is the register size). This will overflow the variable c in most cases. Change "%d" to "%c" in the scanf and change your printf statement to ("%d = %c\r\n",i,c), then add the line getchar(); after your printf statement and after the last printf statement showing your code has completed. This will clear out the newline in the input buffer and I think you will get what you are after.

everything is register sized?? huh?
Tony Lambert
+2  A: 

In the future, please tell us what you expect to see, not just "this isn't what I want." Having said that, let's step through your code.

#include <stdio.h> 
void main(void)

The language standard mandates that main() return an int. This may not prevent your code from compiling, but more and more compilers are starting to issue warnings about main() not being typed int, and there are some (uncommon) platforms where using void main() may result in runtime problems. Unless your compiler documentation specifically says, "yes, we support void main()," use int main(void) or int main(int argc, char **argv).

{   
    int i;   
    char c;   
    for (i=0;i <5;i++)   
    {     
        scanf("%d",&c);

You're confusing scanf(): a %d conversion specifier expects its corresponding argument to be a pointer to int, but you're passing a pointer to char. Either change the type of c to an int (recommended), or use a %c conversion specifier (but then you have to worry about handling whitespace, so it's probably better to just type c as int).

        printf("i=%d\r\n",i);

Why are you bothering to read c if you're not going to print it out? Secondly, \n should be sufficient to write a newline; you don't need the \r.

    }   
    printf("The loop is finished!\r\n");

Same comment as above re: \r.

}
John Bode