Sponsored Content
Top Forums Programming When to define functions in C? Post 302895167 by Lost in Cyberia on Friday 28th of March 2014 07:57:49 PM
Old 03-28-2014
When to define functions in C?

Hey everyone. So I'm looking at a few C programming resources, and it seems, by convention how you should write and define a function, is first declare it's existence before your main...then call it somewhere in your main, and then define after, at the end of the program? Is this necessary? I mean can't you declare AND define it in one block of code before your main function? So...
Code:
void exampleFunction ( example parameters..) 
{code code code
exit 0;};

instead of splitting it up :

Code:
void exampleFunction (example parameters..)

int main ()
{blah
void exampleFunction
 blah blah}

void exampleFunction (example parameters...)
{code code code
exit 0;}

It just seems easier to declare and define it all in one step before you even get to the main funtion rather than save it's definition for the end of the code?
 

8 More Discussions You Might Find Interesting

1. Programming

mysterious #define

in the header file orville.h, outside of the #ifdef #endif , there is the following #define JOB_CONTROL /* support job-control */ As you can see, the JOB_CONTROL macro has no value associated with it. Here is what I go when I ran grep on the entire source code. $ grep -iR... (6 Replies)
Discussion started by: frequency8
6 Replies

2. UNIX for Dummies Questions & Answers

#define in perl

Hi friends, I am not sure if perl questions can be raised here. :rolleyes: But I have a doubt if there is a way to do "#define" in perl, like in C. Does anyone know if it is feasible (without CPAN modules)? Thanks, Srini (7 Replies)
Discussion started by: srinivasan_85
7 Replies

3. Programming

#define

Hello, I would like to conditionaly comment in my code source some fields from arrays. So I use the property ## from the #define definition. my code: ... #define slet /##* #define etsl *##/ ... const T_SVT_ADLL_A653_DESC A_DESC = { { slet qwerty etsl SLICING,... (3 Replies)
Discussion started by: cypleen
3 Replies

4. Programming

help with #define in C

if i do this in C #define NUM 1234512345 then how come i cant print it out using int main(int argc, char **argv) { printf("%d\n", NUM); return 0; } well the result is -1219236538, why isnt it 1234512345 ? (7 Replies)
Discussion started by: omega666
7 Replies

5. Programming

#define in c

Hi, I had a head file, looks like #define MIN_NUM 10 #define MAX_NUM 10 is there any way to get "MAX_NUM" from 10? thanks. peter (9 Replies)
Discussion started by: laopi
9 Replies

6. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

7. Shell Programming and Scripting

Define variable from file.

HI I have file A.txt _1A _2A _3A _4A I want define all as different variable. $1A=_1A $2B=_2A $3C=_3A $4D=_4A Now i can use any variable in my script. (3 Replies)
Discussion started by: pareshkp
3 Replies

8. Shell Programming and Scripting

Define Variables

Hi, I just define the variable in script and use those script in another script but the variable not recognize. test1.sh #!/bin/bash DB="test_db" USR="test_user" PWD="test_pwd" HST="24.254.87.12" test2.sh #!/bin/bash ./test1.sh mysql -u $USR -p $PWD -h $HST... (2 Replies)
Discussion started by: fspalero
2 Replies
XtOffsetOf()															      XtOffsetOf()

Name
  XtOffsetOf - determine the byte offset of a field within a structure type.

Synopsis
  Cardinal XtOffsetOf(structure_type, field_name)

Inputs
  structure_type
	    Specifies a type that is declared as a structure.

  field_name
	    Specifies the name of a field of the structure.

Returns
  The offset in bytes of the specified field from the beginning of the specified structure.

Availability
  Release 4 and later.

Description
  XtOffsetOf()	is  a  macro  that expands to a constant expression that gives the offset in bytes to the specified structure member from the
  beginning of the structure.

Usage
  XtOffset() is usually used to determine the location of an variable within a structure when initializing a resource list.  It  is  used  by
  widget writers, and anyone who needs to fetch application resources with XtGetApplicationResources().  Resource fields are defined in terms
  of offsets from a base address from the beginning of a widget.  Thus, a resource value can be kept up to date by the Resource Manager with-
  out any knowledge of the instance structure of the widget; it uses just a relative byte offset.

  XtOffsetOf()	is  slightly  more portable than XtOffset() which performs the same function but takes a pointer type rather than a structure
  type.

Example
  XtOffsetOf() is used to declare a resource list for the Athena Label widget:

     #define offset(field) XtOffsetOf(LabelRec, field)
     static XtResource resources[] = {
	 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
	     offset(label.foreground), XtRString, XtDefaultForeground},
	 {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
	     offset(label.font),XtRString, XtDefaultFont},
		 .
		 .

Background
  XtOffsetOf() is defined in terms of XtOffset() on many systems:

     #ifdef offsetof
     #define XtOffsetOf(s_type,field) offsetof(s_type,field)
     #else
     #define XtOffsetOf(s_type,field) XtOffset(s_type*,field)
     #endif

See Also
  XtOffset(1).

Xt - Utilities															      XtOffsetOf()
All times are GMT -4. The time now is 10:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy