Initial commit
This commit is contained in:
BIN
ForNextDay/Lec5/Lec5src.zip
Normal file
BIN
ForNextDay/Lec5/Lec5src.zip
Normal file
Binary file not shown.
25
ForNextDay/Lec5/src/debug.c
Normal file
25
ForNextDay/Lec5/src/debug.c
Normal file
@ -0,0 +1,25 @@
|
||||
// pointing.c
|
||||
/*
|
||||
* Never dereference an unitialized pointer!
|
||||
*
|
||||
* Assigning a value to an unitialized pointer much worse!
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define DEBUG 0
|
||||
|
||||
int main(int argc, char * * argv)
|
||||
{
|
||||
int a = 5;
|
||||
int b = 17;
|
||||
int* pa;
|
||||
int* pb;
|
||||
// what happens if you leave these two statements out? Why?
|
||||
pa = &a;
|
||||
pb = &b;
|
||||
|
||||
#if DEBUG > 0
|
||||
printf("main: a = %d, b = %d, argc = %d\n", *pa, *pb, argc);
|
||||
#endif
|
||||
return EXIT_SUCCESS;
|
||||
}
|
18
ForNextDay/Lec5/src/forscope.c
Normal file
18
ForNextDay/Lec5/src/forscope.c
Normal file
@ -0,0 +1,18 @@
|
||||
// forscope.c
|
||||
/*
|
||||
* - Should this compile? Why/why not?
|
||||
* - What will the output be?
|
||||
* - Explain the output.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int i = 0;
|
||||
for(int i=0;i<5;i++){
|
||||
printf("%d\n",i);
|
||||
}
|
||||
printf("%d\n",i);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
28
ForNextDay/Lec5/src/ifscope.c
Normal file
28
ForNextDay/Lec5/src/ifscope.c
Normal file
@ -0,0 +1,28 @@
|
||||
// ifscope.c
|
||||
/*
|
||||
* Should this compile? Why/why not?
|
||||
* What will the output be if:
|
||||
* (i) i=1, j=2
|
||||
* (i) i=2, j=1
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int iErr;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
printf("Enter two integers: ");
|
||||
iErr = scanf("%d %d", &i, &j);
|
||||
if(iErr != 2)
|
||||
{
|
||||
fprintf(stderr,"Egads - something went wrong!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if(i>j){
|
||||
int i = 0;
|
||||
printf("%d\n",i);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
20
ForNextDay/Lec5/src/pointing.c
Normal file
20
ForNextDay/Lec5/src/pointing.c
Normal file
@ -0,0 +1,20 @@
|
||||
// pointing.c
|
||||
/*
|
||||
* Never dereference an unitialized pointer!
|
||||
*
|
||||
* Assigning a value to an unitialized pointer much worse!
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char * * argv)
|
||||
{
|
||||
int a = 5;
|
||||
int b = 17;
|
||||
int* pa;
|
||||
int* pb;
|
||||
// what happens if you leave these two statements out? Why?
|
||||
pa = &a;
|
||||
pb = &b;
|
||||
printf("main: a = %d, b = %d, argc = %d\n", *pa, *pb, argc);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
21
ForNextDay/Lec5/src/swap.c
Normal file
21
ForNextDay/Lec5/src/swap.c
Normal file
@ -0,0 +1,21 @@
|
||||
// swap.c
|
||||
/*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
void swap(int i, int j);
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i = 10;
|
||||
int j = 99;
|
||||
printf("i = %d; j = %d\n", i, j);
|
||||
swap(i,j);
|
||||
printf("i = %d; j = %d\n", i, j);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
void swap(int i, int j) {
|
||||
int swap;
|
||||
swap = i;
|
||||
i = j;
|
||||
j = swap;
|
||||
}
|
Reference in New Issue
Block a user