Me learning ANSI C

gooooogle

New member
Hi. I've spent the last few days learning c programming and today I realised that I actually learned quite a bit. Bellow is the code for a program I made without refering to any form of refrence. I just did it from the top of my head.

#include <stdio.h>

main()
{
int birth_year, required_year, result;
printf("What year were you born?: ");
scanf("%d", &birth_year);
printf("What year do you want to know your age for?: ");
scanf("%d", &required_year);
result = required_year - birth_year;

if (result < 0)
printf("You were not born in %d", required_year);
else if (result == 0)
printf("You were born in %d", required_year);
else
printf("In the year %d you will be %d years old", required_year, result);

printf("\n");
return 0;
}

It's not much but it's something.
 
ahhhh the good old days. I took VB / C / C++ in high school 10th grade stuff. IT is very very similar to PHP. I am suprised for just a few hours how clean your coding looks. Keep it clean if you continue makes it so much easer for Debugging :). Oh and don't forget to comment everything lets you know what you are doing :)
 
I have never done C programming, have seen it a lot though.
Your code is very clean looking and laid out very well, you would be surprised how much that helps when you are looking through it either for a problem or just looking at what code is there.

I don't really comment my code unless it is needed for someone else looking at it, the reason is I normally know what the code is used for anyway after I do it, and adding the comments just takes more time when I don't need it myself.

It is a good idea if you think you might forget what you were doing (Can happen a lot when you start using advanced code and writing code which uses a lot of lines for the one function)

Looking good so far :)
 
John you always comment code in a project. Incase you can't work on it for a while and forget what you were trying to create :)
 
Hehe I have a project which I have been working on for at least 18 months with tens of thousands of lines of code.
I would have about 100 lines of comments and thats it.

Just too much to comment everything when there is so much code to write, when you have to code until the early morning you just couldn't be bothered having to do more work than needed hehe :D

I always know what I was doing through from just looking at what the code is, like if I saw someone elses code with comments, I would still know what they are doing :)
 
Out of interested, where did you start learning it from?
Was it from a site?Or a book?
 
I started learning from a book my dad has called "The C programming Language" by Brian W. Kernighan and Dennis M. Ritchie. A good book.
 
I just rewrote it in RealBasic. Here is the result.

// declare all strings and doubles
dim birth_year, new_year, result_year As String
dim birth_yearint, new_yearint, result_yearint As double

birth_year = edfBirth.text
new_year = edfNewYear.text

// convert strings to doubles for calculations
birth_yearint=cdbl(birth_year)
new_yearint=cdbl(new_year)
result_yearint=cdbl(result_year)

result_yearint = new_yearint - birth_yearint

//convert back to string for displaying
result_year=cstr(result_yearint)
new_year=cstr(new_yearint)

lblResult.text = result_year + " years old"
lblResult.visible = true

I will post a screenshot of it running.
 
Here it is running on Mac OS X. RealBasic can also compile for Windows and Linux but I just happened to be using my Mac at the time.
 

Attachments

  • running.jpg
    running.jpg
    4.8 KB · Views: 6
What you need to learn to become a C programmer is pointers and how the C program is compiled into assembly. C does not have any sort of automatic memeory management. You need to allocate buffers for strings and other data. If you get memory managment wrong, you'll either end up with memory leaks or a big gaping security hole. To understand why, you have to understand how declared varibles are situated in the stack, and various other low-level things.
 
C is the best language that I've ever used, and I've done quite a few. It's a like a Ferrari with no seatbelts: incredibly powerful, but if you make a mistake it could get ugly. Many languages borrow from C syntax too, so it's quite useful...
 
Back
Top