Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
does this work for anyone?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
rommel
Veteran
Veteran


Joined: 19 Apr 2002
Posts: 1140
Location: Williamsburg Virginia

PostPosted: Sun Apr 21, 2002 2:42 am    Post subject: does this work for anyone? Reply with quote

#include <stdio.h>

main()
{
char sting[20];
int variable;
puts("please enter you name here:\n");
scanf("%s,string\n");
printf("your name is %s,string\n");
puts("please enter your phone number here:\n");
scanf("%d,&variable\n");
printf("your phone number is %d,&variable\n");
return(0);
}

i name the file namno.c then run 'gcc namno.c -o namno' and it compiles giving me only a no new line at the end warning...then when i ./namno it asks me to enter my name then i do adn hit enter it gives a segmentation fault.
just wondering if its me(probably) or there is something wrong with konsole or gcc or something.

thanks

ciao
Back to top
View user's profile Send private message
rommel
Veteran
Veteran


Joined: 19 Apr 2002
Posts: 1140
Location: Williamsburg Virginia

PostPosted: Sun Apr 21, 2002 3:04 am    Post subject: Reply with quote

#include <stdio.h>

main()
{
char string[20];
int variable;
puts("please enter you name here:\n");
scanf("%s",string);
printf("your name is %s",string);
puts("please enter your phone number here:\n");
scanf("%d",&variable);
printf("your phone number is %d",&variable);
return(0);
}

ok so this works kinda but the lines tend to run together and no matter what i type as a phone number i get the same return -1073744196?

sorry for the noob ignorant question but well i just dont see it yet
Back to top
View user's profile Send private message
fghellar
Bodhisattva
Bodhisattva


Joined: 10 Apr 2002
Posts: 856
Location: Porto Alegre, BR

PostPosted: Sun Apr 21, 2002 4:38 am    Post subject: Reply with quote

Try this:

Code:
#include <stdio.h>

main()
{
char string[20];
int variable;
puts("please enter you name here:\n");
scanf("%s", string);
printf("\nyour name is %s\n", string);
puts("please enter your phone number here:\n");
scanf("%d", &variable);
printf("\nyour phone number is %d", variable);
return(0);
}


(I'm not in linux right now and I didn't compile it, so I don't know if it works...) :)
_________________
| www.gentoo.org | www.tldp.org | www.google.com |
Back to top
View user's profile Send private message
rommel
Veteran
Veteran


Joined: 19 Apr 2002
Posts: 1140
Location: Williamsburg Virginia

PostPosted: Sun Apr 21, 2002 5:58 am    Post subject: Reply with quote

#include <stdio.h>

main()
{
char string[20];
int variable;
puts("please enter you name here:\n");
scanf("%s", string);
printf("\nyour name is %s\n", string);
puts("please enter your phone number here:\n");
scanf("%d", &variable);
printf("\nyour phone number is %d\n", variable);
return(0);
}

aaahhhh your the man, thanks...you left out one \n but in your case it was probably just an oversite...lol....i on the otherhand had no clue...thanks again
Back to top
View user's profile Send private message
Da Masta
n00b
n00b


Joined: 28 Apr 2002
Posts: 3

PostPosted: Mon Apr 29, 2002 11:51 pm    Post subject: Pointers and the & operator... Reply with quote

For future reference, I think the key thing you have to remember is the way printf and scanf work.

Consider these lines:
scanf("%d",<<variable here>>);
printf("%d",<<variable here>>);

Scanf is scanning the input buffer for a value and storing it to <<variable>>, thus it needs the address of the variable. Appending a & to the name of the variable returns the address of the variable in memory, so that scanf can place the value in the proper address.

On the other hand, printf simply wants to print the value in <<variable>> so there is no need to pass the address, as passing the name of the variable in the function call passes the value of the variable to the printf.

In the case of strings howerver, there is a slight difference. In a character string named str, the name str actually represents &str[0] which, as we now know, is the address of the first member of the string array. the %s in printf is taken as a pointer to the string, and thus printf goes to that address in memory and prints until the end of the string. This time, scanf is actually doing the same thing, since in order to write to the string, it needs teh address of the array, and as we know, str supplies just that.
Back to top
View user's profile Send private message
gsfgf
Veteran
Veteran


Joined: 08 May 2002
Posts: 1266

PostPosted: Sun May 12, 2002 1:28 am    Post subject: Reply with quote

and that, boys and girls is why i use cin;cout
Back to top
View user's profile Send private message
AggieEmpeg
n00b
n00b


Joined: 16 Apr 2002
Posts: 63

PostPosted: Sun May 12, 2002 5:21 am    Post subject: Reply with quote

Yes but cin and cout are only C++ functions... if you have a plain jane C compiler, it won't know what the hell you are talking about. Of course, gcc can do both C and C++ even if you mix C code with C++ code, so maybe this is a moot point.
Back to top
View user's profile Send private message
fmalabre
Guru
Guru


Joined: 19 Jun 2002
Posts: 376
Location: Chicago

PostPosted: Thu Jun 27, 2002 9:12 pm    Post subject: Reply with quote

obviously if u mix c code with c++, it works, as every c++ compilers...

c++ if fully compatible with c
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum