Assertions: you express your expectations in a certain position of the code, and this way you avoid bugs that would otherwise very difficult to trace.
In C use the standard library macro assert. You must #include <assert.h>, then you assert(expression); in the code, and if the expression is evaluating to 0 / false, the program aborts with an error msg printed on sterr that tells you which assertion fails and where it is (file/line).
It is also possible to turn assertions off, adding a #define NDEBUG before the #include <assert.h>.
This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.
Showing posts with label C basics. Show all posts
Showing posts with label C basics. Show all posts
Thursday, April 16, 2009
Tuesday, April 14, 2009
Why main is int and not void.
The standard says so. And it actually makes a difference: here is a detailed discussion about this topic: http://users.aber.ac.uk/auj/voidmain.shtml
Why do you have to pass scanf the address of the variable to write in?
As I was learning C the first time, I remember I was asking this myself... I would have preferred it to return the scanned values, something like:
myVar = scanf("%s") /* don't do this :) */
but of course, in this case (1) you could assign only 1 variable, (2) you would have not had the return value (number of read items)...
Here is some discussion of it: ...
Wednesday, April 11, 2007
Learning C: params from the command line
I want to read two parameters from the command line: a string and a number. The number should be a positive integer smaller than 100.
int main (int argc, char *argv[])
{
char * string;
long int number;
/* are there really 2 parameters? */
if (argc != 3) exit(EXIT_FAILURE);
/* catch them */
string = argv[1];
number = atoi(argv[2]);
/* use them */
printf("String parameter: %s\n",string);
printf("Number parameter+1: %d\n",number+1);
}
The number of parameters (as int) and the parameters itself (as char*) are passed to "main".
Everybody writes main as int main (int argc, char *argv), I guess it's possible to use more descriptive names to the arguments of main, like int main (int argument_counter, char *arguments) but it's probably out of fashion - if you write C you should look serious after all.
The classical beginner's note: It must be considered that argv[0] is the program name (what is useful for example for syntax error messages), so the counter is always actually one more than you intuitively expected and you find the first real parameter as argv[1].
Parameter type: as the parameters are actually always strings, you must convert them using some conversion functions like atoi or strtol (I guess).
Subscribe to:
Posts (Atom)
Labels
software installed on my laptop
(13)
windows software
(12)
personal settings
(10)
linux software
(8)
rails
(7)
shell rc file
(6)
linux basics
(5)
C basics
(4)
color the shell
(4)
ruby
(4)
connection to remote servers
(3)
git
(3)
mutimedia software
(3)
rake tasks
(3)
shell
(3)
active scaffold
(2)
backup
(2)
disk analysis
(2)
less
(2)
oracle
(2)
rails migrations
(2)
rails on oracle
(2)
sql
(2)
ssh
(2)
symbolic links
(2)
top open source projects
(2)
virtualbox
(2)
GUI programming
(1)
MIT
(1)
active record
(1)
algorithms
(1)
applications
(1)
apt
(1)
attr_hidden
(1)
bash
(1)
capistrano
(1)
cms tools
(1)
color picker
(1)
common errors
(1)
cooltips
(1)
data recovery
(1)
data structures
(1)
dvd43
(1)
endline chars
(1)
expression engine
(1)
ftp clients
(1)
gimp
(1)
grep
(1)
handbrake
(1)
hard disk failure
(1)
image conversion
(1)
image editing
(1)
inkscape
(1)
java software
(1)
javascript
(1)
jdiskreport
(1)
linux compact guides
(1)
ls
(1)
online lectures
(1)
oracle lob
(1)
photo manipulation
(1)
php
(1)
polymorphic associations
(1)
prototype
(1)
putty
(1)
rails plugins
(1)
rake
(1)
rsync
(1)
ruby blocks
(1)
scanf
(1)
scite
(1)
script
(1)
scripteka
(1)
sftp clients
(1)
sort_by :sql
(1)
suffix array
(1)
synctoy
(1)
tcsh
(1)
timms
(1)
unlocker
(1)
vector graphics
(1)
vi
(1)
video lectures
(1)
vlc media player
(1)
windows alpha blender
(1)
winscp
(1)
xslt xml
(1)
About Me
- Giorgio Gonnella
- Hamburg, Hamburg, Germany
- Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.