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-16 16:44

>>80
Hello, RMS.

Name: Anonymous 2010-07-16 18:52

>>80
Most GNU programs aren't portable. Non-portability is one of the four main points of the definition of GNU quality:
1. Non-portability
2. Poor documentation
3. Non-standard behavior for standard utilities
4. GPL

Name: Anonymous 2010-07-16 19:35

>>82

emacs

Name: Anonymous 2010-07-16 20:39

>>82
5. Use as many GCC extensions as possible.

Name: Anonymous 2010-07-16 20:58

>>84
i think that's covered by
1. Non-portability

Name: Anonymous 2010-07-17 4:26

Is there anything as a proper version?

Name: Anonymous 2010-07-17 13:34

>>86
Where would the fun in that be?

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);
}

Name: FrozenVoid 2010-07-19 13:26

>>88 External libraries? Not invented here.
#include "stdio.h"
#include "string.h"
#define s(x) (strlen(argv[(x)]))
main(int argc,char**argv){
if(argc!=3){puts("bad args");exit(3);};int i;
if(s(1)!=s(2)){puts(s(1)>s(2)?"1st":"2nd");exit(2);}
for(i=0;i<s(1);i++){if(argv[1][i]!=argv[2][i]){puts(argv[1][i]>argv[2][i]?"1st":"2nd");exit(4);}}}

Name: FrozenVoid 2010-07-19 13:40

#include "stdio.h"// >>89 unoptimized shit
#include "string.h"
main(int argc,char**argv){puts(argv[(strcmp(argv[1],argv[2])>0?1:2)]);}

__________________________
Orbis terrarum delenda est

Name: FrozenVoid 2010-07-19 13:54

>>90
Doesn't handle inequal lengths strings.

Name: FrozenVoid 2010-07-19 14:39

#include "stdio.h"//Fixed & incorporated >>90
#include "string.h"
#define s(x) (strlen(argv[(x)]))
main(int argc,char**argv){
if(argc==3){if(s(1)!=s(2)){puts(argv[s(1)>s(2)?1:2]);exit(2);}
puts(argv[(strcmp(argv[1],argv[2])>0?1:2)]);}}

Name: Anonymous 2010-07-19 19:04

>>34
You missed a + from the end

>>89-90,92
Where, may I ask, is void.h? Imposter!

Name: Anonymous 2010-07-19 19:52

>>88
You should have received a copy of the GNU General Public License along with
this program.

nice try, but you have failed.

#include <stdio.h>
#include <string.h>

#define USAGE(name) ({ printf("Usage: %s number number\n", (name)); exit(EXIT_FAILURE); })

