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