C primer plus(第六版)第十一章源代码
#include <stdio.h>
#define MSG "I am a symbolic string constant."
#define MAXLENGTH 81
int main ( void )
{
char words[ MAXLENGTH] = "I am a string in an array." ;
const char * pt1 = "Something is pointing at me." ;
puts ( "Here are some strings:" ) ;
puts ( MSG) ;
puts ( words) ;
puts ( pt1) ;
words[ 8 ] = 'p' ;
puts ( words) ;
return 0 ;
}
#include <stdio.h>
int main ( void )
{
printf ( "%s, %p, %c\n" , "We" , "are" , * "space farers" ) ;
return 0 ;
}
#define MSG "I'm special"
#include <stdio.h>
int main ( void )
{
char ar[ ] = MSG;
const char * pt = MSG;
printf ( "address of \"I'm special\": %p \n" , "I'm special" ) ;
printf ( " address ar: %p\n" , ar) ;
printf ( " address pt: %p\n" , pt) ;
printf ( " address of MSG: %p\n" , MSG) ;
printf ( "address of \"I'm special\": %p \n" , "I'm special" ) ;
return 0 ;
}
#include <stdio.h>
#define SLEN 40
#define LIM 5
int main ( void )
{
const char * mytalents[ LIM] = {
"Adding numbers swiftly" ,
"Multiplying accurately" , "Stashing data" ,
"Following instructions to the letter" ,
"Understanding the C language"
} ;
char yourtalents[ LIM] [ SLEN] = {
"Walking in a straight line" ,
"Sleeping" , "Watching television" ,
"Mailing letters" , "Reading email"
} ;
int i;
puts ( "Let's compare talents." ) ;
printf ( "%-36s %-25s\n" , "My Talents" , "Your Talents" ) ;
for ( i = 0 ; i < LIM; i++ )
printf ( "%-36s %-25s\n" , mytalents[ i] , yourtalents[ i] ) ;
printf ( "\nsizeof mytalents: %zd, sizeof yourtalents: %zd\n" ,
sizeof ( mytalents) , sizeof ( yourtalents) ) ;
return 0 ;
}
#include <stdio.h>
int main ( void )
{
const char * mesg = "Don't be a fool!" ;
const char * copy;
copy = mesg;
printf ( "%s\n" , copy) ;
printf ( "mesg = %s; &mesg = %p; value = %p\n" , mesg, & mesg, mesg) ;
printf ( "copy = %s; © = %p; value = %p\n" , copy, & copy, copy) ;
return 0 ;
}
#include <stdio.h>
#define STLEN 81
int main ( void )
{
char words[ STLEN] ;
puts ( "Enter a string, please." ) ;
gets ( words) ;
printf ( "Your string twice:\n" ) ;
printf ( "%s\n" , words) ;
puts ( words) ;
puts ( "Done." ) ;
return 0 ;
}
#include <stdio.h>
#define STLEN 14
int main ( void )
{
char words[ STLEN] ;
puts ( "Enter a string, please." ) ;
fgets ( words, STLEN, stdin ) ;
printf ( "Your string twice (puts(), then fputs()):\n" ) ;
puts ( words) ;
fputs ( words, stdout ) ;
puts ( "Enter another string, please." ) ;
fgets ( words, STLEN, stdin ) ;
printf ( "Your string twice (puts(), then fupts()):\n" ) ;
puts ( words) ;
fputs ( words, stdout ) ;
puts ( "Done." ) ;
return 0 ;
}
#include <stdio.h>
#define STLEN 10
int main ( void )
{
char words[ STLEN] ;
puts ( "Enter strings (empty line to quit):" ) ;
while ( fgets ( words, STLEN, stdin ) != NULL && words[ 0 ] != '\n' )
fputs ( words, stdout ) ;
puts ( "Done." ) ;
return 0 ;
}
#include <stdio.h>
#define STLEN 10
int main ( void )
{
char words[ STLEN] ;
int i;
puts ( "Enter strings (empty line to quit):" ) ;
while ( fgets ( words, STLEN, stdin ) != NULL && words[ 0 ] != '\n' )
{
i = 0 ;
while ( words[ i] != '\n' && words[ i] != '\0' )
i++ ;
if ( words[ i] == '\n' )
words[ i] = '\0' ;
else
while ( getchar ( ) != '\n' )
continue ;
puts ( words) ;
}
puts ( words) ;
return 0 ;
}
char * s_gets ( char * st, int n)
{
char * ret_val;
int i = 0 ;
ret_val = fgets ( st, n, stdin ) ;
if ( ret_val)
{
while ( st[ i] != '\n' && st[ i] != '\0' )
i++ ;
if ( st[ i] == '\n' )
st[ i] = '\0' ;
else
while ( getchar ( ) != '\n' )
continue ;
}
return ret_val;
}