- Richard Feynman -
C FAQ
Here's a list of frequently asked question and answers regarding C
Contents
Compiling and executing
Q: How do I start my program (written in C)?
A: Check out Running_your_program
Q: Can I make gcc "talk" swedish?
A: Well, you can set Swedish for the entire terminal/bash. Type export LANG='sv_SE.UTF-8'
in the terminal/bash and you'll get Swedish until you close the terminal/bash. If you want to use Swedish in the future, put the following in the file .bashrc
in your cygwin home folder: export LANG='sv_SE.UTF-8'
Q: (Swedish) Kan jag få gcc att "prata" svenska?
A: Nja, du kan sätta om språket för terminal/bash. Skriv export LANG='sv_SE.UTF-8'
i terminalen/bash så blir det på svenska till du stänger ned terminalen/bash. Om du vill att det skall vara svenska jämt, lägg till följande i filen .bashrc
som finns i din hemkatalog för cygwin: export LANG='sv_SE.UTF-8'
Q: Can I ask gcc to check for more possible errors when compiling?
A: Yes, we suggest the following options:
$ gcc -g -pedantic -Wconversion -Wall -Werror -Wextra -Wstrict-prototypes -fstack-protector-strong
Or use a program such as splint.
Printing
Q: How do i print the value of a variable
A: Check out Easy printing of variables
Q: How do i print the address of a variable
A; printf("%p\n", p );
assuming p is a pointer variable. Also, check out Easy printing of variables. For more information about the &
operator, check this page Chapter:C_Pointers
Q: How do I print variables of other type than int?
A: Check here: Easy_printing_of_variables and OpenGroups printf page.
Q. Can I print the variable name and value a bit easier?
A. You can use the following macro:
#define PRINT_INT_INFO(a) \
printf ("[variable] name:%s value:%d address:%p size:%lu bytes\n", \
#a, a, (void*)&a, sizeof(int));
Here's an example on how to use it:
int main(void)
{
int a=12;
PRINT_INT_INFO(a);
return 0;
}
which (when executed) prints out the following:
[variable] name:a value:12 address:0x7ffe6935088c size:4 bytes
Well, we agree that the macro above is not optional. One macro per type? Really? Well yes, to do the value printout we need to know what format string ("%d" in the case of int
) to use. We could write the macro like this (adding macro to print long
as well):
#define PRINT_VAR_INFO(fmt, a) \
printf ("[variable] name:%s value:" fmt " address:%p size:%lu bytes\n", \
#a, a, (void*)&a, sizeof(a));
#define PRINT_INT_INFO(a) PRINT_VAR_INFO("%d", a)
#define PRINT_LONG_INFO(a) PRINT_VAR_INFO("%ld", a)
.. and "of course", if we want to disable all printouts with the macro we can write like this:
#ifdef USE_DEBUG_PRINTOUTS
#define PRINT_VAR_INFO(fmt, a) \
printf ("[variable] name:%s value:" fmt " address:%p size:%lu bytes\n", \
#a, a, (void*)&a, sizeof(a));
#else
#define PRINT_VAR_INFO(fmt, a)
#endif
#define PRINT_INT_INFO(a) PRINT_VAR_INFO("%d", a)
#define PRINT_LONG_INFO(a) PRINT_VAR_INFO("%ld", a)
To enable the printing, compile with -DUSE_DEBUG_PRINTOUTS
To disable the printing, compile without -DUSE_DEBUG_PRINTOUTS