Results 1 to 3 of 3

Thread: Learning C for beginners

  1. #1
    Join Date
    Aug 2005
    Posts
    293

    Learning C for beginners

    Simple Input and Output

    When you take time to consider it, a computer would be pretty useless without some way to talk to the people who use it. Just like we need information in order to accomplish tasks, so do computers. And just as we supply information to others so that they can do tasks, so do computers.

    These supplies and returns of information to a computer are called input and output. 'Input' is information supplied to a computer or program. 'Output' is information provided by a computer or program. Frequently, computer programmers will lump the discussion in the more general term input/output or simply, I/O.

    In C, there are many different ways for a program to communicate with the user. Amazingly, the most simple methods usually taught to beginning programmers may also be the most powerful. In the "Hello, World" example at the beginning of this text, we were introduced to a Standard Library file stdio.h, and one of its functions, printf(). Here we discuss more of the functions that stdio.h gives us.

    Output using printf()

    Recall from the beginning of this text the demonstration program duplicated below:

    Code:
    #include <stdio.h> 
      
    int main(void) 
    { 
      printf("Hello, world!\n"); 
      return 0; 
    }
    If you compile and run this program, you will see the sentence below show up on your screen:

    Hello, world!

    This amazing accomplishment was achieved by using the function printf(). A function is like a "black box" that does something for you without exposing the internals inside. We can write functions ourselves in C, but we will cover that later.

    You have seen that to use printf() one puts text, surrounded by quotes, in between the brackets. We call the text surrounded by quotes a literal string (or just a string), and we call that string an argument to printf.

    As a note of explanation, it is sometimes convenient to include the open and closing parentheses after a function name to remind us that it is, indeed, a function. However usually when the name of the function we are talking about is understood, it is not necessary.

    As you can see in the example above, using printf() can be as simple as typing in some text, surrounded by double quotes (note that these are double quotes and not two single quotes). So, for example, you can print any string by placing it as an argument to the printf() function:

    Code:
    printf("This sentence will print out exactly as you see it...");

    And once it is contained in a proper main() function, it will show:

    This sentence will print out exactly as you see it...

    Printing numbers and escape sequences

    Placeholder codes

    The printf function is a powerful function, and is probably the most-used function in C programs.

    For example, let us look at a problem. Say we don't know what 1905 + 31214 is. Let's use C to get the answer.

    We start writing


    Code:
    #include <stdio.h> /* this is important, since printf 
                        * can't be used without this line */ 

    int main(void)
    {
    printf("1905+31214 is");
    return 0;
    }


    but here we are stuck! printf only prints strings! Thankfully, printf has methods for printing numbers. What we do is put a placeholder format code in the string. We write:

    printf("1905+31214 is %d", 1905+31214);

    The placeholder %d literally "holds the place" for the actual number that is the result of adding 1905 to 31214.

    These placeholders are called format specifiers. Many other format specifiers work with printf. If we have a floating-point number, we can use %f to print out a floating-point number, decimal point and all. An incomplete list is:

    * %i and %d - int
    * %f - float
    * %lf - double
    * %s - string
    * %x - hexadecimal

    A more complete list is in the File I/O section.


    Tabs and newlines

    What if, we want to achieve some output that will look like:

    Code:
    1905 
    31214 + 
    -----
    printf will not put line breaks in at the end of each statement: we must do this ourselves. But how?

    What we can do is use the newline escape character. An escape character is a special character that we can write but will do something special onscreen, such as make a beep, write a tab, and so on. To write a newline we write \n. All escape characters start with a backslash.

    So to achieve the output above, we write

    Code:
    printf(" 1905\n31214 +\n-----\n%d", 33119);

    or to be a bit clearer, we can break this long printf statement over several lines. So our program will be:


    Code:
    #include <stdio.h> 
      
    int main(void) 
    { 
       printf(" 1905\n"); 
       printf("31214 +\n"); 
       printf("-----\n"); 
       printf("%d", 33119); 
       return 0; 
    }

    There are other escape characters we can use. Another common one is to use \t to write a tab. You can use \a to ring the computer's bell, but you should not use this very much in your programs, as excessive use of sound is not very friendly to the user.


    Other output methods

    puts()

    The puts() function is a very simple way to send a string to the screen when you have no placeholders to be concerned about. It works very much like the printf() function we saw the "Hello, World!" example:

    Code:
    puts("Print this string.");

    will print to the screen:

    Print this string.

    followed by the newline character (as discussed above). (The puts function appends a newline character to its output.) The fputs function is similar:


    Code:
    fputs("Print this string via fputs", stdout);

    will print to the stdout file (usually the screen):

    Print this string via fputs

    without a newline tacked on to the end.

    Since puts() and fputs() do not allow the placeholders and the associated formatting that printf() allows, for most programmers learning printf() is sufficient for their needs.


    Input using scanf()

    The scanf() function is the input method equivalent to the printf() output function—simple yet powerful. In its simplest invocation, the scanf() format string holds a single placeholder representing the type of value that will be entered by the user. These placeholders are exactly the same as the printf() function - %d for ints, %f for floats, and %lf for doubles.

    There is, however, one variation to scanf() as compared to printf(). The scanf() function requires the memory address of the variable to which you want to save the input value. While pointers are possible here, this is a concept that won't be approached until later in the text. Instead, the simple technique is to use the address-of operator, &. For now it may be best to consider this "magic" before we discuss pointers.

    A typical application might be like this:

    Code:
    #include <stdio.h> 
      
    int main(void) 
    { 
      int a; 
      
      printf("Please input an integer value: "); 
      scanf("%d", &a); 
    }

    If you are trying to input a string using scanf, you should not include the & operator.

    If you were to describe the effect of the scanf function call above, it might read as: "Read in an integer from the user and store it at the address of variable a ".

    Note of caution on inputs: When data is typed at a keyboard, the information does not go straight to the program that is running. It is first stored in what is known as a buffer—a small amount of memory reserved for the input source. Sometimes there will be data left in the buffer when the program wants to read from the input source, and the scanf() function will read this data instead of waiting for the user to type something.

  2. #2
    Join Date
    Mar 2008
    Posts
    416
    thanks for this beginners tut

  3. #3
    Join Date
    May 2008
    Posts
    2
    seriously this is a nice tute, thanx
    ...Give Respect 2 Get Respect...

Similar Threads

  1. C# For Beginners
    By Santi in forum Guides & Tutorials
    Replies: 3
    Last Post: 13-05-2011, 01:46 PM
  2. mIRC for Beginners
    By spookshow in forum Guides & Tutorials
    Replies: 3
    Last Post: 08-05-2011, 02:24 AM
  3. TCP Optimizer for beginners
    By yummy- in forum Networking & Security
    Replies: 4
    Last Post: 24-11-2010, 12:10 AM
  4. Aeromodelling for Beginners
    By Mr Alfa in forum Off Topic Chat
    Replies: 6
    Last Post: 13-11-2010, 11:10 AM
  5. Cheap BEGINNERS rig
    By Jagadayu in forum Overclocking & Computer Modification
    Replies: 4
    Last Post: 24-09-2010, 12:23 PM

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
  •  
Page generated in 1,750,444,542.07306 seconds with 16 queries