Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
fredsmith

Begginer with C, Help!

Recommended Posts

Posted · Original PosterOP

Hello,

 

So I am trying to make a program that simply asks the user for their name, then it greets them.

I am not sure what data type to assign to the variable.  Please see below.

#include <stdio.h>main(){????? name;printf("What is your name?");scanf("?????%", &name);printf("Welcome, ?????%", name);}

Appreciate your help!

 

Thanks.

Link to post
Share on other sites

char


"You have got to be the biggest asshole on this forum..."

-GingerbreadPK

sudo rm -rf /

Link to post
Share on other sites

I already tried that, it will only output the first character of the name, not the whole name.

Ah sorry. Forgot it's C and not C++. No string operators in C! You need a char array, like this:

unsigned char Name []

"You have got to be the biggest asshole on this forum..."

-GingerbreadPK

sudo rm -rf /

Link to post
Share on other sites

You have to create an array. In this case, I believe it would be name...

 

char name[50];

 

And the scanf should be like this:

 

scanf("%s", name);

 

Same thing with the printf.

 

Notice there's no '&' in name since it's an array. If you don't know what an array is then I suggest going back to your teacher to ask how to make your program work since I don't see another path for this.

 

Hope it helps!

Edited by Lightz
Link to post
Share on other sites
Posted · Original PosterOP

 

Ah sorry. Forgot it's C and not C++. No string operators in C! You need a char array, like this:

unsigned char Name []

 

 

You have to create an array. In this case, I believe it would be name...

 

char name[50];

 

And the scanf should be like this:

 

scanf("%s", name);

 

Same thing with the printf.

 

Notice there's no '&' in name since it's an array. If you don't know what an array is then I suggest going back to your teacher to ask how to make your program work since I don't see another path for this.

 

Hope it helps!

 

 

Thanks! appreciate your help!

Link to post
Share on other sites

You have to create an array. In this case, I believe it would be name...

 

char name[50];

 

And the scanf should be like this:

 

scanf("%s", name);

 

Same thing with the printf.

 

Notice there's no '&' in name since it's an array. If you don't know what an array is then I suggest going back to your teacher to ask how to make your program work since I don't see another path for this.

 

Hope it helps!

Couldn't he just do

unsigned char []

and not have to worry about length?


"You have got to be the biggest asshole on this forum..."

-GingerbreadPK

sudo rm -rf /

Link to post
Share on other sites

Couldn't he just do

unsigned char []

and not have to worry about length?

If he had used unsigned char [] then that means he would have to declare the name as well.

 

For example...

 

unsigned char name[]={"Joseph"};

 

My teacher always used to say you should ALWAYS declare array sizes UNLESS you declare it's content before hand, or unless you are working with pointers.

Edited by Lightz
Link to post
Share on other sites

-snip-

I've been spending too much time in scripting languages with type prediction. The information is all getting confuzzle'd together in my head. (oh Ruby, how I miss your ducks)


"You have got to be the biggest asshole on this forum..."

-GingerbreadPK

sudo rm -rf /

Link to post
Share on other sites

Couldn't he just do

unsigned char []

and not have to worry about length?

 

In C, you should always declare the array size. If you don't, you could run into some nasty seg faults errors that are not easy to debug. Another note for OP would be to always declare your char array to be a size of one larger than what you need because C always ends it's strings with a null terminator ('/0'). 

For Example if I declare a string as:

char string[] = {'t','e','s','t'}

when you try to print it, it will look something like this: testjhfdkhafhagfdhjkasd. It will print out garbage until it finds a null terminator. And this could also result in another seg fault error.

Link to post
Share on other sites

In C, you should always declare the array size. If you don't, you could run into some nasty seg faults errors that are not easy to debug. Another note for OP would be to always declare your char array to be a size of one larger than what you need because C always ends it's strings with a null terminator ('/0'). 

For Example if I declare a string as:

char string[] = {'t','e','s','t'}

when you try to print it, it will look something like this: testjhfdkhafhagfdhjkasd. It will print out garbage until it finds a null terminator. And this could also result in another seg fault error.

I should really just stop trying to write C...

 

I need to study it again I think. It's been too long.


"You have got to be the biggest asshole on this forum..."

-GingerbreadPK

sudo rm -rf /

Link to post
Share on other sites

My teacher always used to say you should ALWAYS declare array sizes UNLESS you declare it's content before hand, or unless you are working with pointers.

A fixed array length also has issues. If the size is discovered, using clever inputs, it can become a security issue (possibly fixable). 


Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to post
Share on other sites

A fixed array length also has issues. If the size is discovered, using clever inputs, it can become a security issue (possibly fixable). 

that shouldn't be a "fixed array length" issue, but just a lack of control over the input

wether you use a fixed length array or a dinamically sized one, in the moment you call scanf() the array will be capable of storing a fixed amount of data, but the input could possibly go further

so the problem is not the data structure, but the method to access user input, and that's why scanf should be avoided for code that needs to be foolproof/safe (edit: actually there should be a format specifier to tell scanf the buffer length, but it's usually better to just use other functions)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

Newegg

×