Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Code Newbie > Lounge
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 11-13-2002, 04:30 PM   #1 (permalink)
anon
 
Posts: n/a
edit?

Would it be alright if I posted the C tut here to get some editing or suggestions before it's submitted?
__________________
  Reply With Quote
Old 11-15-2002, 01:20 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
Yeah, lets see it...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-15-2002, 03:15 PM   #3 (permalink)
anon
 
Posts: n/a
Bit screwy... Moving it from pico to Abiword to Wordpad...



This is a basic C tutorial. First
off lets discuss what is C. C is an object-oriented language. It was
created in around 1970 by Dennis M. Ritchie. You should have a compiler for this tutorial. I use cc and gcc but if you are on Windows
you can use Borland's C compiler. First off lets look at the basic syntax of C.

#include<stdio.h> //That calls a the stdio.h library for C.


main() //starts the main function.
{ //opens the function
printf("This is C!"); //prints This is C!
} //closes the main function

First off see the ; after printf("This is C!"). Well there needs to be ; at the end of most lines code. Or else the C code will not work.
If you don;t already know // stands for a comment. It is good practice to comment your code. /* */ is also used for comments for
example

printf("This is C!"); /*Prints this is C!
printf("it is!"); */

would only print This is C! Because printf("it is!"); is between the /* and the */. All C files that include C code that haven't been
compiled should be named something.c where something is the filename that you want. For some compilers if it's not a *.c file it wont
compile it! Now if you compiled a program with

printf("This is the first line!");
printf("This the second line!");

would write This is the first line!This is the second line!. Why? Just because you put it on different it doesn't move it over one line.
You need to write:

printf("This is the first line!\n");
printf("This the second line!");

the \n moves the next thing that will be printed over a line. In C \*something* can do something. Like \n moved text over a line well
there are other things that \*something* can do. so if you want to write something like:

printf("I love \!");

Well, Its going to think you mean \! not just \ as text! so it will try to find out what \! means and does instead of printing \! So what do
we do to write a \ to screen? \\

print("I love \\!");

\\ stands for \.
\? stands for ?
\' stands for '
\" stands for "

Now, lets get into strings! First of all in programming there are 3 main types of data. Boolean, true or false,
number/variable, number, and string, which means a string of letters. But just "a" can be a string too. But all numbers can be a variable
or a string. Because you can have 902 as a variable. Or you can have 902 as a string by itself or as a part of a string like "Once there
was a great boy called Vlad who went by v902." the 902 in there isn't a variabiable. So lets make a very basic program where we declare a
string called myFirstString.

#include<stdio.h>

main()
{
char myFirstString[100]="I love Strings!"
printf("%s",myFirstString)
}


Here we DECLARE myFirstString. We declare it by saying what it is. It is a string. As you can see it says char myFirsString. The char
stands for character. And when you say char something you make something a string. The [100] stands for how long the string can be. Here I
put 100 but it could be smaller or larger. Then we write ="I love Strings!". Which tells us what myFirstString is equal to. Then we print
myFirstString to the screen by doing printf("%s",myFirstString). The %s is there to reserve space for where myFirstString will be. For
example lets say we wanted to print to screen "The contents of myFirstString are: " and then here were the contents of myFirstString. We
would write

printf("The contents of myFirstString are: %s",myFirstString)

This will show The contents of myFirstString are: I love Strings!. Because we reserved a place where the contents of
myFirstString would go. Lets try something with reserving places

#include<stdio.h>

main()
{
char myFirstString[100]="First String!";
char mySecondString[100]="Second String!";
printf("myFirstString: %s\nmySecondString: %s\n",myFirstString,mySecondString);
}

I believe thats pretty self-explanatory. Now a string in C is actually an array of characters. First lets discuss what an array is. In an
array there can be infinite amount of characters. Imagine this. You have to make a program. The program has to hold every single person in
a bussiness names. Now how are we gonna do that? Make 3000 different strings?!?! No. We will make an array. Lets say we have an array
called myArray. And it holds the values, J, S, C and X. We could access those values easily by doing myArray[0] for J myArray[1] for S,
myArray[2] for C, etc.. Because computers start counting at 0 not 1. So here is what myArray looks like

myArray

0 1 2 3
J S C X

and strings are arrays, arrays of eltters, so myFirstString is actually

1 2 3 4 5 6 7 8 9 10 11 12 13
F i r s t S t r i n g !

Where myFirstString[0]=F and myFirstString[1]=i. So a string is actually an array of letters.


Now lets go onto numbers! While it was simple for strings because we only had char in numbers we have more ways to declare variables. We
have:

int-for small numbers, usually between 33,000 to -33,000
short-for short integers
long-for long intergers
float-floating point number. It can have a decimal in it and they are usually between 10^38(10 to the 38th power) and -10^38
double-double float, huge!

Lets try something now


#include<stdio.h>

