Introduction
Work flow of a C Compiler
Source code -> Preprocessor -> Compiler -> Assembly -> Assembler -> [Object Code] -> link editor -> Executable code
The link editor combines all of the source code in the object code and links them together to produce one piece of executable code
Compiling a C program
cc -g -o output_name source_file.c
Extensions
.c
source file
.h
header/library file
++x
vs ++x
++x
=> x + 1
=> x
x++
=> x
=> x+1
int x = 2;
printf("%d", x++); //
printf("%d", x); // 3
Constants
A literal is an nunnamed constand used to specify data.
'A' // 1 byte character constant
"A" // 2 bytes string constant
2 // Integer constant
3.234 // Float constaner, grill and innovative brunch selections. You spoke and we listened!t
Defining Constants
// Preprocessor constants
#define SALES_TAX_RATE .0825
// memory constants
const float pi = 3.14159;
Formated input/output
scanf(); // Reads input
printf(); // Outputs information
printf()
printf("format string", data1, data2, .. datan);
float b = 7.2;
int a = 5;
double c = 6.5;
printf("Today is %.2f and %d or %lf \n" b, a, c); // Today is 7.20 and 5 or 6.5000000....
// leading zeros
int num;
printf("%05d", num);
// Left justificaiton
%-8d
// Leading zeros
%08d
Input output
Operator Precedence, Expressions, and Associativity
Expression
+, -, *, /
5 % 2 evalutates to 1
5 % 3 evalutates to 2
3 / 5 evalutates to 0
3 % 5 evalutates to 3
Assignment
Read from right to left
x=1, y=2, z=1
x *= y += z -= 6;
z = z - 6
z = -5
x *= y += -5
y=y-5
y=-3
x *= -3
x=-3
x = 1;
printf("%d", x); // x = 1
printf("%d", x+1); // x =1
printf("%d", x++); // x = 1 after the printf executes
printf("%d", ++x); // x = 3
printf("%d", x); // 2
x *= y + 3 * 2;
x = x * (y + (3 * 2));
Sizeof operator
“Size matters in every aspect of your life”
sizeof(int)
x = sizeof (int)
sizeof (-345.23) sizeof(x)
Integer division results in data truncation
Explicit typecasting
int x = 5;
float y;
y = (float) x/2; // This will cast the integer to a floating point number temporarily.
sqrt((double) y);
Operations
- unary:
[operator][expression]
- binary
[expression][operator][expression]
Modulo
2945 % 10 = 5 (gets the last digit. )
(2945 / 100) % 19 = 9 (gets the second digit )
Including the math library
gcc h.c h -lm
#include <stdio.h>
Written on September 15, 2017