Programming in C

John Samuel

CPE Lyon

Year: 2017-2018
Contact: john(dot)samuel(at)cpe(dot)fr

Creative Commons License

Programming in C

Goals

Programming in C

Course Structure:

Programming Environment

Course:

Class:

Practical Session:

Programming in C

Class Dates
Class 1 12th September
Class 2 13th September
Class 3 19th September
Class 4 20th September
Class 5 26th September
Class 6 3rd October

Programming in C

Practical Sessions

Practical Session

Questions

Give some examples of softwares written in C

C Programming Language

C is a

programming language.

Note: No classes (Not an object-oriented programming language!!!)

C Programming Language

C

Hello World!!

/* File: hello1.c
* prints message on the output screen.
* author: John Samuel
* This is a multiline comment
*/

#include <stdio.h> // headers

// This is a single-line comment
int main() {
  printf("Hello World!!!");
  return 0;
}

Hello World!!

/* File: hello2.c
* prints message on the output screen using a variable.
* author: John Samuel
* This is a multiline comment
*/

#include <stdio.h> // headers

int main() {
  int year = 2017; //variable declaration
  printf("Hello World!!! This is year %d", year);
  return 0;
}

Compilation

$ gcc hello1.c

Execution

$./a.out
Hello World!!!

Compilation

$ gcc -o hello hello2.c

Execution

$./hello
Hello World!!! This is year 2017

Comments

Single-line and multi-line comments

// This is a single line comment

/* This is a multi-line
 * comment
 */

Basic data types

Data Type C keyword Examples
characters char 'h', 'a', ...
integers short, int, long, long long ...,-1,0,1,...
floating point numerals float, double, long double 3.14, 3.14e23
enumeration enum STUDENT, INTERN

Signed and unsigned data types

C keyword Range
characters signed char, unsigned char
integers signed short, signed int, signed long, signed long long, unsigned short, unsigned int, unsigned long, unsigned long long

Size limits of basic data types are machine-dependent!

Variable declaration

char my_char_var = 'a';

Note: See the use of underscores in naming variables

Variable declaration

char my_char_var = 'a';
unsigned char my_uchar_var = 234;
short my_short_var = -12;
unsigned short my_ushort_var = 65535;
int my_int_var = 12;
unsigned int my_uint_var = 3456;
long my_long_var = -1234553;
unsigned long my_ulong_var = 234556;
long long my_llong_var = 1123345;
unsigned long long my_ullong_var = 1234567;
float my_int_var = 3.14;
double my_uint_var = 3.14E-12;
long double my_long_var = 3.14E-22;

Variable declaration

enumerated data types

enum status {STUDENT, INTERN};
enum status s = STUDENT;
enum status {STUDENT=1, INTERN};
enum boolean {FALSE=0, TRUE};

Note: enum: unsigned int

Finding range of data types

Finding range of data types using limits.h

C keyword Range
signed char [SCHAR_MIN, SCHAR_MAX]
unsigned char [UCHAR_MIN, UCHAR_MAX]

Finding range of data types

C keyword Range
(signed) short int [SHRT_MIN, SHRT_MAX]
unsigned short int [0, USHRT_MAX]
(signed) int [INT_MIN, INT_MAX]
unsigned int [0, UINT_MAX]
(signed) long [LONG_MIN, LONG_MAX]
unsigned long [0, ULONG_MAX]
(signed) long long [LLONG_MIN, LLONG_MAX]
unsigned long long [0, ULLONG_MAX]

Finding range of data types

Finding range of data types using float.h

C keyword Range
float [FLT_MIN, FLT_MAX]
double [DBL_MIN, DBL_MAX]
long double [LDBL_MIN, LDBL_MAX]

size of (in bytes)

sizeof

sizeof (char) //data type
sizeof (my_uchar_var) //variable

Programming in C

Handling Output

printf("%d", my_int_var);
printf("%f", my_float_var);

printf: format string

C keyword Format string
char c
unsigned char hhu
short hd
unsigned short hu
int d, i
unsigned int u
long int ld
unsigned long int lu

printf: format string

C keyword Format string
long long int lld
unsigned long long int llu
float f, F
double g, G
long double Lf
string of characters s

printf: format string

Character Format string
Newline \n
Tab \t

Data Representation

Binary notation

  int value = 0b10100100;

Data Representation

Octal notation

  int value = 0b10100100;
  printf("octal value: %o\n", value);

Data Representation

Hexadecimal notation

  int value = 0b10100100;
  printf("hexadecimal value: %x\n", value);

Arithmetic operators

Operator Purpose
+ addition
- subtraction
* multiplication
/ division
% modulus operation

Arithmetic operators

int a = 20, b = 10;
Operator Example Result
+ a + b 30
- a - b 10
* a * b 200
/ a / b 2
% a % b 0

Relational operators

Operator Purpose
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to

Relational operators

int a = 20, b = 10;
Operator Example Result
< a < b 0
<= a <= b 0
> a > b 1
>= a >= b 1
== a == b 0
!= a != b 1

Logical operators

int a = 20, b = 0;
Operator Purpose Example Result
! Not !a 0
&& And a && b 0
|| Or a || b 1

Increment/Decrement operators

int a = 20, b = 0;
Operator Example Result
a++ b = a++ a = 21, b = 20
++a b = ++a a = 21, b = 21
a-- b = a-- a = 19, b = 20
--a b = --a a = 19, b = 19

Bitwise operators

int a = 0x01000100;
Operator Purpose Example Result
~ NOT ~a 0xffffffbb
& AND a & 0x4 0x4
| OR a | 0x2 0x46
^ XOR a ^ 0x4 0x40
<< left shift a << 1 0x88
>> right shift a >> 1 0x22

Assignment operators

int a = 20, b = 0;
Operator Purpose Example
= equal a = b
+= addition assignment a += b
-= substraction assignment a -= b
*= multiplication assignment a *= b
/= division assignment a /= b
%= modulo assignment a %= b

Note: a op = b ::- a = a op b

Assignment operators

Operator Purpose Example
&= bitwise AND assignment a &= b
|= bitwise OR assignment a |= b
^= bitwise XOR assignment a ^= b
<<= bitwise left shift assignment a <<= b
>>= bitwise right shift assignment a >>= b

Note: a op = b ::- a = a op b

Control flow statements

if

if (condition) {
 ...
}

Control flow statements

if

int a = 20, b = 0;
if (a > b) {
 printf("a is greater than b");
}

Conditional statements

if

if (condition1) {
 ...
} else if (condition2) {
 ...
} else {
 ...
}

Note: Non-zero values are considered true-value statements
Note: else is optional

Conditional statements

if

int a = 20, b = 0;
if (a > b) {
 printf("a is greater than b");
} else if (a < b) {
 printf("a is less than b");
} else {
 printf("a is equal to b");
}

Note: Non-zero values are considered true-value statements

Conditional statements

switch

switch (expression) {
 case value1 : statements1
 case value2 : statements2
 ...
 default : statementsn
}

Note: expression must be one of char, short, int or long

Conditional statements

switch

int a = 20;
switch (a) {
 case 10 : statement1
  break;
 case 20 : statement2
 case 30 : statement3
  break;
 ...
 default : statementn
}

Note: Both statement2 and statement3 will be executed.

Questions

What is the output of the following?

if (1) {
  printf("Hi");
} else {
  printf("Hello");
}

Looping statements

for

for(initialization;condition;updation){
 ...
}

Looping statements

for

int a = 0;
for( a = 0; a > 10; a++){
 ...
}

Looping statements

for

int a = 0;
for(; a > 10; ){
 ...
}

Note: Any or all of initialization, condition or updation statements can be missing.

Looping statements

for

int a = 0;
for( a = 0; a > 10; a++){
 ...
  a += 2 ;
 ...
}

Looping statements

while

while(condition){
 ...
}

Looping statements

while

int a = 20;
while(a > 0){
 ...
 a--;
 ...
}

Looping statements

while

int a = 0;
while(a < 20){
 ...
 a++;
 ...
}

Looping statements

do..while

do{
 ...
} while(condition);

Looping statements

do..while

int a = 20;
do{
 ...
  a --;
 ...
} while(a > 0);

Looping statements

do..while

int a = 0;
do{
 ...
  a ++;
 ...
} while(a < 20);

Looping statements: Jump

break

do{
 ...
 if (condition1) {
  ...
  break;
 }
 ...
} while(condition);

Looping statements

continue

do{
 ...
 if (condition1) {
  ...
  continue;
 }
 ...
} while(condition);

Looping statements: Jump

break

while(condition){
 ...
 if (condition1) {
  ...
  break;
 }
 ...
};

Looping statements

continue

while(condition){
 ...
 if (condition1) {
  ...
  continue;
 }
 ...
};

Looping statements: Jump

break

for(initialization;condition;updation){
 ...
 if (condition1) {
  ...
  break;
 }
 ...
};

Looping statements

continue

for(initialization;condition;updation){
 ...
 if (condition1) {
  ...
  continue;
 }
 ...
};

Questions

Write a program that prints 0..20 in ascending and descending order using for, while and do..while.

Questions

Write an infinite loop using for, while, do..while.

Arrays and Matrices

Arrays

are a collection of homogeneous elements

Matrices

Matrices

are rectangular arrays

Multi-dimensional arrays

Multi-dimensional arrays

are rectangular arrays

String of characters

Declaration

char name[20];

Note: C doesn't have a special data type called 'string'.

Array of numbers

Declaration

int iarray[20];
float farray[20];
double darray[20];

Arrays and Matrices

Initialization

int i;
int array[20];
for ( i = 0; i < 20; i++) {
 array[i] = i;
}

Arrays and Matrices

Initialization

int prices[5] = { 11, 12, 13, 14, 15 };
int rooms[] = { 301, 302, 303 };
char message[] = "Hello World!!";

Note: We didn't specify size of rooms and message.

Arrays and Matrices

Initialization

int prices[2][2] = {
  {11, 12},
  {13, 14}
};
int rooms[][] = {
  {201, 202},
  {301, 302}
};
char message[2][8] = {"Hello", "World!!"};

Programming in C

References

Image Credits