Added book examples
This commit is contained in:
19
C7/getchar_example.c
Normal file
19
C7/getchar_example.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// declare variable
|
||||
char c;
|
||||
|
||||
// Wait for a kepypress, store result in c
|
||||
c = getchar();
|
||||
|
||||
// Display on the screen using printf
|
||||
printf ("The charcter pressed was %c\n", c);
|
||||
|
||||
// Display the charcter using putchar()
|
||||
putchar(c);
|
||||
|
||||
return 0; // Exit from main
|
||||
}
|
28
C7/scanf_example_1.c
Normal file
28
C7/scanf_example_1.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int a; // Declare some variables
|
||||
float f;
|
||||
|
||||
// Use printf to prompt the use to enter an integer
|
||||
printf ("Please enter an integer\n");
|
||||
|
||||
// use scanf with %d to read into 'a'
|
||||
scanf ("%d",&a); // note the important &
|
||||
|
||||
// And display on the screen
|
||||
printf ("The value you entered for a is %d\n", a);
|
||||
|
||||
// Use printf to prompt the use to enter an float
|
||||
printf ("Please enter a float\n");
|
||||
|
||||
// use scanf with %f to read into 'f'
|
||||
scanf ("%f",&f); // note the important &
|
||||
|
||||
// And display on the screen
|
||||
printf ("The value you entered for f is %f\n", f);
|
||||
|
||||
return 0; // Exit from main
|
||||
}
|
28
C7/scanf_example_2.c
Normal file
28
C7/scanf_example_2.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
int a,b,c;
|
||||
float f,g;
|
||||
|
||||
// Use printf to prompt the use to enter 3 integers
|
||||
printf ("Please enter three integer\n");
|
||||
|
||||
// use scanf with %d to read into a, b and c
|
||||
scanf ("%d %d %d",&a, &b, &c); // note the important &
|
||||
|
||||
// And display on the screen
|
||||
printf ("The values entered were %d %d %d\n", a, b, c);
|
||||
|
||||
// Use printf to prompt the use to enter an float
|
||||
printf ("Please enter two floats\n");
|
||||
|
||||
// use scanf with %f to read into f ang g
|
||||
scanf ("%f %f",&f, &g); // note the important &
|
||||
|
||||
// And display on the screen
|
||||
printf ("The value you entered were %f and %f \n", f,g);
|
||||
|
||||
return 0; // Exit from main
|
||||
}
|
19
C7/string_with_gets.c
Normal file
19
C7/string_with_gets.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{
|
||||
// Declare variable - maximum 99 characters
|
||||
char str[100];
|
||||
|
||||
// Prompt for text
|
||||
printf("enter some text\n");
|
||||
|
||||
// Store intput in str
|
||||
gets(str);
|
||||
|
||||
// Display a message on the screen
|
||||
printf("you entered : %s\n", str);
|
||||
|
||||
// Exit from main
|
||||
return 0;
|
||||
}
|
20
C7/string_with_scanf.c
Normal file
20
C7/string_with_scanf.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
char name[50];
|
||||
|
||||
// Wait for a kepypress, store result in c
|
||||
printf ("Please enter your name");
|
||||
|
||||
// read in using scanf with %s
|
||||
scanf ("%s",name);
|
||||
|
||||
// Display on the screen using printf
|
||||
printf ("Hello %s\n", name);
|
||||
|
||||
// Exit from main
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user