Thread: Quick review of my basic getchar() program

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Fulham, London
    Posts
    1

    Quick review of my basic getchar() program

    Hi guys,

    I am learning how to use the getchar() function in <stdio.h>. I followed an example from Greg Parry's Absolute C Beginner book, and an old thread on this site from 2011 that helped a student with his homework on getchar().

    I wondered if you guys could review how I've written this and let me know if it is good code. It compiles in Xcode and works, but I'm not sure if it is efficient/sensible.

    Really appreciate any help here as I am very new learner!

    Code:
    //
    //  main.c
    //  getchar() problem: Testing I understand getchar() enough
    //
    //  Created by TwoToesTommy on 30/03/2013.
    //  Copyright (c) 2013 TwoToesTommy. All rights reserved.
    //
    
    
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
        char num[3];
        int ctr;
        
        printf("Enter 3 numbers the hit return: \n");
        
        for (ctr = 0; ctr < 3; ctr++) {
            num[ctr] = getchar();
        
            if (num[ctr] == '\n') {
                ctr--;
                break;
        
            }
            
        }
        
        printf("Your numbers are %c, %c and %c", num[0], num[1], num[2]);
        
        return 0;
    }
    Many thanks

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    It will work, but is quite trivial.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Try to write a function that reads in a complete c-string from stdin and then prints it out.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    - Certain elements in num may be uninitialised. Accessing them and attempting to pass their values to printf has undefined behaviour.

    - The last line of stdout isn't terminated with a newline.

    - You don't check the return value of getchar for EOF.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    To follow up on the last point presented by Barney McGrew:

    Note that the return type of "getchar()" is "int"

    Here's why: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Code Review for Obtaining UNIX HBA Information
    By deadpoet in forum C Programming
    Replies: 0
    Last Post: 01-14-2012, 12:02 PM
  2. program review
    By melestorm in forum C++ Programming
    Replies: 1
    Last Post: 11-12-2009, 05:30 AM
  3. Program review
    By MikeyVeeDubb in forum C Programming
    Replies: 1
    Last Post: 09-24-2009, 01:29 PM
  4. Need a quick review - Welcome comments
    By spirited in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2003, 11:34 PM
  5. quick question about getchar()
    By drharv in forum C Programming
    Replies: 3
    Last Post: 03-12-2002, 03:48 AM

Tags for this Thread