Will the following code compile?If yes, then is there any other problem withthis code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Will the following code compile?If yes, then is there any other problem withthis code?

#include<stdio.h> void main(void) { char *ptr = (char*)malloc(10); if(NULL == ptr) { printf("\n Malloc failed \n"); return; } else { // Do some processing free(ptr); } return;

27th Nov 2016, 10:54 AM
Aniket kinhikar
Aniket kinhikar - avatar
2 Answers
+ 1
No, it give you a good amount of error messages. Use, now a days int main() is used in c as well as c++. This is standard, you can search on google. Anyway #include <stdio.h> #include <stdlib.h> int main() { char *ptr = (char*)malloc(10); if(ptr==NULL) { printf("\n Malloc failed \n"); return 0; } else { // Do some processing free(ptr); } return 0; } Also There is no output in your program. if ptr is not null. And I want to add something. use if(!ptr) it us equivalent to if(ptr==NULL)
27th Nov 2016, 11:15 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
There wil be a warning not error... That we should use int return type instead of void.. And i appreciate your answer .. Thank you.
28th Nov 2016, 4:30 PM
Aniket kinhikar
Aniket kinhikar - avatar