Introduction

Why C?

Intro for C++ Programmers (C++ v.s. C):

“You can do OOP in C, but not cleanly, and why would you want to?”

C++ -> C:

Missing in C:

History

In 1978, the first book of C programming, The C Programming Language, was published.

ANSI C:

C18/C17:

C Communities

Overview

Keywords

Note: C is a case-sensitive language, so all keywords must be written in lowercase.

Operators

Getting Started

Here is an example program:

#include <stdio.h>
int main()
{
    printf("Hello, World\n");
    return 0;
}

Here’s another example program:

#include <stdio.h>
int main()
{
    puts("Hello, World");
    return 0;
}

Note on Comments: Some versions of C only support multi-line comments (/* */), though C99 added support for single-line comments (//)

Compiling and Running

Simplest:

$ gcc hello.c

Change default name:

$ gcc hello.c -o hello

Take advantage of “recent” improvements:

$ gcc –o hello –std=c99 hello.c

See more warnings:

$ gcc –o hello –std=c99 -Wall hello.c

Turn on light optimization (helps us see more warnings like uninitialized variables):

$ gcc –o hello –std=c99 -Wall -O1 hello.c

make

make: Goes through a descriptor file starting with the target it is going to create.

Example: Minimal makefile

# Makefile
all:    hello
# This line says that hello.c is a dependency of hello
hello:  hello.c
  # Make runs this only if ./hello has a timestamp older than hello.c
  gcc -o hello -std=c99 -Wall -O1 hello.c
clean:
  rm -f hello

How make Works:

Note: Commands must be indented with tabs rather than spaces.

Example: Another Makefile

FLAGS = -std=c99 -Wall -O1
all:    hello goodbye
hello:  hello.c goodbye.c
  gcc ${FLAGS} -o hello hello.c
  gcc ${FLAGS} -o goodbye goodbye.c
clean:
  rm -f hello
  rm -f goodbye

More on printf

printf() is a function provided by the standard I/O library (stdio.h).

Example: Using printf()

#include <stdio.h>
int main()
{
  char grade = 'A';
  # Print a char
  printf("Character grade is %c \n", grade);
  # Print a float
  float toll= 15.678;
  printf("Toll value is %f \n", toll);
  # Print an int
  int x = 125;
  printf("Integer x value is %d\n" , x);
  # Print a double
  double width= 23.123456;
  printf("Double value is %lf \n", width);
  # Print an octal
  printf("Octal value of x is %o \n", x);
  # Print a hexadecimal
  printf("Hexadecimal value of is %x \n", x);
  return 0;
}

Format Specifiers

Format SpecifierDescription
%dvalue of an integer variable
%cvalue of a character variable
%fvalue of a float variable
%lfvalue of a double variable
%svalue of a string variable
%ooctal value corresponding to integer variable
%xhexadecimal value corresponding to integer variable
%newline character

Escape Sequences

Escape SequenceDescription
Newline. Position cursor to the beginning of the next line.
Horizontal tab. Move cursor to the next tab stop
Carriage return. Position cursor to the beginning of the current line; do not advance to the next line
Alert. Sound the system bell
\Backslash. Used to print a backslash character
Single quote. Use to print a single quote character
Double quote. Used to print a double quote character

scanf()

scanf(): Function to read character, string, numeric data from keyboard.

Example: Reading a character from stdin

#include <stdio.h>
int main()
{
  char userChar;
  puts("Please enter a character: ");
  scanf("%c", &userChar);
  printf("Your character: %c\n", userChar);
  return 0;
}

Example: Doing math on user input.

#include <stdio.h>
int main()
{
  int x, y, z;
  printf("Input three different integers: \n");
  scanf("%d %d %d", &x, &y, &z);

  int SUM = x + y + z;
  float AVG = (SUM / 3);
  int PRODUCT = x * y * z;

  printf("Sum: %d \n", SUM);
  printf("Average: %f \n", AVG);
  printf("Product: %d \n", PRODUCT);
  return 0;
}

Example: Printing the squares and cubes of the first 10 positive integers.

#include <stdio.h>
int main()
{
  printf("Number\tSquare\tCube\n");
  for (int i = 1; i <= 10; i++)
  {
      int NUMBER = i;
      int SQUARE = i * i;
      int CUBE = i * i * i;
      printf("%d\t%d\t%d\n", NUMBER, SQUARE, CUBE);
  }
  return 0;
}

Math Library

How-To Use:

Common Functions:

FunctionDescription
acosArc cosine
acoshHyperbolic arc cosine
asinArc sine
asinhHyperbolic arc sine
atanArc tangent
atan2Arc tangent and determine quadrant using sign
atanhHyperbolic arc tangent
cbrtCube root
ceilNearest integer greater than the argument
cosCosine
coshHyperbolic cosine
expExponential
fabsAbsolute argument of floating point argument
floorNearest integer lower than the argument passed
hypotSquare root of sum of two arguments (hypotenuse)
logNatural logarithm
log10Logarithm of base 10
powNumber raised to given power
sinSine
sinhHyperbolic sine
sqrtSquare root
tanTangent
tanhHyperbolic tangent

Random Number Generation (srand and rand)

srand(): Seeds the pseudo random number generator used by the rand()

Example: Seeding the random number generator with the machine’s current time.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
  srand(time(0));
  int RANDOM = rand();
  printf("Random Number: %d", RANDOM);
  return 0;
}

Example: Generating a random number in a range.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
  srand(time(0));
  int RANDOM = 5+rand()%16;
  printf("Random Number between 5 and 20: %d", RANDOM);
  return 0;
}

More Functions from stdlib.h

Communication with the Environment:

Integer Arithmetic:

String Conversion:

Dynamically Allocated Arrays:

Deallocating Storage:

Searching and Sorting Functions:

More Functions from string.h

Comparison:

Search:

Copying:

Miscellaneous: