+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Basic C, for beginners!

  1. #1
    Phoenixz is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    256
    Blog Entries
    1
    Rep Power
    13

    Basic C, for beginners!

    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.)

    Code:
    #include <stdio.h>
    #include <strings.h>
    #define HOURS_IN_DAY 24
    
    main()
    {
    Now, we'll make a char variable with a limit of 21 characters, for the name
    Code:
    char   name[21];
    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.

    now, we'll move to printf, scanf.

    printf is output, whereas scanf is input. So, you have the names now how to use them.
    Code:
    printf("I want to print this out!\n");
    This will put into the screen
    I want to print this out!
    and the cursor will move down a line (\n means new line, see ascii chart.)

    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:
    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();
    }
    fflush(stdin); makes sure that the... buffer I think is the word is empty from the last thing you inputted.

    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!
    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();
    }
    Side note: I didn't find a way to fit the define in, but at least you know how it works!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Basic C, for beginners!

    Very nice!
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Basic C, for beginners!

    Nice!

    I haven't done much C++ or C but is there a difference between using:

    Code:
    const int nMin = 2;
    and

    Code:
    #define nMIN  2;

  5. #4
    Phoenixz is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    256
    Blog Entries
    1
    Rep Power
    13

    Re: Basic C, for beginners!

    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.

  6. #5
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Basic C, for beginners!

    Also don't you have to declare a return type for main?

  7. #6
    Phoenixz is offline Programming Professional
    Join Date
    Dec 2008
    Posts
    256
    Blog Entries
    1
    Rep Power
    13

    Re: Basic C, for beginners!

    not in C

  8. #7
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Basic C, for beginners!

    Nice One .. +rep

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Basic C, for beginners!

    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++.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Join Date
    Apr 2008
    Posts
    789
    Blog Entries
    5
    Rep Power
    24

    Re: Basic C, for beginners!

    Actually const is in c99, like a lot of good ideas from c++.
    Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
    "When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Basic C, for beginners!

    True, the two languages continue to influence each other.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Similar Threads

  1. Basic Compiler Design Tutorial for Beginners
    By Bibek Dahal in forum General Programming
    Replies: 2
    Last Post: 10-17-2011, 01:17 AM
  2. Any Python beginners ?
    By ToX in forum Python
    Replies: 54
    Last Post: 10-11-2010, 06:09 AM
  3. Some Basic Linux Commands, For Beginners...
    By Onur in forum Linux Tutorials, Guides and Tips
    Replies: 5
    Last Post: 10-12-2009, 12:07 AM
  4. Beginners [Read This]
    By Donovan in forum Tutorials
    Replies: 15
    Last Post: 12-27-2008, 12:57 PM
  5. beginners C# project
    By matty241 in forum C# Programming
    Replies: 44
    Last Post: 05-31-2008, 09:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts