0% found this document useful (0 votes)
3 views

Day 1 - Workshop - C Programming

This document provides an introduction to the C programming language, covering its history, importance, and basic concepts such as syntax, variables, and output functions. It outlines the necessary tools for getting started, including text editors and compilers, and explains how to write and execute simple C programs. Additionally, it discusses comments, format specifiers, and variable identifiers, essential for writing readable and functional C code.

Uploaded by

Alexa Martin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Day 1 - Workshop - C Programming

This document provides an introduction to the C programming language, covering its history, importance, and basic concepts such as syntax, variables, and output functions. It outlines the necessary tools for getting started, including text editors and compilers, and explains how to write and execute simple C programs. Additionally, it discusses comments, format specifiers, and variable identifiers, essential for writing readable and functional C code.

Uploaded by

Alexa Martin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

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.

Objective : This workshop will teach you the very basics of C.


2. C Getting Started :

● To start using C, you need two things:


○ A text editor, like Notepad, to write C code
○ A compiler, like GCC, to translate the C code into a language that the computer will understand
● There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).

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.

Note: Web-based IDE's can work as well, but functionality is limited.

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.

Or you can also use online compiler : https://www.w3schools.com/c/tryc.php?filename=demo_helloworld


2. C Getting Started :

C Quickstart :

● Let's create our first C file.


● Open Codeblocks and go to File > New > Empty File.
● Write the following C code and save the file as “myfirstprogram.c” (File > Save File as)

Fig : Example of C program named as “myfirstprogram.c”.


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 :

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:

● int main(){printf("Hello World!");return 0;}

● Remember: The compiler ignores white spaces. However, multiple lines

makes the code more readable.


4. C Output :
The printf() function is used to output values/print text:

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:

Example try to use and see the output :

printf("Hello World!\nI am learning C.\nAnd it is awesome!");

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:

Example try to use and see the output :

printf("Hello World!\nI am learning C.\nAnd it is awesome!");

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.

Examples of other valid escape sequences are :


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

● Single-line comments start with two forward slashes (//).


● Any text between // and the end of the line is ignored by the compiler (will not be executed).
● This example uses a single-line comment before a line of code:

Input code : Output :

// 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

● Single-line comments start with two forward slashes (//).


● Any text between // and the end of the line is ignored by the compiler (will not be executed).
● This example uses a single-line comment before a line of code:

Input code : Output :

// This is a comment Hello World!


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

● Single-line comments start with two forward slashes (//).


● Any text between // and the end of the line is ignored by the compiler (will not be executed).
● This example uses a single-line comment before a line of code:

Input code : Output :

printf("Hello World!"); // This is a comment Hello World!


6. C Comments :
C Multi-line Comments :

● Multi-line comments start with /* and ends with */.


● Any text between /* and */ will be ignored by the compiler:

Input :

#include <stdio.h> Output :

int main() {

/* The code below will print the words


Hello World! to the screen, and it is amazing
*/

printf("Hello World!");
}
6. C Comments :
C Multi-line Comments :

● Multi-line comments start with /* and ends with */.


● Any text between /* and */ will be ignored by the compiler:

Input :

#include <stdio.h> Output :

int main() {
Hello World!

/* The code below will print the words


Hello World! to the screen, and it is amazing
*/

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:

int myNum = 15;

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 :

int myNum = 15; // myNum is 15


myNum = 10; // Now myNum is 10
Now lets see how to use these variables in output :

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 :

int myNum = 15;

printf(myNum); // Nothing happens

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 ,

Add Variables Together :


● To add a variable to another variable, you can use the + operator:

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 ,

Add Variables Together :


● To add a variable to another variable, you can use the + operator:

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 ,

● All C variables must be identified with unique names.


● These unique names are called identifiers.
● Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
● Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

Example : Rules :

// Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is


int m = 60;
8. C Variables : Variable Identifiers ,

● All C variables must be identified with unique names.


● These unique names are called identifiers.
● Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
● Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

Example : Rules :

The general rules for naming variables are:


// Good
int minutesPerHour = 60; ● Names can contain letters, digits and underscores
● Names must begin with a letter or an underscore (_)
// OK, but not so easy to understand what m actually is ● Names are case sensitive (myVar and myvar are different
int m = 60; variables)
● Names cannot contain whitespaces or special characters like
!, #, %, etc.
● Reserved words (such as int) cannot be used as names
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:

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;
}
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:

Input code : Output :

#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 :

Fig : Basic C data types


Basic Format Specifiers :

Fig : Basic format specifiers


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) :

Input code : Output :

#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) :

Input code : Output :

#include <stdio.h>
prog.c: In function ‘main’:

int main() { prog.c:5:9: error: assignment of read-only variable


const int myNum = 15; ‘myNum’
myNum = 10;
5 | myNum = 10;

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:

Input code : Output :

#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:

Input code : Output :

#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 :

C divides the operators into the following groups:

● 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:

Input code : Output :


#include <stdio.h>

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:

Input code : Output :


#include <stdio.h>

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

many C keywords that are accessible.

● 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 :

● A list of 32 reserved keywords in the C language is given below:


Example :

● In the above program, float and return are keywords. The float keyword is used to declare variables, and the return is used to

return an integer type value in this program.


Thank You!!

You might also like