Thursday 7 August 2014

Excerpts from "Head First C"

=====
scanf("%79[^\n]", line") 
It means read up to 79 characters, so long as they're not NEWLINES.
=====
For free text input use fgets().

char line[80];
fgets(line, 80, stdin);
printf("Your quote was: %s", line);
===== 
The stack is the section of memory used for local variable storage. Every time you call a function, all of the function's local variables are created on the stack. Variables are added to the stack when you enter a function, and get taken off the stack when you leave.
===== 
The heap is for dynamic memory - pieces of data that get created when the
progeam is running and then hang around a long time.
===== 
The code segment is the part of the memory where the actual assembled code gets loaded.
=====
printf("I like Turtles!"); is the simplified version of fprintf(stdout, "I like Turtles!");
=====
">" redirects the Standard Output. But "2>" redirects the Standard Error.
=====
i % 2 means "The remainder left when you divide by 2".
=====
FILE* in_file = fopen("input.txt", "r");
FILE* out_file = fopen("output.txt", "w");
fprintf(out_file, "Don't wear %s with %s", "red", "green");
fscanf(in_file, "%79[^\n]\n", sentence);
fclose(in_file);
fclose(out_file);
=====
int main(int argc, char* args[])
{
.... Do stuff....
}
The first argument contains the name of the program as it was run by the user.
=====
file accessibility sanity check:

FILE* in;
if (!(in = fopen("dont_exist.txt", "r"))) {
    fprintf(stderr, "Can't open the file.\n");
    return 1;
}
=====
After processing the arguments, the 0th argument will no longer be the program name.
=====
To avoid ambiguity, you can split your main arguments from the options using "--". So you would write "set_temperature -c -- -4". getopts() will stop reading options when it sees the "--", so the rest of the line will be read as simple arguments.
=====
When we tell the compiler about a function it's called a function declaration. C allows you to take that whole set of declarations out of your code and put them in a header file.
=====
When the compiler sees an include line with angle brackets it assumes it will find the header file somewhere off in the directories where the library code lives. But our header file is in the same directory as our .c file. By wrapping the header file name in quotes we are telling the compiler to look for a local file.
=====
What if you want to share variables? Source code files normally contain their own separate variables to prevent a variable in one file affecting a variable in another file with the same name. But if you genuinely want to share variables, you should declare them in your header file and prefix them with the keyword extern.
=====
Every file that make compiles is called a target.
For every target, make needs to be told two things:
  1. The dependencies: which files the target is going to be generated from.
  2. The recipe: the set of instructions it needs to run to generate the file.
Together the dependencies and the recipe form a rule.
=====
All the recipe lines MUST begin with a TAB character. If you just try to indent the recipe lines with spaces, the build won't work.
=====
Make takes away a lot of the pain of compiling files. But if you find that even make is not automatic enough, take a look at a tool called autoconf:
http://www.gnu.org/software/autoconf/. Autoconf is used to generate makefiles.
=====
structtypedef struct {
     int cell_no;
     const char * wallpaper;
     float minutes_of_charge;
} phone;
phone p = {5557879, "s.png", 1.35};








 

No comments:

Post a Comment