2

I am not so sure when the function printf in C will return a negative number. I know that printf will return a negative number, most likely -1, when it has failed to print a string. But I want to know why and when it will fail to print a string. For instance my code here:

    #include<stdio.h>
   #include<stdlib.h>
   #include<string.h>
#pragma pack(1)
typedef struct{
    char name[14];
    int age;
    int yearofbirth;
}person;
int print(person var)
{
    if(printf("%s\n", var.name)<0)
    {
        if(printf("%d\n", var.age)<0)
        {
           if(printf("%d\n\n", var.yearofbirth)<0)
           {
                return 1;
           }
        }

    }
    return 0;
}
int main(void)
{
    int i=0;
    person thing;
    FILE* file=fopen("Data.txt", "r");
    if(file)
    {
        long amountofstruct = 0;
        char* z=(char*)malloc(sizeof(char)*200);
        while(fgets(z,100,file)!=NULL)
        {
            amountofstruct++;
        }
        fseek(file,0,SEEK_END);
        free(z);
        int k=0;
       /* while(k<amountofstruct)
        {
            fseek(file,sizeof(person)*k,SEEK_SET);
            fread(&function[k], sizeof(person),1,file);
            k++;
        }
        int szt;
        printf("%p",function);
        for(szt=0;szt<amountofstruct;szt++)
        {
            printf("Person %d:\tName:%s\t Age:%i\t Year Of Birth:%i\n",szt+1,function[szt].name,function[szt].age,function[szt].yearofbirth);
        }*/
        printf("thesizeofstruct:%ld\n",sizeof(person));
        printf("%ld",amountofstruct);
//thing.name = {' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};

        char *l = (char*)malloc(11);
        while(k<amountofstruct)
        {

            fseek(file,sizeof(person)*k,SEEK_SET);
            if(fread(&thing, sizeof(thing), 1, file)!=0)
            {
                thing.name[13]='\0';
               // printf("sizeof(thing string)%lu\n sizeof(thing)%lu\n", sizeof(thing.name),strlen(thing.name));
                if(print(thing)==0)
                {
                    printf("\nThe thing failed, sorry");
                    return 0;
                }
                //char *p = strchr(thing.name, ' ');
               // *p='\0';
             //   strcpy(l,thing.name);
                //printf("Person %d: \t Name: %s\t Age: %d\t Year Of Birth: %d\n", k+1, l, thing.age, thing.yearofbirth);
        //        thing.name[0]='\0';
          //      thing.age=0;
           //     thing.yearofbirth=0;
                k++;
            }
        }
        free(l);
    }
    else
    {
        perror("The file could not be opened");
    }
    getchar();
    return 0;
}

the printf printing the integers var.age and var.yearofbirth in function print always returns a negative number. The line

printf("%s\n", var.name);

in function print succeeds, but that's all. But in my Data.txt, the age and the year of birth of each person is very clearly provided as follows.

    Name   Age   YearofBirth

So why is it that printf would return a negative number? Thank You,

Aarony Jamesys
  • 222
  • 1
  • 3
  • 14
  • Possible duplicate of [Return value of printf() function in C](http://stackoverflow.com/questions/7055882/return-value-of-printf-function-in-c) – John3136 Oct 22 '16 at 11:45
  • 3
    Your two subsequent `printf`s aren't even executed if the first one succeeds and returns something positive. – tkausl Oct 22 '16 at 11:45
  • 1
    The [man page](https://linux.die.net/man/3/printf) states that `If an output error is encountered, a negative value is returned. `. If that happens, have a look at [errno](https://linux.die.net/man/3/errno) to find out what went wrong. And a gentle request: next time please clean up your code before you post, these commented out blocks have nothing to do with the problem you are having, and it makes it hard to read the code, and looks untidy. – fvu Oct 22 '16 at 11:50
  • Because the condition is `false` for numbers bigger than or equal to zero, hence after the first `printf` which succeeds and returns some positive number, the condition is false and you return `0`. – tkausl Oct 22 '16 at 11:55
  • @tkausl can you explain more about conditions being false? I still do not understand. What do you mean conditions? – Aarony Jamesys Oct 22 '16 at 11:59
  • @rezeal "Condition" is the thing between `if (` and `)`. – melpomene Oct 22 '16 at 12:06

1 Answers1

2

printf(3) will return -1 for example, if you fclose(3) the FILE descriptor associated to it prior to calling printf(). Another example is if some other process has revoked your file descriptor, or if the disk gets full of data, or if you overpass your disk quota, or if the socket closes by some reason (case stdout is associated to a socket). There are plenty of things that can make the printf(3) family of functions to return -1. Normally that is associated to external events associated to the system calls it executes internally. Suppose you are printf() to a pipe, and the reading process (the one that receives your data) suddenly dies. Your printf() will be unable to write(2) the buffer data and receive an error from write(2), then it will return -1 and you'll have to check errno for the actual cause of error.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31