Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C program

Name: Anonymous 2010-07-13 18:06

Make me a C program that reads given two integers (no limits) and outputs the largest.

Do this in a proper GNU hello.c way.

Name: Anonymous 2010-07-19 12:02


/*
 largest.c -- compares two integers, prints the largest and exit.

 Copyright 2010 Kao Moji.

 This program is free software: you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE. See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 this program.  If not, see <http://www.gnu.org/licenses/>;.
*/

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

int main(int argc, char *argv[])
{
    if (argc < 3) {
  fprintf(stderr, "number of args fail\n");
  exit(EXIT_FAILURE);
    }
    mpz_t a;
    if (mpz_init_set_str(a, argv[1], 10) == -1) {
  mpz_clear(a);
  fprintf(stderr, "1st arg fail\n");
  exit(EXIT_FAILURE);
 }
    mpz_t b;
    if (mpz_init_set_str(b, argv[2], 10) == -1) {
  mpz_clear(b);
  fprintf(stderr, "2nd arg fail\n");
  exit(EXIT_FAILURE);
 }
    if (mpz_out_str(stdout, 10, mpz_cmp(a, b) > 1 ? a : b) == 0) {
  mpz_clear(a);
  mpz_clear(b);
  fprintf(stderr, "comparison fail\n");
  exit(EXIT_FAILURE);
 }
 if (ferror(stdout)) {
  mpz_clear(a);
  mpz_clear(b);
  fprintf(stderr, "printf fail\n");
  exit(EXIT_FAILURE);
 }
 mpz_clear(a);
 mpz_clear(b);
 exit(EXIT_SUCCESS);
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List