# Newbie here trying to teach myself programming in c

• 02-08-2006
paulntysmom
Newbie here trying to teach myself programming in c
Ok I am trying to teach myself programming in c but I am lost. I have gotten through the first few tutorials but I am stuck on this one.

I need my output to be as the following:

This program will do some computations.

The two numbers I am using in this program are 63 and 25.

The Sum of 63 plus 25 is 88.

This is what i have written so far but I am so lost on this. Can someone tell me what I am doing wrong? Please :) I am getting an error on line 12 at the {
Code:

``` /*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         return 0; } {         int sum;         sum = 63 + 25;         printf ("The Sum of 63 and 25 is %i\n", sum); }```
or should it be something like this:

Code:

```/*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         return 0; {         int  sum;         int  num1 = 63;         int  num2 = 25;         sum = num1 + num2;    // addition }```
I get the second one to run with no errors but the "The sum of 63 plus 25 is 88" is not showing up.
• 02-08-2006
Sysop_fb
Quote:

Originally Posted by paulntysmom
Ok I am trying to teach myself programming in c but I am lost. I have gotten through the first few tutorials but I am stuck on this one.

I need my output to be as the following:

This program will do some computations.

The two numbers I am using in this program are 63 and 25.

The Sum of 63 plus 25 is 88.

This is what i have written so far but I am so lost on this. Can someone tell me what I am doing wrong? Please :) I am getting an error on line 12 at the {
Code:

``` /*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         return 0; } {         int sum;         sum = 63 + 25;         printf ("The Sum of 63 and 25 is %i\n", sum); }```
or should it be something like this:

Code:

```/*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         return 0; {         int  sum;         int  num1 = 63;         int  num2 = 25;         sum = num1 + num2;    // addition }```
I get the second one to run with no errors but the "The sum of 63 plus 25 is 88" is not showing up.

Code:

`return 0;`
That's causing your program to end..
Also watch your bracket use, here's a sample to compare.

Code:

```/*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> int main () {         int sum;         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         sum = 63 + 25;         printf ("The Sum of 63 and 25 is %d\n", sum);         return 0; }```
Moved the declaration of sum since it needs to go in the beginning of a block of code.
Changed the %i in printf to a %d.
Moved the return 0 to the end of the main function.

You might want to pick up a book to read.. like 'The C Programming Language'
• 02-08-2006
grumpy
As you've given it here, I'm surprised the second one executed: at all: there is no closing brace to terminate the main) function. Try one of the following;

Code:

``` #include <stdio.h> main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n"); {         int  sum;         int  num1 = 63;         int  num2 = 25;         sum = num1 + num2;    // addition         printf("Sum is %d\n", sum);  // print the result       }         return 0;    // return statement AFTER you compute the sum }```
or (if you want to do it with functions);
Code:

```#include <stdio.h> void myfunction(); main () {         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         myfunction()         return 0; } void myfunction() {         int  sum;         int  num1 = 63;         int  num2 = 25;         sum = num1 + num2;    // addition         printf("Sum is %d\n", sum);  // print the result }```
As a general rule, code placed in a function after a return statement does not get executed. And the compiler doesn't magically decide that, since you're computing a sum, that it needs to print the result.

Unrelated to your problem: the // style of comment is fairly recent, and is only supported in C++ compilers (reasonably common) or C compilers compliant with the 1999 C standard (relatively rare). C compilers that comply with the 1989 C standard will choke on such comments.
• 02-08-2006
paulntysmom
Thank you :)
Ok I am slowly understanding what I did wrong. I did purchase two books programming in ansi c and C Programming in Easy Steps. I have no programming experience (can you tell LOL)

I think the most confusing part to me is just adding in a new part to the program.

Example from above

This program will do some computations
The two numbers I am using in this program are 62 and 25.
The sum of 63, plus 25 is 88

Now if I wanted to add another line in there
Code:

`(say something like - The product of 63 times 25 is 1575)  do I just start of by using { and will the return 0: go at the very end of the program? and then close if off using }`
• 02-08-2006
paulntysmom
Ok I think I did this right to add in on my last reply! Can someone please let me know if I did it right or wrong?

Code:

```/*mavrakp1.c */ /*Created by Colleen Mavrakos 2/7/2006 */ #include <stdio.h> int main () {         int sum;         printf("This program will do some computations\n");         printf("The two numbers I am using in this program are 63 and 25.\n");         sum = 63 + 25;         printf ("The Sum of 63 plus 25 is %d\n", sum);         sum = 63 * 25;         printf ("The Product of 63 times 25 is %d\n", sum);         return 0; }```
I compiled it and it came up with no errors and the output of it is :
This program will do some computations.
The two numbers I am using in this program are 63 and 25.
The Sum of 63 plus 25 is 88
The Product of 63 times 25 is 1575
• 02-08-2006
dwks
Excellent program. :)

Good luck with programming.
• 02-09-2006
Navar
If you want a free compiler that does a decient job...and 1999 compliant - Dev-C is freeware.
• 02-09-2006
paulntysmom
Thanks
Thank you guys so much for your help. This board will help me alot :)

I purchased Borlands Turbo C++ 4.5 (is that a good one) but it was a breeze to set up and it is user friendly (well at least for me) I really do want to learn how to program so I am reading reading and reading as much as I can.
• 02-09-2006
fgw_three
Quote:

Originally Posted by paulntysmom
Thank you guys so much for your help. This board will help me alot :)

I purchased Borlands Turbo C++ 4.5 (is that a good one) but it was a breeze to set up and it is user friendly (well at least for me) I really do want to learn how to program so I am reading reading and reading as much as I can.

I can't answer your compiler questions, as I work mostly on Linux and use gcc.

But I do want to wish you luck, and tell you to keep asking questions in here! It's refreshing to see someone who really WANTS to learn, as opposed to some folks who just want to find the easiest way to get their homework assignment done.

BTW, the previous suggestion about picking up "The C Programming Language" by Brian Kernighan and Dennis Ritchie is something you really should think about. It might seem a bit complex from a raw beginner standpoint (althought it was my first text), but it is a book that I think you will find highly valuable as you progress.
• 02-09-2006
Navar
Quote:

Originally Posted by fgw_three
"The C Programming Language" by Brian Kernighan and Dennis Ritchie is something you really should think about.

This is the book I use in Comp Solutions, part of EE program at UTA...
good book

Borlind is a decient compiler, one of the industry leaders.
• 02-09-2006
paulntysmom
Thank you, I will buy that book also. Everything helps :) You guys are wonderful! Thanks for all the advice.