wiki

Help! I'm trapped in a wiki!

Site Tools


lang:c

C

C is a versatile, mature and efficient language.

Expressions are a combination of operators and operands.
An expression followed by a semmicolon (“;”) is a statement (it is an action)

C concepts
Memory Management
Structs
Debug messages
Random numbers

Defines

In C you can define stuff that's quickly replaced by the preprocessor.

Let's say you have the following program:

#include <stdio.h>
int main()
{
	printf("Hello, World!\n");
#ifdef DEBUG
	printf("Secret debug info!\n");
#endif
	return 0;
}

Compile it with gcc -o prog -c prog.c -DDEBUG. Your program will print out the secret debug info in addition to its regular output.
This is useful for debugging and more.

Storage-class specifiers

Make a variable's storage class more explicit to the compiler.

  • static - block scope
    Block-scope static variables will keep their values between function calls. They're only initialized once at program startup, unlike regular function variables.
  • static - file scope
    File-scope static variables (outside of any function) are restricted to the current source file only. You can't access them from another source file.

Notes

  • Strings can be literals or arrays. Deep down, they're just pointers to the start of the char array.
    However, string literals are stored read-only and cannot be modified: char* s;
    If you want to make a mutable string, declare it as an array of characters: char s[];
  • The arrow operator (→) is used to access fields in a pointer to a struct.
    For example: thinkpad->price = 800; (thinkpad is a pointer to a struct laptop thinkpad struct)
    This is useful instead of writing it as (*thinkpad).price = 800;
  • Include <stdint.h> for fixed-size types such as uint64_t

See also

https://beej.us/guide/bgc/ → Great funny introduction to C
https://c-faq.com/index.html → C FAQs, very useful
https://www.learn-c.org/ → wip C guide

http://web.archive.org/web/20080217222203/http://c.snippets.org/ Useful public domain C snippets

https://www.andreinc.net/2023/02/01/demystifying-bitwise-ops#number-systems interesting article about binary operators and memory in general

https://verplant.org/oo_programming_in_c.shtml OOP?

lang/c.txt · Last modified: by 127.0.0.1