C Basics

Ryan Robucci

Introduction to the C Programming Language

Table of Contents

Review

C History

The C Standard

What is C?

C Resources

C vs. Java

Hardware to Application Onion Model

Hardware---Registers programming by rewiringMachine Language e.g. ox001F ox0002 ox0006Add RI,R2 2nd GLAssembler1st GLUnique to the MachineHigh-Level Languagea+=b;C, Pascal3rd GLHigher-Level Language Advanced programming 3rd GLC++, Javaconcepts, e.g OOP4th GL e.g. CASE(ComputerAided Sofware Engineering)Application ProgramsFigure 6.0 FromHardware to Applications*Figure 6.0 From Textbook

Libraries

Hello World

/*
file header block comment
*/
#include <stdio.h>
int main( )
{
// print the greeting ( // allowed with C99 )
printf( “Hello World\n”);
return 0;
}

Compiling on Unix

Compiler Options

Compilation Flow

Program is executed by calling name of executable at Unix prompt:
E.g.

unix>./hello

Compiler Vocabulary

Identifiers

Choosing Identifiers example

Declaring, Defining, Initialization

Assignments

Initialization

Types

Integral Data Types

Integral Type Sizes

Integral Specifiers

Fixed-Width Integer Types

Floating Point Types

Character Data Types

Const qualifier

sizeof()

Variable Declaration

Arithmetic Operators

Boolean Data Type

#include <stdbool.h>
bool isRaining = false;
if(isRaining)
 printf(“Bring your umbrella\n”);

Logical Operators and Comparisons

Control Structures

Curly Braces

int main(){
 int i=7;
 int t;
 if(i==7) {
   i=i+j;
   int k; //forbidden by c89 standard (c99 okay)
   k=i*i;
   t=k*2;
 }
}

If - Else block

if (expression) (statement)

e.g.

if(x>3) x+=1; //simple form
if(expression) { //simple form with {} to group
  statement;
  statement;
}
if(expression){ //full if/else form
 statement;
} else {
 statement;
}

If - Else If – Else block

if(expression1) {
 statement 1;
} else if (expression2) {
 statement2;
} else {
 statement3;
}

Spacing Variation (Be Consistent)

K&R style

if(expression) {
 statement;
}else {
 statement;
}

Stroustrup

if (expression)
{
 statement;
}
else {
 statement;
}

Allman

if (expression)
{
 statement;
}
else
{
 statement;
}

There are many spacing styles for logic blocks. Pick one and be consistent.

https://en.wikipedia.org/wiki/Indentation_style

Liunx indent

indent (sudo apt install indent)- changes the appearance of a C program by inserting or deleting whitespace.
Can manipulate many aspects of indentation, braces, line spacing, etc...
Example KR Style:

indent -kr orig.c -o indented.c

Example Allman/BSD style (from https://en.wikipedia.org/wiki/Indent_(Unix)):

indent -st -bap -bli0 -i4 -l79 -ncs -npcs -npsl -fca -lc79 -fc1 -ts4 orig.c -o indented.c

Switch

switch (expression) {
 case const-expression-1:
   statement;
   break;
 case const-expression-2:
   statement;
   break;
 case <const-expression-3>: //combined case 3 and 4
 case <const-expression-4>:
   statement;
   break;
 case <const-expression-5>: //no break mistake? maybe
   statement;
 case <const-expression-6>:
   statement;
   break;
 default: // optional
 statement;
}

Omitting the break statements is a common error

compiles, but leads to inadvertent fall-through behavior. This behavior is just like the assembly jump tables it implements.

While – Do While

while(expression){ //executes 0 or more times
 statement;
}
do{ //executes 1 or more times
 statement;
} while(expression)

For loops

for(initialization; continuation; action){
 statement;
}
for(; continuation; action){
 statement;
}
int i = 99;
for(; i!=0;){
 statement;
 i-=1;
}
for (int i = 99; i!=0; i=i-1){
 statement;
}

These are equivalent.
The second one is much more readable.
The second one also uses the C99 variable declaration inside the for loop.

Break and Continue

while(expression){
 statement;
 statement;
 if(condition)
   break;
 statement;
 statement;
}
//control jumps here on break.

Continue

while(expression){
 statement;
 if(condition)
   continue;
 statement;
 statement;

 //control jumps here on continue
}
goto label1; //unconditional jump
int n=99; //declares variable, and represents and initialization
label1:
//here n is not initialized to 99

Conditional Expression

Other Operators

Arrays

int grades[30];
int areas[10] = {1,2,3};
long widths[12] = {0};
int IQs[] = {120, 121, 99, 154};

Arrays defined using a Variable for Size

Multi-Dimensional Arrays

int board[4][5]; // 4 rows, 5 columns
int x = board[0][0]; //1st row, 1st column
int y = board[3][4]; //4th (last) row, 5th (last) column

#define

#define PI 3.14159double area = PI * radius * radius;

Use of #define vs. const

Good practice involves a generous use of parenthesis with #define

#define NUMBER (2)

Examples:

Options to view preprossor Output

Enumeration Constants

C Functions

C Functions

Simple C Program

#include <stdio.h>
typedef double Radius;
#define PI (3.1415)

/* given the radius, calculates the area of a circle */
double CircleArea( Radius radius ){
 return ( PI * radius * radius );
}

// given the radius, calculates the circumference of a circle
double Circumference( Radius radius ){
 return (2 * PI * radius );
}

int main( ){
 Radius radius = 4.5;
 double area = circleArea( radius );
 double circumference = Circumference( radius ); // print the results
 return 0;
}

Simple C Program with prototypes

#include <stdio.h>

typedef double Radius;

#define PI (3.1415)

/* function prototypes */
double CircleArea( Radius radius );
double Circumference( Radius radius );

int main( ){
 Radius radius = 4.5;
 double area = circleArea( radius );
 double circumference = Circumference( radius );
 //print the results here
 return 0;
}

/* given the radius, calculates the area of a circle */
double CircleArea( Radius radius ){
 return ( PI * radius * radius );
}

// given radius, calcs circumference of circle
double Circumference( Radius radius ){
 return (2 * PI * radius );
}
• includes

• typedefs

• defines

• function 
   prototypes


• main







• function 
   definitions