Introduction to C

C language features

  • Provides lowlevel access to memory

  • used in system programming

    • Operating systems: such as Linux
    • Microcontrollers: cars and planes
    • embedded systems: phones, portable electronics, ..
  • used in derivation of C++, Objective C, C#

  • C has enormous influence on other languages: Java, PHP, Python, . . .

  • High-level but close to the hardware

  • Fast: allows low-level programming

  • compiles to native code

  • C lacks: garbage collection, OOP, . . .

Installation

  • Linux: included with most linux distributions
    • you can check it by entering this into the command line: gcc -v
  • Mac OS: you need to install Xcode
  • Windows: download and install MinGW, ensure that bin subdirectory is in PATH

Hello World!

#include <stdio.h>
int main(void)
{
  #"\n" is an escape character means "newline"
	printf("Hello World!\n");
  #0 indicates the program ends normally
	return 0;
}
  • C standard library header files include function definitions, variable declarations.
#include <stdio.h>
  • Other header files are math.h, stdlib.h, string.h, time.h.
  • the main entry of the of C programs, returns integer (int) and has no parameters (void) followed by a curly bracket

Complie and run

  • save the code in hello.c (.c is the extension used for c language programs)
  • compile the program by entering to the command line

​ gcc hello.c

  • this create an executable file a.out on Linux and Mac OS, and a.exe on Windows

  • now you can run it by typing ./a.out (Linux MacOS) or a.exe (Windows)

  • you can change the name of the output file with:

    gcc hello.c -o hello

C keywords and Identifiers

C keywords

auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while

Integer Types

datatype size range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Datatypes

  • Integer Types
  • in unsigned the most significant bit (MSB) will not be used as sign (+ or -)
  • The header file limits.h has many useful constants to check the range of different datatypes: SCHAR_MIN, SCHAR_MAX, UCHAR_MAX, INT_MIN, INT_MAX, UINT_MAX, ...
  • Floating-Point Types
datatype size range precision
float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places
long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal places
  • The header file float.h provides constants to check the range of float datatypes : FLT_MIN, FLT_MAX, ...
  • the sizes and ranges may be different on you computer based on the platform you use (hardware and OS)