From the course: C Programming Basics: Flow Control, Variables, and Pointers

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Solution: Write a Hello program

Solution: Write a Hello program

(upbeat music) - [Instructor] For my solution, I created a constant expression size. Its value is set to 64 and I figure 64 bytes is enough to store a name. This value is used in the malloc statement at line 10. It sets the buffer size as an argument based on the size of a character variable. Pointer character variable name is declared at line eight. And the result of the malloc function is saved in that pointer, which is immediately tested for the null pointer, which is something I hope you didn't forget. If the pointer is null, the program displays a message and quits. Otherwise, the printf statement at line 17 prompts for input, and the fgets function at line 18 reads that input. The constant expression size is used to help limit input and ensure that the buffer doesn't overflow, then the results are displayed in line 19, build and run. I'll type my name and press enter and there it is, text stored…

Contents