0% found this document useful (0 votes)
42 views8 pages

SRC 2 M

This document contains C source code examples for educational purposes. It includes programs demonstrating bugs like printing the wrong number of asterisks in a loop. It also contains examples showing floating point number truncation and imprecision. Additionally, it showcases functions with side effects like printing output, functions that return values, and a function that cubes a parameter value.

Uploaded by

Ursap Buddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

SRC 2 M

This document contains C source code examples for educational purposes. It includes programs demonstrating bugs like printing the wrong number of asterisks in a loop. It also contains examples showing floating point number truncation and imprecision. Additionally, it showcases functions with side effects like printing output, functions that return values, and a function that cubes a parameter value.

Uploaded by

Ursap Buddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

buggy-0.

c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

/****************************************************************************
* buggy-0.c
*
* David J. Malan
* [email protected]
*
* Should print 10 asterisks but doesn't!
***************************************************************************/
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 10; i++)
printf("*");
}

buggy-1.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

/****************************************************************************
* buggy-1.c
*
* David J. Malan
* [email protected]
*
* Should print 10 asterisks, one per line, but doesn't!
***************************************************************************/
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 10; i++)
printf("*");
printf("\n");
}

floats-0.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-0.c
*
* David J. Malan
* [email protected]
*
* Tries to print 1/10 as a floating-point value.
*
* Demonstrates truncation.
*/
#include <stdio.h>
int main(void)
{
float f = 1 / 10;
printf("%.1f\n", f);
}

floats-1.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-1.c
*
* David J. Malan
* [email protected]
*
* Prints 1/10 as a floating-point value to one decimal place.
*
* Demonstrates division of floating-point values.
*/
#include <stdio.h>
int main(void)
{
float f = 1.0 / 10.0;
printf("%.1f\n", f);
}

floats-2.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-2.c
*
* David J. Malan
* [email protected]
*
* Prints 1/10 as a floating-point value to 28 decimal places.
*
* Demonstrates imprecision of floating-point values.
*/
#include <stdio.h>
int main(void)
{
float f = 1.0 / 10.0;
printf("%.28f\n", f);
}

function-0.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.

/**
* function-0.c
*
* David J. Malan
* [email protected]
*
* Prints a user's name.
*
* Demonstrates a function (not from a library) with a side effect.
*/
#include <cs50.h>
#include <stdio.h>
// prototype
void PrintName(string name);
int main(void)
{
printf("Your name: ");
string s = GetString();
PrintName(s);
}
/**
* Says hello to someone by name.
*/
void PrintName(string name)
{
printf("hello, %s\n", name);
}

function-1.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.

/**
* function-1.c
*
* David J. Malan
* [email protected]
*
* Demands that user provide a positive integer.
*
* Demonstrates use of a function (not from a library) with a return value.
*/
#include <cs50.h>
#include <stdio.h>
// prototype
int GetPositiveInt();
int main(void)
{
int n = GetPositiveInt();
printf("Thanks for the %i!\n", n);
}
/**
* Gets a positive integer from a user.
*/
int GetPositiveInt(void)
{
int n;
do
{
printf("Please give me a positive int: ");
n = GetInt();
}
while (n < 1);
return n;
}

return.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.

/****************************************************************************
* return.c
*
* David J. Malan
* [email protected]
*
* Cubes a variable.
*
* Demonstrates use of parameter and return value.
***************************************************************************/
#include <stdio.h>
// function prototype
int cube(int a);
int main(void)
{
int x = 2;
printf("x is now %i\n", x);
printf("Cubing...\n");
x = cube(x);
printf("Cubed!\n");
printf("x is now %i\n", x);
}
/**
* Cubes argument.
*/
int cube(int n)
{
return n * n * n;
}

You might also like