|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/may26/helloName.c
/*
This is a modified version of the "Hello, World! program.
Instead of just saying, "Hello, world!" it asks for your name, and
then says Hello, <YourNameHere>! using scanf
*/
#include <stdio.h>
int main() {
char name[30];
/*
above, we've created a character array called "name" with enough room for 30 characters
in practice, you'd only want to use up to 29 characters because at the very end of the
string there is a character that is used to tell the computer that this is the end of
the string. if you don't do this, the computer will think that the string goes on for longer
than it actually does. this is called the "null terminator" and it denoted with the '\0' character
*/
printf("Who is this? "); //prompting for a name
scanf("%s", name);
/*
we're telling scanf to expect a string. unlike integers, there is no need to put
an & symbol in front of the variable.
*/
printf("Hello, %s!\n", name); //prints out the name
return 0;
/*
there is an issue with scanf, namely that scanf doesn't handle spaces very well,
in this case, it will only print the first word and ignore everything after the first space.
the other files in this directory explore how to do the same thing using some other input functions
that handle strings.
*/
}
|