Day 1 - Workshop - C Programming
Day 1 - Workshop - C Programming
Day - 01
Day : 01
Index : Duration : 8 Hours
● Introduction :
○ C Getting Started
○ C Syntaxes
○ C Output
○ C Comments
○ C Variables
○ C Data Types
○ C Constants
○ C Operators
○ C Keywords
1. Introduction :
What is C?
● C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.
● It is a very popular language, despite being old.
● C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Why Learn C?
● It is one of the most popular programming language in the world
● If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C#, etc, as
the syntax is similar
● C is very fast, compared to other programming languages, like Java and Python
● C is very versatile; it can be used in both applications and technologies.
C Install IDE :
● An IDE (Integrated Development Environment) is used to edit AND compile the code.
● Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug
C code.
We will use “Code::Blocks” in our tutorial, which we believe is a good place to start.
You can find the latest version of Codeblocks at http://www.codeblocks.org/. Download the mingw-setup.exe file, which will install the
text editor with a compiler.
C Quickstart :
Input code :
int main()
{
printf("Hello World!");
return 0;
}
3. C Syntax :
● You might have already seen the following code a couple of times.
● Let's break it down to understand it better:
Input code : ● Line 1: #include <stdio.h> is a header file library that lets us work with input
and output functions, such as printf() (used in line 4). Header files add
functionality to C programs.
int main()
● Line 2: A blank line. C ignores white space. But we use it to make the code
{
more readable.
printf("Hello World!");
● Line 3: Another thing that always appear in a C program, is main(). This is
return 0;
called a function. Any code inside its curly brackets {} will be executed.
}
● Line 4: printf() is a function used to output/print text to the screen. In our
example it will output "Hello World".
● Line 5: return 0 ends the main() function.
● Line 6: Do not forget to add the closing curly bracket } to actually end the main
function.
3. C Syntax :
● You might have already seen the following code a couple of times.
● Let's break it down to understand it better:
Input code :
Note :
● Don't worry if you don't understand how #include <stdio.h> works. Just
int main()
{ think of it as something that (almost) always appears in your program.
printf("Hello World!");
● Every C statement ends with a semicolon ;
return 0;
} ● The body of int main() could also been written as:
Input code
#include :
<stdio.h>
Output :
int main() {
printf("Hello World!");
return 0;
}
You can add as many printf() functions as you want. However, note that it does not insert a new line at the end of the output:
Input code :
Output :
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
}
4. C Output :
The printf() function is used to output values/print text:
Input code
#include :
<stdio.h>
Output :
int main() {
printf("Hello World!");
Hello World!
return 0;
}
You can add as many printf() functions as you want. However, note that it does not insert a new line at the end of the output:
Input code :
Output :
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
}
4. C Output :
The printf() function is used to output values/print text:
Input code
#include :
<stdio.h>
Output :
int main() {
printf("Hello World!");
Hello World!
return 0;
}
You can add as many printf() functions as you want. However, note that it does not insert a new line at the end of the output:
Input code :
Output :
#include <stdio.h>
int main() { Hello World!I am learning C.
printf("Hello World!");
printf("I am learning C.");
return 0;
}
C New Lines :
● To insert a new line, you can use the “\n” character:
Input code :
Output :
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
You can also output multiple lines with a single printf() function. However, be aware that this will make the code harder to read:
Tip: Two “\n\n” characters after each other will create a blank line.
C New Lines :
● To insert a new line, you can use the “\n” character:
Input code :
Output :
#include <stdio.h>
int main() {
printf("Hello World!\n"); Hello World!
printf("I am learning C."); I am learning C.
return 0;
}
You can also output multiple lines with a single printf() function. However, be aware that this will make the code harder to read:
Tip: Two “\n\n” characters after each other will create a blank line.
5. C Output :
What is “\n” exactly?
● The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to the beginning of the
next line on the screen. This results in a new line.
● Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing
alternative code.
● Comments can be singled-lined or multi-lined.
Single-line Comments
// This is a comment
printf("Hello World!");
6. C Comments :
Comments in C
● Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing
alternative code.
● Comments can be singled-lined or multi-lined.
Single-line Comments
● Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing
alternative code.
● Comments can be singled-lined or multi-lined.
Single-line Comments
Input :
int main() {
printf("Hello World!");
}
6. C Comments :
C Multi-line Comments :
Input :
int main() {
Hello World!
printf("Hello World!");
}
7. C Variables :
Variables are containers for storing data values.
In C, there are different types of variables (defined with different keywords), for example:
● int - stores integers (whole numbers), without decimals, such as 123 or -123
● float - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
Syntax :
type variableName = value;
Where type is one of C types (such as int), and variableName is the name of the variable (such as x or
myName). The equal sign is used to assign a value to the variable.
7. C Variables :
Example : Create a variable called myNum of type int and assign the value 15 to it:
You can also declare a variable without assigning the value, and assign the value later:
Example :
int myNum;
myNum = 15;
Note: If you assign a new value to an existing variable, it will overwrite the previous value:
Example :
You learned from the output chapter that you can output values/print text with the printf() function:
Example :
printf("Hello World!");
In many other programming languages (like Python, Java, and C++), you would normally use a print function to display the value of a
variable. However, this is not possible in C:
Example :
To output variables in C, you must get familiar with something called "format specifiers".
8. C Variables : Format Specifiers ,
● Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is
basically a placeholder for the variable value.
● A format specifier starts with a percentage sign %, followed by a character.
● For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes,
inside the printf() function:
● To print other types, use %c for char and %f for float:
Input code :
Output :
#include <stdio.h>
int main() {
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
8. C Variables : Format Specifiers ,
● Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is
basically a placeholder for the variable value.
● A format specifier starts with a percentage sign %, followed by a character.
● For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes,
inside the printf() function:
● To print other types, use %c for char and %f for float:
Input code :
Output :
#include <stdio.h>
5
int main() {
// Create variables
5.990000
int myNum = 5; // Integer (whole number)
D
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
#include <stdio.h>
int main() {
// Create variables
int myNum = 5; // Integer (whole
number)
float myFloatNum = 5.99; // Floating point
number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
8. C Variables : Format Specifiers ,
● Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is
basically a placeholder for the variable value.
● A format specifier starts with a percentage sign %, followed by a character.
● For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes,
inside the printf() function:
● To print other types, use %c for char and %f for float:
Input code :
Output :
#include <stdio.h>
5
int main() {
// Create variables
5.990000
int myNum = 5; // Integer (whole number)
D
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
8. C Variables : Format Specifiers ,
Input code :
Output :
#include <stdio.h>
int main() {
int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);
return 0;
}
8. C Variables : Format Specifiers ,
Input code :
Output :
#include <stdio.h>
11
int main() {
int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);
return 0;
}
8. C Variables : Variable Identifiers ,
Example : Rules :
// Good
int minutesPerHour = 60;
Example : Rules :
● As explained in the Variables chapter, a variable in C must be a specified data type, and you must use a format specifier inside
the printf() function to display it:
#include <stdio.h>
int main() {
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
9. C Data Types :
● As explained in the Variables chapter, a variable in C must be a specified data type, and you must use a format specifier inside
the printf() function to display it:
#include <stdio.h>
5
int main() {
// Create variables
5.990000
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
D
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}
Basic Data Types :
● When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the
variable as "constant", which means unchangeable and read-only) :
#include <stdio.h>
int main() {
const int myNum = 15;
myNum = 10;
printf("%d", myNum);
return 0;
}
10. C Constants :
● When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the
variable as "constant", which means unchangeable and read-only) :
#include <stdio.h>
prog.c: In function ‘main’:
printf("%d", myNum); |
return 0;
}
11. C Operators :
● Operators are used to perform operations on variables and values.
● In the example below, we use the + operator to add together two values:
● Ex : int myNum = 100 + 50;
● Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a
variable and a value, or a variable and another variable:
#include <stdio.h>
int main() {
int sum1 = 100 + 50; // 150 (100 +
50)
int sum2 = sum1 + 250; // 400 (150 +
250)
int sum3 = sum2 + sum2; // 800 (400 +
400)
printf("%d\n", sum1);
printf("%d\n", sum2);
printf("%d\n", sum3);
11. C Operators :
● Operators are used to perform operations on variables and values.
● In the example below, we use the + operator to add together two values:
● Ex : int myNum = 100 + 50;
● Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a
variable and a value, or a variable and another variable:
#include <stdio.h>
150
int main() {
int sum1 = 100 + 50; // 150 (100 + 400
50)
800
int sum2 = sum1 + 250; // 400 (150 +
250)
int sum3 = sum2 + sum2; // 800 (400 +
400)
printf("%d\n", sum1);
printf("%d\n", sum2);
printf("%d\n", sum3);
C Operators :
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Bitwise operators
● Sizeof Operator
Arithmetic Operators :
Arithmetic operators are used to perform common mathematical operations.
Comparison Operators :
● Comparison operators are used to compare two values.
● Note: The return value of a comparison is either true (1) or false (0).
● In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:
Input code :
Output :
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
if( x > y )
{
printf("x is greater");
}
if ( y > x )
{
printf("y is greater");
}
}
Comparison Operators :
● Comparison operators are used to compare two values.
● Note: The return value of a comparison is either true (1) or false (0).
● In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:
Input code :
Output :
#include <stdio.h>
int main() {
x is greater
int x = 5;
int y = 3;
if( x > y )
{
printf("x is greater");
}
if ( y > x )
{
printf("y is greater");
}
}
Comparison Operators :
A list of all comparison operators:
Logical Operators :
Logical operators are used to determine the logic between variables or values:
Sizeof Operator :
The memory size (in bytes) of a data type or a variable can be found with the sizeof operator:
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
return 0;
}
Note that we use the %lu format specifer to print the result, instead of %d. It is because the compiler expects the sizeof operator to return a
long unsigned int (%lu), instead of int (%d). On some computers it might work with %d, but it is safer to use %lu.
Sizeof Operator :
The memory size (in bytes) of a data type or a variable can be found with the sizeof operator:
4
int main() {
int myInt; 4
float myFloat; 8
double myDouble;
1
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
return 0;
}
Note that we use the %lu format specifer to print the result, instead of %d. It is because the compiler expects the sizeof operator to return a
long unsigned int (%lu), instead of int (%d). On some computers it might work with %d, but it is safer to use %lu.
C Keywords :
● C keywords cannot be used as variable names, thus they must be included in your data. This tutorial lists the
● A keyword is a reserved term in the C library that is used to carry out an internal action; you cannot use it as
an identifier in C programmes. These keywords' functions and meanings are already known to the compiler.
C Keywords List :
● In the above program, float and return are keywords. The float keyword is used to declare variables, and the return is used to