Hello, Everybody.....

B

blackneos940

Guest
I'm back..... Despite recent events, I still Love Programming..... :) I, well....... I started making "Code Katas", based on an idea someone had, I believe..... It's where I practice, well, Coding..... :) But, in a C Code Kata, I seem to have run into a problem..... :( When I compile this
Code:
#include <stdio.h>

#include <string.h>

#define TEST "62.4"

int main(void)

{
  int x;

  float y;

  char z[20];

  scanf ("%s", &x);

  scanf ("%f", &y);

  scanf ("%c", &z);

  printf ("%s", x);

  printf ("%f", y);

  printf ("%c", z);

  return 0;

}

It throws these errors:

Datatypes.c: In function ‘main’:
Datatypes.c:16:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
scanf ("%s", &x);
^
Datatypes.c:20:10: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
scanf ("%c", &z);
^
Datatypes.c:22:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf ("%s", x);
^
Datatypes.c:26:11: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
printf ("%c", z);

I wonder what it could all mean.....? :( Thanks for any help guys....... :) And someday, I'm gonna start a Project..... It will be in Memory of my Dad.... :) You BET it'll be Open Source....... :) :D
 


Many problems here:

All your variables are uninitialized. Since they are local variables, they pick up some garbage values that exist in memory at the location of the variables. Always initialize all your local variables. In this case, you are attempting to input values using scanf().

Variable names should be more meaningful. For instance, "int count;" rather than "int x", except for loop variables.

Code:
#define TEST "62.4"
is never used here. Not an error. You might have meant it to be used later.

You should prompt the user what he/she should enter at the prompt.

Always check the return value from scanf() to insure that you actually got successfully input a value. If not, then you would need to get rid of the remainder of the characters left in the input buffer. See how to "Flush the input buffer"!

Code:
 scanf ("%s", &x);
"%s" in scanf() or printf() both require the address of a character array. You are passing the address of an int 'x' to scanf(). Passing 'z' here would work.

Properly using the "%s" format specifier in scanf(), will only get a "Word" not the full line. For example if the text entered is "One two three", only "One" would be input into the array.

Code:
 printf ("%s", x);
You are passing an int instead of the address of a char, or the char array name. 'z' would be appropriate here.

Code:
scanf ("%c", &z);
"%c" requires the address of a char variable. In your case, you are actually passing a pointer to a pointer to a char. Using scanf() I would recommend only using a char variable, although using getchar(), I would use an int variable instead. See:
Code:
$ man getchar

Code:
 printf ("%c", z);
You are passing the address of a char where you actually need a char here. Passing "z[0]" or a char variable here would be appropriate
 
Well, ok..... I made some tweaks to this Code....... :) For some reason, what works in another C File does not work in this Exercise File..... :\ Also, this is something personal, though it IS FOSS..... :) Basically, that's why it was a little vague..... :D Also, this is the Code that seemed to Compile correctly..... :)
Code:
#include <stdio.h>

#include <string.h>

#define TEST "62.4"

int main(void)

{
  int x;

  float y;

  char z;

  scanf ("%i", &x);

  scanf ("%f", &y);

  scanf ("%c", &z);

  printf ("%i", x);

  printf ("%f", y);

  printf ("%c", z);

  return 0;

}

So anyway..... This whole "Format" thing is confusing, but..... I THINK I'm starting to understand things..... :\ Anyway, that #define Preprocessor was meant to be used later on in this (unfinished) Code Kata..... :D You're right.... :D So, whatd'ya think now.....? :3
 

Members online


Latest posts

Top