C Lang Intro
Flow Control
Loops
Arrays
Example: Bubble sort
Example: Sieve of Eratosthenes
Compiler converts source code into machine lang
Detects and reports errors
Compiler outputs object code to object files (.obj, .o)
Compilation phases
Preprocessing phase: source code may be modified or added
// main.c // This is a comment
#include <stdio.h> // This is a preprocessor directive
int main() // Define function main
{ // Mark beginning of main
printf("Hi, there!\n"); // Print string to console
return 0; // Return control to operating system
} // Mark end of main
// Comment 1: Compiler ignores everything with two slashes on a line
/* Comment 2 */
/*
* Comment 3:
* Compiler ignores everything between /* ... */
*/
#include <stdio.h> // # marks a preprocessing directive
#define PI 3.1415 // define a constant
int main() // Function header
{ // Opening brace
// ... // Function body
} // Closing brace
#include <stdio.h>
int main(void)
{
int salary; // Declare a variable called salary
salary = 10000; // Store 10000 in salary
printf("My salary is %d.\n", salary);
return 0;
}
if(a > b)
printf('a is greater b');
if(a < b)
printf('b is greater a');
else
printf('a is greater b');
if(a < b)
{
if(a < z)
{
// ...
}
else
{
// ...
}
}
else if(z < b)
{
// ...
}
else
{
// ...
}
switch(customerId)
{
case 35:
printf('Customer id is 35');
break;
case 36:
// ...
break;
default:
// ...
break;
}
for(int i = 0; i < 10; i++)
{
// ...
}
int count = 0;
for( ; count < 10; ++count)
{
// ...
}
for( ;; )
{
// ...
if(quitPrg)
break;
}
int i = 0;
int count = 10;
while(i < count)
{
//...
i++;
}
for( ;; )
{
// ...
while(true)
{
// ...
}
// ...
}
#include <stdio.h> // # marks a preprocessing directive
Beginning C (5th Edition)
Ivor Horton
Thank you for your attention!