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 - C Tutorial
From the course: C Programming Basics: Flow Control, Variables, and Pointers
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Exploring variables4m 55s
-
(Locked)
Understanding pointers4m 54s
-
(Locked)
Using pointers4m 14s
-
(Locked)
Challenge: Create a char pointer1m
-
(Locked)
Solution: Create a char pointer1m 16s
-
(Locked)
Accessing arrays with pointers6m 46s
-
(Locked)
Creating pointer functions4m 52s
-
(Locked)
Working with pointer arrays4m
-
(Locked)
Allocating memory4m 50s
-
(Locked)
Challenge: Write a Hello program28s
-
(Locked)
Solution: Write a Hello program1m 33s
-
-