Question:
C programming question: declaring array at top level & value of main() parameter?
Dfdsf
2013-02-26 21:17:40 UTC
"Discuss this statement: If a program has
five functions that manipulate an array of data values, it makes more sense to
declare this array at the program’s top level so that each function does not need to have an array parameter."

Hm...I'd agree with this because this way the code-writing process is more efficient? Less prone to errors?

When function main() of a C program has a non-void parameter list, why is the value of its first parameter never less than 1?

Is it because or else the string will be empty?
Three answers:
Richard
2013-02-27 03:15:39 UTC
1.

Defining a array at the start of a program outside any function (including main), declares it as a global variable. It will be valid in all functions (including main) unless a local variable with the same name is defined within a function.



This is useful if a function always accesses the same array. However, if you have a function that is used with different arrays (or other variables) from different calls within your program, then it is better to pass the variable or a pointer to the variable to the called function.



2.

A C program has a list of parameters that count from 0 upwards. For example, with a program called my_prog:



my_prog param_A param_B



Parameter 0 is "my_prog"

Parameter 1 is "param_A"

Parameter 2 is "param_B"



Parameter 0 is always the name that was used to invoke the program. In some cases in UNIX/Linux systems, where two utility programs do very similar jobs, the same executable is used, with two different outcomes depending upon which name was used to invoke it. The program checks what name was used and reacts accordingly.



The Linux "busybox" program is a single executable that performs many different utility functions. In DSL Linux there are around 145 system utilities that can be called simply by using symbolic links to /bin/busybox. The busybox executable selects what it has to do depending upon what name was used to call it. The list of utilities in my copy of DSL Linux busybox is:



[, [[, ar, arping, awk, basename,

bunzip2, busybox, bzcat, cal, cat, chgrp,

chmod, chown, chroot, clear, cpio,

cut, date, dd, df, diff, dirname, dmesg,

dos2unix, du, echo, egrep, eject,

env, expr, false, fdflush, fdformat, fdisk,

fgrep, find, fold, free, freeramdisk,

fsck.minix, getty, grep, gunzip,

gzip, head, hexdump, hostid, hostname,

hwclock, id, ifconfig, ifdown, ifup,

insmod, install, ip, ipcalc, kill,

killall, klogd, last, length, less,

ln, loadfont, loadkmap, logger, logname,

logread, ls, lsmod, md5sum, mesg,

mkdir, mkfifo, mkfs.minix, mknod,

mkswap, mktemp, modprobe, more, mount,

mv, netstat, nice, nohup, nslookup, od,

passwd, pidof, ping, printenv, printf,

ps, pwd, readlink, realpath, renice,

reset, rm, rmdir, rmmod, route,

rpm2cpio, run-parts, sed, setkeycodes,

sleep, sort, stat, strings, stty, sum,

swapoff, swapon, sync, syslogd, tail,

tee, test, time, top, touch, tr,

traceroute, true, tty, uname, uncompress,

uniq, unix2dos, unzip, uptime, usleep,

uudecode, uuencode, watch, wc, wget,

which, who, whoami, xargs, yes, zcat



The busybox executable is around 375 kB in size. This averages out at around 2.6 kB per utility. It also means there is space wasted on only one allocation unit of the file system rather than 145 potential allocation units if each function was coded separately.
?
2013-02-27 06:17:41 UTC
Probably yes. But you are still making a global variable so if the program changes a lot later it might later cause issues.
anonymous
2013-02-27 05:22:33 UTC
Ok, in binary, 1 = on, 0 = off.



Therefore, we use "1" to declare a function as on, and "0" as off.. I'm sure you know this.

So, if a value was set to "0" it would be off and worthless... Unless you wanted, for some reason, to come back and edit it later (such as not using it in a BETA test)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...