Initial commit of lecture code

This commit is contained in:
Louise Brown
2022-04-07 16:58:01 +01:00
commit 5b205092a7
60 changed files with 2413 additions and 0 deletions

22
LC7/fgets_string.c Normal file
View File

@@ -0,0 +1,22 @@
/* EEEE1001/H61CAE*/
/* A program to show how to use fgets to safely read in a string */
#include <stdlib.h>
#include <stdio.h>
int main()
{
char MyName[50]; /* Define a string of 10 characters */
/* A string */
printf("\nPlease enter your name & press return ");
fgets(MyName, 80, stdin);
printf("\nHello %s ",MyName);
return 0; /* End of the program */
}

27
LC7/getch.c Normal file
View File

@@ -0,0 +1,27 @@
/*
Chapter 7: example of getch
This program reads a single keystroke and displays it
on the screen
*/
#include <stdio.h> /* Standard include files */
#include <stdlib.h>
#include <conio.h>
int main(void)
{
char x; /* Define a variable of type char */
x = getch(); /* Use getchar to read a keypress and store the */
/* result in 'x' */
putchar(x); /* Display the character stored in 'x' on the */
/* screen using putchar */
return 0;
}

27
LC7/getchar.c Normal file
View File

@@ -0,0 +1,27 @@
/*
Chapter 7: example of getchar
This program reads a single keystroke and displays it
on the screen
*/
#include <stdio.h> /* Standard include files */
#include <stdlib.h>
int main(void)
{
char x; /* Define a variable of type char */
x = getchar(); /* Use getchar to read a keypress and store the */
/* result in 'x' */
putchar(x); /* Display the character stored in 'x' on the */
/* screen using putchar */
return 0;
}

22
LC7/gets_string.c Normal file
View File

@@ -0,0 +1,22 @@
/* EEEE1001/H61CAE - Chapter 7 */
/* A program to show how to use gets to read a string (that can include spaces) */
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char MyName[50]; /* Define a string of 10 characters */
/* A string */
printf("\nPlease enter your name & press return ");
gets(MyName);
printf("\nHello %s ",MyName);
return 0; /* End of the program */
}

36
LC7/scanf_examples.c Normal file
View File

@@ -0,0 +1,36 @@
/* Chapter 7 : Examples of using scanf */
/* A program to show how to use scanf to read various types of values (float/integer)*/
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
int v1; /* An integer */
float fv; /* A float */
/* Reading an integer */
printf("\nPlease enter an integer and press return ");
scanf("%d",&v1);
printf("\nThe value you entered was %d ",v1);
/* Reading a floating point number */
printf("\nPlease enter a floating point number & press return ");
scanf("%f",&fv);
printf("\nThe value you typed in was %f ",fv);
/* Reading all three in one go */
printf("\nPlease enter your age and salary\n");
scanf("%d %f", &v1, &fv);
printf("\nYou are %d years old ",v1);
printf("\nAnd earn %f per year ",fv);
return 0; /* End of the program */
}

23
LC7/scanf_string.c Normal file
View File

@@ -0,0 +1,23 @@
/* EEEE1001/H61CAE - Chapter 7 */
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char MyName[50]; /* Define a string of 50 characters */
/* Prompt for and read in a string */
printf("\nPlease enter your name & press return ");
scanf("%s",MyName);
/* Display fixed text and string on the screen */
printf("\nHello %s ",MyName);
return 0; /* End of the program */
}