Thread: Explain this IF statement, please.

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    5

    Question Explain this IF statement, please.

    Please take a look at this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    //Tells computer when ARRAY_SIZE is used to automate 100
    #define ARRAY_SIZE 30
    
    
    
    int main(void){
    
        //int grade[ARRAY_SIZE];
        int *grade;
        char continueResponse; //Y/N
        int count = 0;
        int i; //loop conditional variable
    
    
    //allocates memory for the data
    grade =malloc(sizeof(int) * ARRAY_SIZE);
    if(grade == 0){
        exit(1); //return 1;
    }
    
    printf("This program will create a list of your grades.\nPress ENTER after each grade.\n\n");
    
    //input loop
    for(i = 0; i < ARRAY_SIZE; i++)
        {
        printf("Enter grade percentage(0-100) [%02d/%02d]: ", i+1, ARRAY_SIZE);
        scanf("%d",&grade[i]); //caps the inputs to array size of 30
        count++; //count=count + 1; increments to next position in array
        printf("Continue? (Y/N): ");
        scanf(" %c",&continueResponse);//reads response
    
        if(continueResponse != 'Y' &&continueResponse != 'y')
            {
            printf("\n == Grade Book ==\n\n");
            break;//break out of for loop
            }
        }
    
    printf("Grades entered: \n");
    
    for(i = 0; i < count; i++){
        printf("\t%d\n", grade[i]);
    }
    free(grade);
    
    return 0;
    }
    What is this section doing? How/why is the "1" variable there?

    Code:
    if(grade == 0){
        exit(1); //return 1;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,400
    1. malloc returns NULL when it fails to allocate the requested memory.

    2. A simple 0 is silently cast to NULL when used in a pointer context, such that if ( grade == 0 ) becomes if ( grade == NULL )

    3. The traditional return / exit from main is 0 for success or 1 for error.
    For the most part, exit(n) and return n; in main are the same thing.

    Most programs don't exist in isolation, so it's useful to be able to communicate success/failure to the environment.

    In a Linux/Bash environment, you could do this.
    Code:
    myprog.exe
    if [ $? == 0 ]; then
      echo "Your program was successful"
    else
      echo "Your program failed!"
    fi
    Where $? contains the value that your main exited with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    467
    Quote Originally Posted by Salem View Post
    3. The traditional return / exit from main is 0 for success or 1 for error.
    For the most part, exit(n) and return n; in main are the same thing.
    .
    You should exit(EXIT_FAILURE); rather than with a constant to be portable. However most OSes don't have a strong concept of a process "failing". It can be hard to define for many real programs.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Replies: 2
    Last Post: 12-10-2014, 07:57 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. Could someone explain nested-if statement to me?
    By shiyu in forum C Programming
    Replies: 13
    Last Post: 02-02-2003, 07:16 PM
  5. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM

Tags for this Thread