在c中打印垃圾值

print garbage value in c

我想做的是计算字母数字的数量,我正在使用指针。 一切正常,但是当我尝试打印值时,它打印了我想要的值+垃圾值,因为它保留了内存

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

 #define SIZE 10000 

 void printChar(int *p);

 int main(){

int *p,*new_p;
int c,i = 0,numOfAlNum = 0,numOfChar = 0;

p = (int*)malloc(SIZE * sizeof(int));

/*If memory cannot be allocated*/
if(p == NULL){
    printf("Error! memory not allocated\n");
    exit(0);
}
else
    printf("Memory successfully allocated\n");


printf("enter something\n");


while((c = getchar()) != '\n'){

    *(p + i) = c;
    i++;
    /*Add Realloc to the loop*/
    new_p = realloc(p,(i+1)*sizeof(int));
    
    /*check for ability to allocate new memory*/
    if(new_p == NULL){
        printf("Error! memory not allocated\n");
        exit(0);
    }else{
        p = new_p;  
    }
    
    /*Check is alphanumeric*/
    if(isalnum(c)){
        numOfAlNum++;
    }
    
    numOfChar++;    
}


printf("The output is: \n");
for(i = 0; i < SIZE; i++){
    printf("%s",(p+i));
}
printf("\nNumber of Characters is %d\n",numOfChar);
printf("Number of Alpha-Numeric is %d\n",numOfAlNum);


 return 0;

}

预期输出示例:“hello world”我得到的是:“hello world&^^^%^#” 我如何去掉最后不必要的值?

how do i get rid of the unnecessary values at the end ?

而不是打印到分配的大小 SIZE,
打印分配的分配部分:numOfChar.

使用"%c"打印单个字符。
"%s" 用于 字符串 空字符 终止字符数组。

//for(i = 0; i < SIZE; i++){
//  printf("%s",(p+i));
//}

for(j = i; i < numOfChar; i++){
  printf("%c",(p+i));
}