int main(int argc, char *argv[argc])
{ char *name = (argv++)[0];
  if(argc != 3)
    USAGE(name);
  int len[2] = {0};
  int larger = 0;
  for(int i = 0; i < 2; ++i)
    for(int j = 0; argv[i][j];)
    { if(argv[i][j] < '0' || argv[i][j] > '9')
        USAGE(name);
      if(argv[i][j] == '0')
      { ++argv[i];
        --j; }
      else
      { if(i == 2 && !larger)
        { int t = argv[0][j] - argv[1][j];
          if(t) larger = (t < 0); }
        ++len[i]; }}
  int t = len[0] - len[1];
  ++argv;
  if(t)
    puts(argv[t < 0];
  else
    puts(argv[larger];
  return 0; }

Name: Anonymous 2010-07-20 1:59

How >>92 works? can someone explain?

Name: Anonymous 2010-07-20 2:11

>>95
1. It checks if the argc is 3, i.e. there 2 arguments(1st is the program name).
2. it uses strlen to compare if strings are inequal, and print the longest via ternary operator.
3. If it does not exit() it skips to last line(which means strings have equal length) and print one which strcmp selects as the largest(since strcmp compares each character sequentially) because 0<9 in ASCII any difference in sequence will make strcmp returns at the point where a character was different(larger in ASCII) and reports the difference as string1[y]<0>string2[x]. Where y/x are [0-9]
4. If the strings are completely equal, it returns string1.

Name: Anonymous 2010-07-20 2:20

>>95
it doesn't.
which is larger, 005 or 42?

Name: Anonymous 2010-07-20 2:21

>>94 forgot to #include <stdlib.h>.

Name: Anonymous 2010-07-20 13:57

>>97 is a faggot

Name: Anonymous 2010-07-20 16:58

>>99 is inferior coda

Name: YodaTheCoda 2010-07-20 17:07

>>100
no your the poppet

Name: sage 2010-07-20 17:11

>>89

int?

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

GNU MP is a portable library written in C for arbitrary precision arithmetic on integers, rational numbers, and floating-point numbers. It aims to provide the fastest possible arithmetic for all applications that need higher precision than is directly supported by the basic C types.

Name: Anonymous 2010-07-20 21:04

>>102
array subscripts in C are ints.

you're all doing it wrong, though. the obvious solution is to have the two numbers stored in files, so you don't run into command line length limits.

Name: Anonymous 2010-07-21 0:26

#include "stdio.h"//File ints comparator
#include "string.h"
#include "io.h"
#include "stdlib.h"
#define f(x,y) FILE* (x)=fopen((y),"rb")
#define fr(x,y) ;fread( &(x),4,1,(y));
#define filesz(x) filelength(fileno(x))
main(int argc,char**argv){
if(argc==3){f(int1,argv[1]);f(int2,argv[2]);
if(!int1||!int2){exit(10);}
if(filesz(int1)!=filesz(int2)){puts(argv[(filesz(int1)>filesz(int2)?1:2)]);exit(2);};int i,c,c2;
for(i=0;i<(filesz(int1)-(filesz(int1)%4));i+=4){fr(c,int1);fr(c2,int2);
if(c!=c2){puts(argv[(c>c2?1:2)]);exit(8);}
}

for(i=0;i<(filesz(int1)%4);i++){c=fgetc(int1);c2=fgetc(int2);if(c!=c2){puts(argv[(c>c2?1:2)]);exit(7);}}
puts(argv[1]);exit(2);
}}

Name: Anonymous 2010-07-21 0:45

#include "stdio.h"//Optimized
#include "string.h"
#include "io.h"
#include "stdlib.h"
#define f(x,y) FILE* (x)=fopen((y),"rb")
#define fr(x,y) ;fread( &(x),4,1,(y));
#define filesz(x) filelength(fileno(x))
main(int argc,char**argv){int i,c,c2,d,d2;if(argc==3){f(int1,argv[1]);f(int2,argv[2]);if(!int1||!int2){exit(10);};d=filesz(int1);d2=filesz(int2);
if(d!=d2){puts(argv[(d>d2?1:2)]);exit(2);};
for(i=0;i<(d-(d2%4));i+=4){fr(c,int1);fr(c2,int2);
if(c!=c2){puts(argv[(c>c2?1:2)]);exit(8);}};for(i=0;i<(d%4);i++){c=fgetc(int1);c2=fgetc(int2);if(c!=c2){puts(argv[(c>c2?1:2)]);exit(7);}};puts(argv[1]);exit(2);}}

Name: Anonymous 2010-07-21 1:08

#include "stdio.h"//Added cache for reading really long ints
#include "string.h"
#include "io.h"
#include "stdlib.h"
#define f(x,y) FILE* (x)=fopen((y),"rb")
#define fr(x,y) ;fread( &(x),4,1,(y));
#define filesz(x) filelength(fileno(x))
#define MAXCACHE 2<<16
main(int argc,char**argv){int i,c,c2,d,d2;if(argc==3){f(int1,argv[1]);f(int2,argv[2]);if(!int1||!int2){exit(10);};d=filesz(int1);d2=filesz(int2);
if(d!=d2){puts(argv[(d>d2?1:2)]);exit(2);};
setvbuf(int1,NULL,_IOFBF,MAXCACHE);
setvbuf(int2,NULL,_IOFBF,MAXCACHE);
for(i=0;i<(d-(d2%4));i+=4){fr(c,int1);fr(c2,int2);
if(c!=c2){puts(argv[(c>c2?1:2)]);exit(8);}};for(i=0;i<(d%4);i++){c=fgetc(int1);c2=fgetc(int2);if(c!=c2){puts(argv[(c>c2?1:2)]);exit(7);}};puts(argv[1]);exit(2);}}

Name: Anonymous 2010-07-21 7:17

>>103
Upgrade from bash if that's a problem for you.

Name: Anonymous 2010-07-21 11:01

#include "void.h"

If you're going to post C code and you're not #including "void.h" [b]you are totally doing it wrong.

Name: Anonymous 2010-07-21 14:08

>>108
I do #include <config.h>, is that good enough?

Name: Anonymous 2013-01-19 23:17

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Name: Anonymous 2013-09-29 18:42

kek'em trips

Name: Anonymous 2013-09-29 19:46

#include <ctype.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char a, b, *s, *t;
    int max;

    if (argc != 3) {
        fprintf(stderr, "usage: %s x y\n", argv[0]);
        return 1;
    }
    s = argv[1];
    t = argv[2];
    max = 0;
    while (*s || *t) {
        if (*s && !isdigit(*s)) {
            fprintf(stderr, "not an integer: %s\n", argv[1]);
            return 2;
        }
        if (*t && !isdigit(*t)) {
            fprintf(stderr, "not an integer: %s\n", argv[2]);
            return 3;
        }
        if (!*s)
            max = 2;
        else if (!*t)
            max = 1;
        else if (!max && *s < *t)
            max = 2;
        else if (!max && *s > *t)
            max = 1;
        if (*s)
            s++;
        if (*t)
            t++;
    }
    if (max)
        printf("%s\n", argv[max]);
    return 0;
}

Name: Anonymous 2013-09-29 22:13


i = raw_input("Enter num 1: ")
ii = raw_input("Enter num 2: ")

if i < ii:
    print "%r is larger" % i
else:
    print "%r is larger" % ii

Name: Anonymous 2013-09-29 22:19

>>113
Got that backwards on the last three lines.

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