Click to See Complete Forum and Search --> : learning C question


alshataan
12-30-2007, 01:35 PM
Hey guys, I'm trying to learn C and I'm doing it from a book. Also it is my first language.

So here's my newbie question: I'm trying to write a simple program which asks you to type your name. Then it prints "Hello <insert name> !"

Here's my code:

#include <stdio.h>
/*Asks for name then says 'hi'and reprints name*/
main()
{
double name, name2;

printf("Enter your name: \n");
while ((name = getchar()) != EOF){
printf("Hello", name);
}
return 0;
}

Of course I've tried numerous ways and typically I get the following output at best:

Enter your name:

I type Billy

Then it prints stuff like BHello iHello lHell lHello yHello

I can see that It takes a character, runs the loop, then gets the next and runs it. But I dont want that and if I change it I get other stuff like "Hello Hello Hello ?" (The question mark is surrounded by black and the actual mark is white inside. Or it just prints hello.

I tried storing the getchar() input into a variable then after the loop printing the variable..But then I get numbers.


Any suggestions?

Thanks!
~
~
~
~
~

con
12-30-2007, 03:16 PM
You must print 'Hello' before the loop. Hope this helps :D

phlipant
12-30-2007, 10:12 PM
Hey guys, I'm trying to learn C and I'm doing it from a book. Also it is my first language.

So here's my newbie question: I'm trying to write a simple program which asks you to type your name. Then it prints "Hello <insert name> !"

Here's my code:

#include <stdio.h>
/*Asks for name then says 'hi'and reprints name*/
main()
{
double name, name2;

printf("Enter your name: \n");
while ((name = getchar()) != EOF){
printf("Hello", name);
}
return 0;
}

Of course I've tried numerous ways and typically I get the following output at best:

Enter your name:

I type Billy

Then it prints stuff like BHello iHello lHell lHello yHello

I can see that It takes a character, runs the loop, then gets the next and runs it. But I dont want that and if I change it I get other stuff like "Hello Hello Hello ?" (The question mark is surrounded by black and the actual mark is white inside. Or it just prints hello.

I tried storing the getchar() input into a variable then after the loop printing the variable..But then I get numbers.


Any suggestions?

Thanks!
~
~
~
~
~

getchar reads a single character, I think you would like to read a string.


#include <stdio.h>
/*Asks for name then says 'hi'and reprints name*/
int main()
{
char name[20];

printf("Enter your name:");
scanf ("%s",name);
printf("Hello %s\n", name);
return 0;
}


also,note. %s to format the string in a printf and using a char array rather than a double.

hotcold
12-31-2007, 09:02 AM
Hi.

The lint (or lint-like) program can be your best friend. They do a lot of checking for meaning (semantics) as well as for syntax. Here's what splint had to say about your code:
% splint original.c
Splint 3.1.1 --- 23 Apr 2004

original.c: (in function main)
original.c:8:10: Assignment of int to double: name = getchar()
To allow all numeric types to match, use +relaxtypes.
original.c:8:9: Dangerous comparison involving double types:
(name = getchar()) != EOF
Two real (float, double, or long double) values are compared directly using a
C primitive. This may produce unexpected results since floating point
representations are inexact. Instead, compare the difference to FLT_EPSILON
or DBL_EPSILON. (Use -realcompare to inhibit warning)
original.c:8:9: Operands of != have incompatible types (double, int):
(name = getchar()) != EOF
original.c:9:3: Format string for printf has 0 args, given 1
Types are incompatible. (Use -type to inhibit warning)
original.c:5:15: Variable name2 declared but not used
A variable is declared but never used. Use /*@unused@*/ in front of
declaration to suppress message. (Use -varuse to inhibit warning)

Finished checking --- 5 code warnings
Best wishes ... cheers, hotcold

alshataan
12-31-2007, 03:42 PM
Excelllent. Thanks for the help.

whitewater3505
12-31-2007, 11:13 PM
Wow learning C for your first language is very impressive. If you want to learn something easier first i would start with python. A lot of newer programmers get fed up with pointers and other things that C/C++ requires. Phlipant's code is perfect. Keep up the good work man.

bwkaz
01-01-2008, 10:45 AM
Phlipant's code is perfect. Er, except for the fixed-length buffer that the user can type any length of data into. I.e., except for the gaping buffer overflow. ;)

About the only way to fix that if you want to keep using scanf is to put a maximum width on the %s (in this case, %19s, because the field length does not include the terminating zero byte, while the array will contain that byte). You could also use fgets(buf, 20, stdin) if you wanted -- that way the user could have a space in their name (but still not a newline).

whitewater3505
01-01-2008, 02:36 PM
Hey buffer overflows are fun, makes your code risky. ;) I was speaking in terms of brand new off the block programmer, who err ummmm should not have to worry about that in the beginning. But in all seriousness if not found, buffer overflow is a very dangerous thing, and should always be fixed. :D

bwkaz
01-01-2008, 03:20 PM
I was speaking in terms of brand new off the block programmer, who err ummmm should not have to worry about that in the beginning. Well, I figure it's probably easier to get people in the right habits about that kind of thing early, rather than trying to break dangerous habits later on. But yeah, maybe you're right.

(Or maybe that's a good reason not to learn C as a first language. Python, for instance, can't overflow a buffer unless there's a bug in the language core (or in the C implementation for some Python library you're using), so you don't have to worry about this stuff with some more-powerful languages.)

whitewater3505
01-01-2008, 05:23 PM
Yeah I have a good friend who is a python guru. Whenever I was starting out in college learning c++, he kept telling me python was so much better. Of course being a newbie programmer I didn't understand half of the thousand reasons he told me. After a year or so I finally understood why, and now I am a python enthusiast but still appreciate C and C++. I think everyone should start out in python then move their was into other more "difficult" languages, that way they have a strong understanding about programming. :D

mrrangerman43
01-01-2008, 05:33 PM
bwkaz

(Or maybe that's a good reason not to learn C as a first language.

What do you recommend as a beginning/first program language? I see Python is recommended, what do you say?

hotcold
01-01-2008, 06:29 PM
Hi.
What do you recommend as a beginning/first program language? I see Python is recommended, what do you say?
Depends of the goal and other factors: Hobson's choice for a local course, book, materials, compiler availability, curiosity, challenge, requirement at work, preparation for job / advancement in general or for a specific field, necessary for hobbyist work, peer pressure, background, personality traits (persistence, desire, learning style, patience, etc), others -- the usual :) ... cheers, hotcold

whitewater3505
01-01-2008, 07:40 PM
Hi.

Depends of the goal and other factors: Hobson's choice for a local course, book, materials, compiler availability, curiosity, challenge, requirement at work, preparation for job / advancement in general or for a specific field, necessary for hobbyist work, peer pressure, background, personality traits (persistence, desire, learning style, patience, etc), others -- the usual :) ... cheers, hotcold

Could not have said it more perfectly. The good thing is that once you learn your first language, learning another language is much easier.

mrrangerman43
01-01-2008, 09:22 PM
hotcold

Depends of the goal and other factors: Hobson's choice for a local course, book, materials, compiler availability, curiosity, challenge, requirement at work, preparation for job / advancement in general or for a specific field, necessary for hobbyist work, peer pressure, background, personality traits (persistence, desire, learning style, patience, etc), others -- the usual ... cheers, hotcold

So when you first started programming you knew what language you wanted to learn for the job you would have in the future ?

What I'm asking is what would be a good language to build a good foundation in programming ? A programmer as yourself had to start somewhere didn't you ?

I know there are many P. languages a person can learn, and a hundred reason for learning a given programming language. But doesn't one or two standout as a good one to start with?

It's building a good foundation I'm looking at for now.

alshataan
01-02-2008, 07:53 AM
Thanks for all the help guys.

FYI I chose C for several reasons. I knew it would be hard but I also knew once I get it down then I get two great benefits. First, I can do pretty much anything I want with it(even if it takes alot longer and more effort than say python). And second, after C most other languages will be all the more easier to learn. Of course those arent my primary reasons, but they are good ones I think.

Also, many other languages have borrowed quite a bit from C(I think), so almost anything after C will be quite familiar.

I'm using the k&r ANSI C Language book(I've ordered the solutions guide but not yet received) to learn in addition to posting questions here and looking up stuff on the internet. My only complaint is that it seems to target experienced programmers more but the writers also warned of that. So if anyone knows of any good texts or websites to compliment those than that would be great. In the meantime I'm just buckling down and giving it all I've got!

hotcold
01-02-2008, 10:54 AM
Hi, mrrangerman43.
So when you first started programming you knew what language you wanted to learn for the job you would have in the future ?

What I'm asking is what would be a good language to build a good foundation in programming ? A programmer as yourself had to start somewhere didn't you ?
...
That's only one of the influences that I mentioned, but, as it turns out, yes.

I was in an EE program in the USA, and if one wanted to get anything done in a technical field with the aid of a computer, one needed to know Fortran (FORTRAN, as it was called then). However, one also needed to know the machine in order to make the best use of it, so I coded in absolute octal for a long time. After that, we were allowed to use assembly. Finally, we were introduced to Fortran. That's still the standard for heavy-duty arithmetic work as well as for many significant production codes in the applied sciences.

On the business side of computing was COBOL (http://en.wikipedia.org/wiki/COBOL). There were minor languages -- ALGOL (http://en.wikipedia.org/wiki/ALGOL) was used for publishing and of some use in Europe. I did see a language called IPL, Information Processing Language (http://en.wikipedia.org/wiki/Information_Processing_Language), but I never saw anyone write code for it.

Currently, the language that seems most popular is Java (http://www.tiobe.com/tpci.htm). If one goes by percentages for applicability to jobs, that could be a good choice.

Other combinations of criteria would probably involve different choices ... cheers, hotcold

whitewater3505
01-02-2008, 11:21 AM
Hi, mrrangerman43.

I was in an EE program in the USA, and if one wanted to get anything done in a technical field with the aid of a computer, one needed to know Fortran (FORTRAN, as it was called then). However, one also needed to know the machine in order to make the best use of it, so I coded in absolute octal for a long time. After that, we were allowed to use assembly. Finally, we were introduced to Fortran. That's still the standard for heavy-duty arithmetic work as well as for many significant production codes in the applied sciences.

On the business side of computing was COBOL (http://en.wikipedia.org/wiki/COBOL). There were minor languages -- ALGOL (http://en.wikipedia.org/wiki/ALGOL) was used for publishing and of some use in Europe. I did see a language called IPL, Information Processing Language (http://en.wikipedia.org/wiki/Information_Processing_Language), but I never saw anyone write code for it.

Currently, the language that seems most popular is Java (http://www.tiobe.com/tpci.htm). If one goes by percentages for applicability to jobs, that could be a good choice.

Other combinations of criteria would probably involve different choices ... cheers, hotcold


Hotcold, you are a beast of a programmer. You program in all of the languages I pretend do not exist. Kudos to you man. In your experience as a programmer, when you get a job at a company, is it strictly one language or did they expect you to know more? I am about to enter the workforce. I am receiving my bachelors in CS, and may go for my masters, still not sure.

hotcold
01-02-2008, 01:11 PM
Hi, whitewater3505.
... In your experience as a programmer, when you get a job at a company, is it strictly one language or did they expect you to know more? ...
I have not been an employee in a long time; I've been doing contracting.

I think it's a mixed bag: some places want specialization, but more probably want a well-rounded worker. A few contracts ago, my client wanted a very large Fortran code ported to a beefy PC. The code had previously run on a Cray.

Another contract I had encouraged me to learn MUMPS (http://en.wikipedia.org/wiki/MUMPS), but I also designed and led a mentoring program there, and taught a few classes in the use of a wiki.

If I had to choose between having deep knowledge and having broad knowledge, I'd choose the latter in the current job market.

However, one needs to do some research on one's own to see what the available market desires ... cheers, hotcold

JRefL5
01-02-2008, 04:54 PM
Whitewater,
I agree with hotcold, look at the market, and get a good understanding of the foundations of the languages, not just the OO ones. those of us who learned machine & assembly languages and then COBLE, Fortran, etc are a dissapering breed and some of us left some dam poor code to those who will need to maintain or recode in a new language.

HC I don't think anyone outside of Burroughs corp used ALGOL. <from an old Univac/Sperry programmer>

hotcold
01-02-2008, 05:49 PM
Hi, JRefL5.
... HC I don't think anyone outside of Burroughs corp used ALGOL. <from an old Univac/Sperry programmer>
We didn't use it much. One research group at the UofM used it because it had good plotting routines, and the principal researcher was European.

Later, we used an ALGOL from the University of Texas (Austin) for a languages course, but that didn't last very long. The UofM was the distribution center for CDC mainframe MNF and M77 Fortran compilers, and for early Pascal.

When languages began to branch out a bit more, we obtained SNOBOL4 and SNOBOLC (Cal Berkeley) -- I once wrote a PL/1 to Fortran source converter in SNOBOLC. Ralph Griswold went on to create Icon at Arizona (http://en.wikipedia.org/wiki/Icon_%28programming_language%29)

These days, there are scads of languages, living and dead, a feast to feed the hunger of the curious: (http://en.wikipedia.org/wiki/Comparison_of_programming_languages) , not to mention the comparison of one program written in 1,000 languages, (http://99-bottles-of-beer.net/).

Thanks for the jog ( stroll, crawl :) ) down memory lane ... cheers, hotcold

bwkaz
01-02-2008, 08:18 PM
What do you recommend as a beginning/first program language? I see Python is recommended, what do you say? In case anyone's still wondering: For a first language, I'd say Python, yeah. The less you actually program in C, the more you'll get done, by a long shot. (This is true for the same reason that you get more done in C than in assembly: a more-powerful language lets you focus more on what you're doing than on how you're doing it.)

Don't ignore C forever, but learn it third or fourth or so, not first. (And Java is completely EVIL. Learn it if you must, but don't ever use it willingly. The reason is that when a pointy-haired-boss-type tells you what language they think you should use, they're (almost) never actually basing that decision on any relevant information: they're assuming that all languages are equivalent, and they're just choosing one based on, effectively, fashion. But languages aren't all equivalent; not by a long shot.)

webwolf
01-03-2008, 03:36 AM
My first language was GWBASIC (puke). Later Moved to Pascal then C/C++. I don't really consider C to be a bad choice for a first language, it just depends on the determination of the learner. As far as tutorials for a beginner I'd check http://www.freeprogrammingresources.com
you should find just about anything you need there.

and an appology for my english, although it's my native language, I don't get to speak it much.

Jeremy

con
01-03-2008, 05:46 AM
and an appology for my english, although it's my native language, I don't get to speak it much.

dont worry! most native english speakers dont know the diffrence between were and where; no, know and now; your and you're!!!!! I always lol when some not-native-english person excuses himself when posting on forums. So dont worry no one really cares. :) Gaming forums tend to be alot worse...

mrrangerman43
01-03-2008, 09:21 AM
Thanks all,


alshataan

I apologize my intent wasn't to hijack your thread, I should have started my own. :o