main()
{
int myVar, myVar2;
myVar=20;
myVar2=30;
myVar=myVar+myVar2;
printf("%i",myVar);
}

Now it should print 50. We declared myVar and myVar2 integers because of the int. Then we said that myVar=20, and myVar=30. Then we said
myVar=20+30. and then printed myVar. As %s stood for string, %i is for integer. Now here is something you haven't learned before that you
should learn now as to save you some time debugging. You need to declare all your variables and strings in the very start of the code.
Like this wouldn't work:

#include<stdio.h>

main()
{
int myVar;
myVar=20;
int myVar2;
myVar2=30;
myVar=myVar+myVar2;
printf("%i",myVar);
}

Because you declared myVar, then had a line of code, then declared another integer. You can't do this. ALL intergers and strings have to
be declared first then code can be written. This would work though:

#include<stdio.h>

main()
{
int myVar=20;
int myVar2=30;
myVar=myVar+myVar2l;
print("%i",myVar);
}

Here are the very basics. I plan to write another tutorial, you may want to check that one out, it will cover loops, if then, functions,
and other more advanced things
__________________
  Reply With Quote
Old 11-15-2002, 11:01 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
erm, its not entirely true that you need to declare all you're variables before you do a line of code... it seems that some compilers are just strict about it (borland), it is not a requirement for all compilers.

... and int's can be larger than 33,000 or -33,000 ...
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 11-15-2002, 11:19 PM   #5 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
oh my... and I just noticed you said this:

Quote:
C is an object-oriented language.
there is NO way C is an object-oriented language
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 11-16-2002, 05:44 AM   #6 (permalink)
anon
 
Posts: n/a
woops.... But the int's can be larger if they 32-bit. Some compilers only allow 16-bit ones. I'll add a note there. And most compilers are strict about declaring variables at the start... cc and gcc wont let you get by with it...

PS. Thanks for overlooking it
__________________
  Reply With Quote
Old 11-18-2002, 10:28 PM   #7 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Variable declaration is to be done at the start of any block, not just the start of the program or function at global scope...

Example:
Code:
int main() { int x = 10; if (x == 10) { int y = 3; printf("x is %d and y is %d\n", x, y); } return 0; }
Completely legitimate, even tested in gcc to make sure my C++ isn't flowing into ANSI C.

Regarding the 33,000 thing, the true range for a 16-bit signed integer is -32768 to 32767, but int's haven't really been seen as 16-bit in any mainstream compiler for quite some time. To really discover the limits of a most types, include <limits.h> then use the sizeof(type) operator, multiply by CHAR_BIT then shift the value of 1 to the left that many times minus 1 (signed), don't subtract if you want the max for the unsigned value. IE:
Code:
#include <limits.h> #include <stdio.h> int main() { unsigned long x = (1 << ((sizeof(int) * CHAR_BIT)-1)); printf("the max size of a signed int is: %lu\n", x); return 0; }
Execute this and you'll see results that look something along the lines of:
Quote:
the max size of a signed int is: 2147483648
... assuming a 32-bit compiler. Of course, you don't really have to test these because there are predefined values available in limits.h that look something like:
Code:
#define USHRT_MAX 0xffff #define SHRT_MAX 0x7fff #define SHRT_MIN (-0x7fff - 1) #define UINT_MAX 0xffffffffU #define INT_MAX 0x7fffffff #define INT_MIN (-0x7fffffff - 1)
but it is always more fun to figure these things out. I think.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 11-20-2002, 04:03 PM   #8 (permalink)
anon
 
Posts: n/a
But that wasn't discussed yet. Thats why I didn't mention it...
__________________
  Reply With Quote
Old 11-20-2002, 07:01 PM   #9 (permalink)
crichards
Registered User
 
Join Date: Nov 2002
Location: Arizona
Posts: 2
crichards is on a distinguished road
Send a message via AIM to crichards
Its best to get in to the habit of using /* */ comments in C, for not only backwards compatibility, but also because they are used more in Unix code.

// is used mainly for C++

And there are ways to get C to behave like an object oriented language. GTK+ does this.

LNO people are invading, it seems.
__________________
crichards is offline   Reply With Quote
Old 11-20-2002, 10:50 PM   #10 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
"behave like"

the fact remains that it is NOT an object orientated language, simple as that.

and "//" is used in java and javascript and c# ...
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 11-21-2002, 03:02 PM   #11 (permalink)
anon
 
Posts: n/a
yes but /* is also used in JS just so ya know . But then again JS is just C+Java+simplicity to me...
__________________
  Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding Cron bdl Linux 0 02-16-2003 01:58 PM
Changing variable type for edit box : mfc - .net sde Standard C, C++ 0 01-31-2003 09:53 AM
Newbie Help (Japanese Language Support) DasXel Linux / BSD / OS X 11 01-14-2003 06:20 AM


All times are GMT -8. The time now is 04:10 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle