BNR Reading Notes1-How Programming Works

By prince No comments

Variables and Types

short, int, long
float, double
float, double
char
pointer
struct
float weight;
weight = 14.2;

if/else

if (conditional) {
// Execute this code if the conditional evaluates to true
} else {
// Execute this code if the conditional evaluates to false
}
< Is the number on the left less than the number on the right?
> Is the number on the left greater than the number on the right?
<= Is the number on the left less than or equal to the number on the right?
>= Is the number on the left greater than or equal to the number on the right?
== Are they equal?
!= Are they not equal?
&& Logical AND — true if and only if both are true
|| Logical OR — false if and only if both are false
! Logical NOT — true becomes false, false becomes true

Functions

int main (int argc, const char * argv[])
{
congratulateStudent(“Kate”, “Cocoa”, 5);
sleep(2);
congratulateStudent(“Bo”, “Objective-C”, 2);
sleep(2);
congratulateStudent(“Mike”, “Python”, 5);
sleep(2);
congratulateStudent(“Liz”, “iOS”, 5);
return 0;
}

Global and static variables

// Declare a global variable
float lastTemperature;
float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf(“%f Celsius is %f Fahrenheit.\n”, cel, fahr);
return fahr;
}
int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit.\n”, freezeInF);
printf(“The last temperature converted was %f.\n”, lastTemperature);
return EXIT_SUCCESS;
}
// Declare a static variable
static float lastTemperature;
// Initialize lastTemperature to 50 degrees
static float lastTemperature = 50.0;

Format Strings

// Write the beginning of the novel
char *firstLine = “It was the best of times.\n”;
char *secondLine = “It was the worst of times.\n”;
// Print the beginning of the novel
printf(firstLine);
printf(secondLine);

Numbers

An integer is a number without a decimal point – a whole number.
char a; // 8 bits
short b; // Usually 16 bits (depending on the platform)
int c; // Usually 32 bits (depending on the platform)
long d; // 32 or 64 bits (depending on the platform)
long long e; // 64 bits

Floating-point numbers

float g; // 32 bits
double h; // 64 bits
long double i; // 128 bits
The math library
#include <math.h>

Loops

The while loop
while (some check) {
some code
some last step
}
The for loop
for (some initialization; some check; some last step) {
some code;
}
break
continue
The do-while loop
do { something } while ( something else stays true );

Addresses and Pointers

memorycpu

Getting addresses

int main(int argc, const char * argv[])
{
int i = 17;
printf(“i stores its value at %p\n”, &i);
printf(“this function starts at %p\n”, main);
return 0;
}

Storing addresses in pointers

int main(int argc, const char * argv[])
{
int i = 17;
int *addressOfI = &i;
printf(“i stores its value at %p\n”, addressOfI);
printf(“this function starts at %p\n”, main);
return 0;
}

Getting the data at an address

int main(int argc, const char * argv[])
{
int i = 17;
int *addressOfI = &i;
printf(“i stores its value at %p\n”, addressOfI);
printf(“this function starts at %p\n”, main);
printf(“the int stored at addressOfI is %d\n”, *addressOfI);
return 0;
}

How many bytes?

Using sizeof() you can find the size of a data type.
int main(int argc, const char * argv[])
{
int i = 17;
int *addressOfI = &i;
printf(“i stores its value at %p\n”, addressOfI);
*addressOfI = 89;
printf(“Now i is %d\n”, i);
printf(“An int is %zu bytes\n”, sizeof(i));
printf(“A pointer is %zu bytes\n”, sizeof(addressOfI));
return 0;
}

Pass-By-Reference

#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
double pi = 3.14;
double integerPart;
double fractionPart;
// Pass the address of integerPart as an argument
fractionPart = modf(pi, &integerPart);
// Find the value stored in integerPart
printf(“integerPart = %.0f, fractionPart = %.2f\n”, integerPart, fractionPart);
return 0;
}

Writing pass-by-reference functions

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr)
{
// This function assumes meters is non-negative.
// Convert the number of meters into a floating-point number of feet
double rawFeet = meters * 3.281; // e.g. 2.4536
// How many complete feet as an unsigned int?
unsigned int feet = (unsigned int)floor(rawFeet);
// Store the number of feet at the supplied address
printf(“Storing %u to the address %p\n”, feet, ftPtr);
*ftPtr = feet;
// Calculate inches
double fractionalFoot = rawFeet – feet;
double inches = fractionalFoot * 12.0;
// Store the number of inches at the supplied address
printf(“Storing %.2f to the address %p\n”, inches, inPtr);
*inPtr = inches;
}

Structs

Sometimes you need a variable to hold several related chunks of data. In C, you can do this with a
structure, commonly called a struct. Each chunk of data is known as a member of the struct.
// Here is the declaration of the struct
struct Person {
float heightInMeters;
int weightInKilos;
};
int main(int argc, const char * argv[])
{
struct Person mikey;
mikey.heightInMeters = 1.7;
mikey.weightInKilos = 96;
struct Person aaron;
aaron.heightInMeters = 1.97;
aaron.weightInKilos = 84;
printf(“mikey is %.2f meters tall\n”, mikey.heightInMeters);
printf(“mikey weighs %d kilograms\n”, mikey.weightInKilos);
printf(“aaron is %.2f meters tall\n”, aaron.heightInMeters);
printf(“aaron weighs %d kilograms\n”, aaron.weightInKilos);
return 0;
} strut

The Heap

#include <stdio.h>
#include <stdlib.h> // malloc() and free() are in stdlib.h
int main(int argc, const char * argv[])
{
// Declare a pointer
float *startOfBuffer;
// Ask to use some bytes from the heap
startOfBuffer = malloc(1000 * sizeof(float));
// …use the buffer here…
// Relinquish your claim on the memory so it can be reused
free(startOfBuffer);
// Forget where that memory is
startOfBuffer = NULL;
return 0;
} stackheap  
 
 
 
 
 

发表评论

 

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据