Can someone give me a hint as to why this isn't printing the array? I don't know what is wrong with my print function. I want to make certain it's working correctly before I add in the other parts to my code. I'm guessing I have not setup the array correctly & that's why nothing is printing out.
#define NUMSTU 50
#include <stdio.h>
//function prototype
void printdata();
//Global variables
int stuID[NUMSTU];
int stuCount;
int totStu;
int main ()
{
int stuCount = 0;
int totStu = 0;
int studentID;
//Prompt user for number of student's in class
printf("Please enter number of student's in class:");
scanf ("%d", &totStu);
for (stuCount = 0; stuCount <totStu; stuCount++)
{
//Prompt user for student ID number
printf("\n Please enter student's ID number:");
scanf("%d", &studentID);
stuID[NUMSTU] = studentID;
}
//Call Function to print data
printdata();
return 0;
}//end main
void printdata(){
//This function will display collected data
//Input: Globals stuID[NUMSTU]
//Output: none
//Display column headers
printf("\n\n stuID\n");
//loop and display student ID numbers
for (stuCount = 0; stuCount <totStu; stuCount++){
printf("%d", stuID);
}
}
stuID[NUMSTU] = studentID;has undefined behavior. You're writing to an out-of-bounds element.printf("%d", stuID);has undefined behavior.printf%dtakes anint, but you're passing anint *.totStu. Only one of them has a non-zero value.