Quick review of my basic getchar() program

This is a discussion on Quick review of my basic getchar() program within the C Programming forums, part of the General Programming Boards category; Hi guys, I am learning how to use the getchar() function in <stdio.h>. I followed an example from Greg Parry's ...

  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
    287
    It will work, but is quite trivial.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    287
    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
    2,237
    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, 11:02 AM
  2. program review
    By melestorm in forum C++ Programming
    Replies: 1
    Last Post: 11-12-2009, 04: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, 02:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21