screwed up programmer

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Can anyone tell me what I'm doing here.

#include <stdio.h>
#include "p.h"

extern int show(const char *p){
printf(%c,p);}

Compile into p.o

p.h says
#include <stdio.h>
int show(const char *p);

prog.c says

#include <stdio.h>
#include "p.h"

int main(){
show("hello");}
compile gcc prog.c -o prog

This is all f*cked up. What's wrong with that printf? Go ahead and laugh
and throw tomatoes.

Bill
 
J

Jack Klein

Can anyone tell me what I'm doing here.

#include <stdio.h>
#include "p.h"

extern int show(const char *p){
printf(%c,p);}

The call to printf() above is certainly wrong. The first argument to
printf() must be a string, perhaps you meant "%c"?

If that's the case the second argument must be a char, which p is not.
p is a pointer to char, which happens to point to the string literal
"hello".

If you wanted to print the first character of that string, you need to
write:

printf("%c", *p);

....*p is a single char, what "%c" needs.

Two other things about your function show...

1. Putting extern on a function declaration, prototype, or definition
is unnecessary and redundant. Unless it contains the static keyword,
it has external linkage.

2. You state that the function will return an int, then you don't
return anything.
Compile into p.o

p.h says
#include <stdio.h>
int show(const char *p);

prog.c says

#include <stdio.h>
#include "p.h"

int main(){
show("hello");}
compile gcc prog.c -o prog

This is all f*cked up. What's wrong with that printf? Go ahead and laugh
and throw tomatoes.

Bill

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Richard Heathfield

Bill said:
Can anyone tell me what I'm doing here.

#include <stdio.h>
#include "p.h"
extern int show(const char *p){
printf(%c,p);}

Syntax error. printf requires a format string, and %c isn't one. "%c", on
the other hand, is one. Your compiler should have told you this. Note that
"%c" would match with a character, not a character string. Perhaps you mean
"%s", or perhaps you mean *p - it's impossible to tell from your code.
 
B

Bill Cunningham

Note that
"%c" would match with a character, not a character string. Perhaps you mean
"%s", or perhaps you mean *p - it's impossible to tell from your code.
So these 2 prototypes would be alright?

printf("%c",*p);
or printf("%s",p);
casting and precedence is my worst enemy in C. When to cast and I seen code
with bunches of parenthesis which is confusing to the beginner. Me anyway.

Bill
 
S

Serve La

Bill Cunningham said:
Note that
So these 2 prototypes would be alright?

printf("%c",*p);
or printf("%s",p);

Yes, the first prints the first char of where p is pointing to, the second
prints all characters until a '\0' is encountered.
 
R

Richard Heathfield

Bill said:
Note that
So these 2 prototypes would be alright?

printf("%c",*p);
or printf("%s",p);

Those are not prototypes. They are function calls. The first one prints the
first character in the string to which p points. The second prints the
whole string to which p points.
casting and precedence is my worst enemy in C.

I see no casts here, nor do I see any need for them.
When to cast and I seen
code with bunches of parenthesis which is confusing to the beginner. Me
anyway.

Casting is usually wrong, and the places where it's right aren't the places
you'd expect. In good C code, casts are very rare.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top