Hi. I am new in C programming. I am tasked to complete a program. The hardest part in programming is to understand the logic of a program. Please explain to how and why the algorithm works. I have commented the parts that i need know of/needs help in green. Your help is so much appreciated. Thanks.

These are the requirements:
Complete the following C program so that it displays the addresses of

1. functions addr, f1, f2, and main;
2. all string literals such as "Hello, world!";
3. all initialized global variables;
4. all un-initialized global variables;
5. all dynamically allocated memories;
6. all formal parameters in functions;
7. all local variables;
8. start and end of its command line arguments;
9. start and end of its environment (variables and values).

The sample program is shown below.

// To declare package/lib
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

extern char **environ;

int global_x = 10; // initialised global variable
int global_y; // un-initialised global variable
char global_array1[] = "Hello, world!"; // initialised global array
char global_array2[10]; // un-initialised global array
char *global_pointer1 = "bye!"; // global pointer to a string literal
char *global_pointer2; // un-initialised global pointer
float global_float = 100.1; // initialised global variable
double global_double; // un-initialised global variable

// To define global variable
#define ONEGB 1073741824
#define ONEMB 1048576
#define ONEKB 1024

char *addr(unsigned long a)
unsigned long r; // remainder

/* ---> Why must does division have to do with finding a address??
r = (unsigned long) a;
int gb = (int) ( r / ONEGB );

r -= gb * ONEGB;
int mb = (int) ( r / ONEMB );

r -= mb * ONEMB;
int kb = (int) ( r / ONEKB );

r -= kb * ONEKB;
int b = (int) ( r );

// To dynamically allocated memory
char *p = malloc(64);

// To store value in p. --> Why does"%4dGB" means and how it works. Why can't
to be just %d??
sprintf(p, "%4dGB, %4dMB, %4dKB, %4d", gb, mb, kb, b);

return p;

int f2(int x)
char * f2_p;
int f2_x = 21;

f2_p = malloc(1000);

// A BLANK part. What do i need to do here????

// What is L:?? Is it a variable to be declared in the BLANK part?
L: f2_x = 10;
return f2_x;

// To declare a function
void f1(int x1, int x2, float x3, char x4, double x5, int x6)
int f1_x = 10;
int f1_y;
char *f1_p1 = "This is inside f1"; // Create pointer to another string
char *f1_p2;

f1_p2 = malloc(100);
..... // What do i need to do here??

f1_y = f2(10);

main(int argc, char *argv[])
/* What do i need to do here??

f1( .... );


6 Years
Discussion Span
Last Post by chrjs

the %4d is saying that the string that this represents cannot exceed 4 characters, while %d would be as many characters as necessary.

r = (unsigned long) a;
int gb = (int) ( r / ONEGB );

r -= gb * ONEGB;
int mb = (int) ( r / ONEMB );

r -= mb * ONEMB;
int kb = (int) ( r / ONEKB );

r -= kb * ONEKB;
int b = (int) ( r );

This part is determining the size of something in a gigabytes, megabytes, kilobytes, bytes format when the size is given simply in bytes (the variable a). It is not finding any addresses.

It divides the total bytes by the number of bytes in a gigabyte to find the number of gigabytes (the result of the division is rounded when casted to an int). Then subtracts the number of bytes represented by however many gigabytes it found to get the remainder of the division and uses that to calculate the amount of kilobytes and so on.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.