LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-13-2003, 04:12 AM   #1
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Writting my first C program in linux


Code:
#include <stdio.h>

main ()
{
int a;
printf("Please enter something:\n");
scanf("%d",&a);
printf("The value stored in a is %d\n\n",a);
}
But when I compile I get:

Code:
micxz@mars:~/c-experiment> gcc -ggdb -Wall -o myprog myprog.c 
myprog.c:4: warning: return-type defaults to `int'
myprog.c: In function `main':
myprog.c:9: warning: control reaches end of non-void function
And when I run i I get:

Code:
micxz@mars:~/c-experiment> ./myprog 
Please enter something:
dd
The value stored in a is -1073744232
Why am I getting numbers? I guess there are alot of questions and I know most of you will say get a book.

My question is can you guys post links to some good howto's and introductions to C programming. I want to know more;
 
Old 03-13-2003, 04:36 AM   #2
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
I changed "scanf("%d",&a);" to "scanf("%s",&a);"

But stiil no go. I think this part is still the problem though;
 
Old 03-13-2003, 06:55 AM   #3
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
Code:
#include <stdio.h>

int main ()
{
int a;
int err;

printf("Please enter something:\n");

err=scanf("%d",&a); // scanf returns
// "number of input items assigned"

if (err != 1)
        printf("You didn't enter a number :-(\n\n");
else
        printf("The value stored in a is %d\n\n",a);
return (err==1); // 0 if ok :-)
}
 
Old 03-13-2003, 07:13 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
why are you getting a number? because you have said "int a;" i.e. variable "a" IS a number.....
 
Old 03-13-2003, 07:51 AM   #5
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
the variable "a" is of type integer => a number

the parameter "%d" in scanf (man scanf) tells scanf to expect a number from input and to store the result in the variable "a"

the types have to match
e.g.
if you read in a number: int ==> %d
if you want to read a character : char ==> %c
etc.

see the "scanf" man page for details
 
Old 03-13-2003, 07:55 AM   #6
GiR
LQ Newbie
 
Registered: Mar 2003
Location: Florida
Posts: 4

Rep: Reputation: 0
i am learning C++ myslef, but on Windows, XP Pro

Last edited by GiR; 03-13-2003 at 07:56 AM.
 
Old 03-13-2003, 12:21 PM   #7
macewan
Senior Member
 
Registered: Jan 2002
Distribution: Ubuntu, Debian
Posts: 1,055
Blog Entries: 1

Rep: Reputation: 45
you might want to consider python. it extremely easy to learn.

http://python.org/doc/Newbies.html

print "Hello, world!"

http://www.hetland.org/python/instant-hacking.php
http://www.honors.montana.edu/~jjc/easytut/easytut/


print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s
 
Old 03-13-2003, 12:59 PM   #8
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Something like this works for what you need:

#include <stdio.h>

int main() {
char array1[50];


printf("Enter a string less than 50");
printf(" characters with spaces: \n");
gets(array1);

printf("\nYou entered: ");
puts(array1);

return;
}

Ummm...for some reason when I compile it doing:

cc get.c -o get

It gives an error that says:

the `gets' function is dangerous and should not be used.

I don't know whats so dangerous about it but it works fine.
 
Old 03-13-2003, 01:03 PM   #9
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
% man 3 gets

[...]

BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets()
will read, and because gets() will continue to store characters past the end of the buffer, it is extremely danger_
ous to use. It has been used to break computer security. Use fgets() instead.
 
Old 03-13-2003, 01:44 PM   #10
cornet
LQ Newbie
 
Registered: Nov 2001
Distribution: Debian
Posts: 14

Rep: Reputation: 0
Have a look at this.. It addresses some problems from your code:
* Input is buffered to prevent buffer overflows.
* Input is checked to make sure it *is* an integer rather than just assuming that it is.

You should never use gets or scanf if you can avoid it..

#include <stdio.h>
#include <stdlib.h>

#define MAX_BUF 80

int main(void)
{
char input[MAX_BUF]; /* Input buffer */
int number; /* Holds number after input */

printf("Please enter something:\n");

/* Read input from stdin */
fgets(input, sizeof(input), stdin);

/* Convert to integer */
number = atoi(input);

/* Check that it worked! */
if ( number != 0 ) {
printf("The number you entered was %d\n", number);
} else {
printf("You did not enter a valid number (or you entered 0)\n");
}

return 0;
}
 
Old 03-14-2003, 01:37 AM   #11
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
comet Thanks compiled with no errors. THANKS everyone. I'm really a beginner at this stuff. But if I enter more than 10 numbers I get wrong results, is this because of the buffer?
 
Old 03-14-2003, 04:38 AM   #12
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
just to add my 2 cents to what comet was saying, you really shouldnt use scanf or gets because like he said they can cause buffer overflow errors which is common way hackers get control of a computer and run their own code. So basically, those functions are security risks as well as just a good way to crash a program.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writting an OS in C++ makko Programming 2 08-27-2005 08:27 AM
Is a file in writting ? stephnane AIX 3 08-03-2004 12:17 AM
Is a file in writting stephnane Linux - General 2 07-30-2004 10:39 PM
Collaborative writting synna Linux - Software 2 06-25-2004 10:33 AM
Error on writting in ttyS0 phyrenees Linux - General 3 12-20-2002 04:32 PM


All times are GMT -5. The time now is 08:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration