int a = 25, b = 5, c = 10, d = 7;
NSLog(@"a %% b = %i", a % b);
NSLog(@"a %% c = %i", a % c);
NSLog(@"a %% d = %i", a % d);
NSLog(@"a / d * d + a %% d = %i", a / d * d + a % d);
}
@autoreleasepool {
float f1 = 123.125, f2;
int i1, i2 = -150;
i1 = f1;
NSLog(@"%f assigned to an int produces %i", f1, i1);
f1 = i2; //integer to floating conversion
NSLog(@"%i assigned to a float produces %f", i2, f1);
f1 = i2 / 100; //integer divided by integer
NSLog(@"%i divided by 100 produces %f", i2, f1);
f2 = i2 / 100.0; //integer divided by a float
NSLog(@"%i divided by 100.0 produces %f", i2, f2);
f2 = (float) i2 / 100; //type cast operator
NSLog (@"(float) %i divided by 100 produces %f", i2, f2);
}
#include
void congratulateStudent(char *student, char *course, int numDays)
{
printf("%s has done as much %s programming as I could fit into %d days.\n", student, course, numDays);
}
int main (int argc, const char * argv[])
{
congratulateStudent("Mark", "Cocoa", 5);
congratulateStudent("Bo", "Objective-C", 2);
congratulateStudent("Mike", "Python", 5);
congratulateStudent("Ted", "iOS", 5);
}
#include
void singTheSong(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n");
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer); //this function class itself
}
}
int main (int argc, const char * argv[])
{
singTheSong(99);
#include
float fahrenheitFromCelsius(float 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);
return 0;
}
#include
float remainingAngle(float angleA, float angleB)
{
return 180 - (angleA + angleB);
}
int main (int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf("The third angle is %.2f\n", angleC);
return 0;
}
#include
int main (int argc, const char * argv[])
{
int x = 255;
printf("x is %d.\n", x);
printf("In octal, x is %o.\n", x);
printf("In hexadecimal, x is %x.\n", x);
long y = 2873;
printf("y is %ld.\n", y);
printf("In octal, y is %lo.\n", y);
printf("In hexadecimal, y is %lx.\n", y);
printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);
printf("11 / 3 = %d remainder of %d \n", 11 / 3, 11 % 3);
//use the cast operator
printf("11 / 3.0 = %f\n", 11 / (float)3);
printf("The absolute value of -5 is %d\n", abs(-5));
return 0;
}
#include
int main (int argc, const char * argv[])
{
int i = 0;
while (i < 12) {
printf("%d. Aaron is Cool.\n", i);
i++;
}
for (i = 9; i < 12; i++){
printf("Checking i = %d\n", i);
if (i + 90 == i * i){
break;
}
}
printf("The answer is %d.\n", i);
i = 99;
while (i >= 0){
printf("%d\n", i);
if(i % 5 == 0){
printf("Found one!\n");
}
i -= 3;
}
return 0;
}
int main (int argc, const char * argv[])
{
int i = 17;
float *ptr;
int *addressOfI = &i;
printf("i stores its value at %p\n", addressOfI);
printf("i stores its value at %p\n", &i);
printf("the int stored at addressOfI is %d\n", *addressOfI);
*addressOfI = 87;
printf("Now i is %d\n", i);
printf("this function starts at %p\n", main);
int j = 23;
int *addressOfj = &j;
printf("j stores its value at %p\n", addressOfj);
*addressOfj = 100;
printf("Now j is %d\n", j);
printf("An int is %zu bytes\n", sizeof(int));
printf("A pointer is %zu bytes\n", sizeof(int *));
printf("A float is %zu bytes.\n", sizeof(float));
return 0;
}
#include
#include
typedef struct {
float heightInMeters;
int weightInKilos;
} Person;
int main (int argc, const char * argv[])
{
Person person;
person.weightInKilos = 96;
person.heightInMeters = 1.8;
printf("person weighs %i kilograms\n", person.weightInKilos);
printf("person is %.2f meters tall\n", person.heightInMeters);
long secondsSince1970 = time(NULL);
printf("it has been %ld seconds since 1970\n", secondsSince1970);
struct tm now;
printf("The time is %d:%d:%d\n", now.tm_hour, now.tm_min, now.tm_sec);
return 0;
}