Hey again, today, we're going to learn our first script, i'm not talking as simple as hello world, but I will go over that, expanding on it, just to show some features available to you.
I will explain each line, and use code tags. at the end i'll add it all together. :>
Here we have a main function, it goes at the beginning (well, after #includes, and defines).
Kay, so we have #include, this adds a whole library to your script making it essentially, alot longer, but it's needed for some commands to work and you won't see it.
In this example we'll call stdio.h, you can open that later and read all the things it includes if you so wish.
We also include strings.h for strcmp
We're also going to make a define! I tried hard to make a define fit into this so be greatful! ><
Define works pretty easily, and it works like we are going to Define THIS_STRING as THIS VALUE, this value is an integer (you'll see in the code.)
Now, we'll make a char variable with a limit of 21 characters, for the nameCode:#include <stdio.h> #include <strings.h> #define HOURS_IN_DAY 24 main() {
and there we go, include, define, main done we can also call that like it's a regular var too throughout the code, and it makes it so much easier to read.Code:char name[21];
now, we'll move to printf, scanf.
printf is output, whereas scanf is input. So, you have the names now how to use them.
This will put into the screenCode:printf("I want to print this out!\n");and the cursor will move down a line (\n means new line, see ascii chart.)I want to print this out!
Now for scanf, scanf can be used in 3 ways, strings, integers, and single characters. Why does this matter, if it's the same function you may ask?
Well, you got to pick which to use;
%c - characters
%d - integers (when using any var that isn't a string use an amper sign before the name of it - I won't explain why that will confuse you.)
%s - strings
Now, a string is essentially a group of characters, and that's what i'll be using today.
Code so far, all together:
fflush(stdin); makes sure that the... buffer I think is the word is empty from the last thing you inputted.Code:#include <stdio.h> #define HOURS_IN_DAY 24 main() { char name[21]; printf("I want to print this out\n"); printf("Please enter your name: "); scanf("%s", name); printf("Your name is %s\n", name); fflush(stdin); getchar(); }
getchar(); stops the program from ending when you press enter to input your name.
Now, i'm going to introduce you to if, and strcmp, and the idea of negative assignment.
The if statement will be like so; if strcmp (string compare) name isn't Aaron.
it is isn't because of the ! making it negative.
also remember if statements don't require a ";"
Code:if(!strcmp(name, "Aaron")) { printf("Hey, we share names!"); }
So, let's right it all out now for the final time!
Side note: I didn't find a way to fit the define in, but at least you know how it works!Code:#include <stdio.h> #include <strings.h> #define HOURS_IN_DAY 24 main() { char name[21]; printf("I want to print this out\n"); printf("Please enter your name: "); scanf("%s", name); printf("Your name is %s\n", name); if(!strcmp(name, "Aaron")) printf("We share the same name!"); else printf("Your name isn't the same"); fflush(stdin); getchar(); }![]()
Very nice!
Nice!
I haven't done much C++ or C but is there a difference between using:
andCode:const int nMin = 2;
Code:#define nMIN 2;
I believe const doesn't exist in C, but only in C++? or not used really in C... #define name value is much much easier to read and understand, though.
Edit: also thanks to you, WingedPanther for leaving good comments both times.![]()
Also don't you have to declare a return type for main?
not in C
Nice One .. +rep![]()
const is unique to C++. One of the reasons for const in C++ is to make #define less necessary. Because C++ is more strongly typed than C, this makes using const worthwhile in C++.
True, the two languages continue to influence each other.
There are currently 3 users browsing this thread. (0 members and 3 guests)
Bookmarks