Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is a subroutine in C language?

Status
Not open for further replies.

subdural

Junior Member level 1
Joined
Aug 21, 2004
Messages
19
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Neutral Zone
Activity points
173
Hi

I'm newbie. I need to know what is the subroutine in C language?
For example I have C source code name main1.c as below;

main()

{

int a = 0x12345678, b;
b = endswap(a);
printf("Existing value in hex: %X\n", a);
printf("Endian-swapped value in hex: %X\n", b);

}

How to link the subroutine with other C source code named endswap.c

Thanks in addvanced
 

Re: subroutine in C

subroutines are called functions in C language.

in your code "endswap" & "printf" are functions (same as subroutines)

"printf" is a standard library function.

if you are using a any GUI including TC, you have to create a project and add all C files to the project. for example main.c and endswap.c

If you are using a commnadline compiler like gcc, you have to supply all files as arguments like

gcc main.c endswap.c "or"

gcc -test main.c endswap.c (here output exe/out file name will be test)


Cheers
idlebrain
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

You can also put multiple C functions into one source file, like this:

Code:
#include <stdio.h>

int endswap(int x)
{
  return x*x;     /* or whatever you want to do */
}

int main(void)
{
  int a = 0x12345678, b;

  b = endswap(a);
  printf("Existing value in hex: %X\n", a);
  printf("Endian-swapped value in hex: %X\n", b);
  return 0;
}
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

Hi Idlebrain,

Excellent information.
Since the subroutine is function in C, in main.c source code assume that a=0x12345678 is little-endian (memory) and how to program the subroutine in endswap.c source code so that the output b = 78563412 in big-endian.
Should I write as below; (I'm using arm-elf-gcc compiler)

endswap()

{

int a = 0x78563412, b;
b = endswap(a);
printf("Existing value in hex: %X\n", a);
printf("Endian-swapped value in hex: %X\n", b);

}
 

subroutine in C

I think you can put it by using include.

#include <endswap.c>
main()

{

int a = 0x12345678, b;
b = endswap(a);
printf("Existing value in hex: %X\n", a);
printf("Endian-swapped value in hex: %X\n", b);

}
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

here is the code. the endswap function converts little endian to big endian and vice versa.
It is very simple for understanding.

in endswap.c
Code:
int endswap(int number)
{
	unsigned char *bp1, *bp2;
	int rev;

	bp1 = (char *)&number;
	bp2 = (char *)&rev;

	bp2[0] = bp1[3];
	bp2[1] = bp1[2];
	bp2[2] = bp1[1];
	bp2[3] = bp1[0];

	return rev;

}

in main.c
Code:
#include <stdio.h>

int endswap(int); /* function prototype */

void main(void)
{
	int no = 0x12345678;
	printf("before = %X\n",no);
	no = endswap(no);
	printf("After  = %X\n",no);
}

pico said:
I think you can put it by using include.

#include <endswap.c>
Offcourse Yes, But it is not recomonded to include c files.

Cheers
idlebrain
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

Hi Idle,

Appriecite you endswap function. However when I compile and link between main.c and endswap.c I got these errors. Could help me to solved it.

$ arm-elf-gcc main.c endswap.c -o assn3a.exe

endswap.c: In function `main':
endswap.c:20: warning: return type of 'main' is not `int'
/cygdrive/c/DOCUME~1/NAZRI~1.WAR/LOCALS~1/Temp/ccSgudxp.o(.text+0xa4): In functi
on `main':
: multiple definition of `main'
/cygdrive/c/DOCUME~1/NAZRI~1.WAR/LOCALS~1/Temp/ccswBXjg.o(.text+0x0): first defi
ned here
/cygdrive/c/program files/gnuarm/bin/../lib/gcc/arm-elf/3.4.3/../../../../arm-el
f/bin/ld: Warning: size of symbol `main' changed from 88 in /cygdrive/c/DOCUME~1
/NAZRI~1.WAR/LOCALS~1/Temp/ccswBXjg.o to 80 in /cygdrive/c/DOCUME~1/NAZRI~1.WAR/
LOCALS~1/Temp/ccSgudxp.o
collect2: ld returned 1 exit status
 

Re: subroutine in C

May be the return type of main startup code is defined as int.
Try this in main.c file.
Code:
#include <stdio.h> 

int endswap(int); /* function prototype */ 

int main(void) 
{ 
   int no = 0x12345678; 
   printf("before = %X\n",no); 
   no = endswap(no); 
   printf("After  = %X\n",no); 

   return 0;
}
If it is not working first try a simple hello world
program.
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

Hi Idlebrain,

Good job! I managed to compile and get the expected output. However, could you explain how this function operate/work? Thanks
 

Re: subroutine in C

The Basics

"Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte at the highest address. (The little end comes first.) For example, a 4 byte LongInt

Byte3 Byte2 Byte1 Byte0


will be arranged in memory as follows:
Base Address+0 Byte0
Base Address+1 Byte1
Base Address+2 Byte2
Base Address+3 Byte3

Intel processors (those used in PC's) use "Little Endian" byte order.

"Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. (The big end comes first.) Our LongInt, would then be stored as:

Base Address+0 Byte3
Base Address+1 Byte2
Base Address+2 Byte1
Base Address+3 Byte0

Motorola processors (those used in Mac's) use "Big Endian" byte order.

The Algorithm
as per the above expl, we need to swap the bytes as follows
BA+0 to BA+3,BA+1 to BA+2,BA+2 to BA+1,BA+3 to BA+0.
This is done by taking two char(byte) pointers bp1 = BA of actual endian, bp2 = BA of other endian. Now byte locations are swaped as required.

I hope it is clear.
idlebrain
 

    subdural

    Points: 2
    Helpful Answer Positive Rating
Re: subroutine in C

Hi

Good information and self explanation. However the endswap function seem like array.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top