Programming
LESSON 1
What is C programming?
C programming is a general-purpose, procedural,
imperative computer programming language
developed in 1972 by Dennis M. Ritchie at the Bell
Telephone Laboratories to develop the UNIX
operating system. C is the most widely used
computer language. It keeps fluctuating at number
one scale of popularity along with Java programming
language, which is also equally popular and most
widely used among modern software programmers.
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
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Get StartedWith C
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
C Keywords and Identifiers
•Character set
-A character set is a set of alphabets, letters and
some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ...................................
X Y Z
Lowercase: a b c
...................................... x y z
•Digits
•Special Characters
0 1 2 3 4 5 6 7 8 9
C Keywords
•Keywords are predefined, reserved words used in
programming that have special meanings to the
compiler. Keywords are part of the syntax and they
cannot be used as an identifier. For example:
• Here, int is a keyword that indicates money is a variable of
type int (integer).
• As C is a case sensitive language, all keywords must be written in
lowercase. Here is a list of all keywords allowed in ANSI C.
int money;
C Keywords
C Identifiers
• Identifier refers to name given to entities
such as variables, functions, structures etc.
• Identifiers must be unique. They are
created to give a unique name to an entity
to identify it during the execution of the
program. For example:
C Identifiers
int money;
double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be
different from keywords. You cannot
use int as an identifier because int is a
keyword.
Rules for naming identifiers
1. A valid identifier can have letters (both uppercase and
lowercase letters), digits and underscores.
2. The first letter of an identifier should be either a letter or
an underscore.
3. You cannot use keywords like int, while etc. as
identifiers.
4. There is no rule on how long an identifier can be.
However, you may run into problems in some compilers
if the identifier is longer than 31 characters.
•You can choose any name as an identifier if you follow
the above rule, however, give meaningful names to
identifiers that make sense.
CVariables, Constants and Literals
Variables
•In programming, a variable is a container
(storage area) to hold data.
•To indicate the storage area, each variable
should be given a unique name (identifier).
Variable names are just the symbolic
representation of a memory location. For
example:
int playerScore = 95;
Here, playerScore is a variable of int type. Here, the
variable is assigned an integer value 95.
The value of a variable can be changed, hence the
name variable.
char ch = 'a';//
some codech = 'l';
Rules for naming a variable
•1. A variable name can only have letters
(both uppercase and lowercase letters),
digits and underscore.
•2. The first letter of a variable should be
either a letter or an underscore.
•3. There is no rule on how long a variable
name (identifier) can be. However, you
may run into problems in some compilers
if the variable name is longer than 31
characters.
Note: You should always try to give meaningful names to variables. For
example: firstName is a better variable name than fn.
C is a strongly typed language. This means that
the variable type cannot be changed once it is
declared. For example:
int number = 5; // integer variable
number = 5.5; // errordouble
number; // error
Here, the type of number variable is int. You cannot assign a floating-point
(decimal) value 5.5 to this variable. Also, you cannot redefine the data type of
the variable to double. By the way, to store the decimal values in C, you need
to declare its type to either double or float.
Literals
Literals are data used for representing fixed
values. They can be used directly in the
code. For example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals.
1. Integers
2. Float-point
3. Characters
4. Escape Sequences
5. String Literals
1. Integers
An integer is a numeric literal(associated with
numbers) without any fractional or exponential part.
There are three types of integer literals in C
programming:
 decimal (base 10)
 octal (base 8)
 hexadecimal (base 16)
For example:
Decimal: 0, -9, 22 etcOctal: 021, 077, 033 etc
Hexadecimal: 0x7f
2. Floating-point Literals
A floating-point literal is a numeric
literal that has either a fractional form or
an exponent form. For example:
2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
3. Characters
A character literal is created by enclosing a
single character inside single quotation
marks. For example: 'a', 'm', 'F', '2', '}' etc.
4. Escape Sequences
Sometimes, it is necessary to use
characters that cannot be typed or has
special meaning in C programming. For
example: newline(enter), tab, question
mark etc.
In order to use these characters, escape
sequences are used.
Escape Sequences
Escape Sequences Character
b Backspace
f Form feed
n Newline
r Return
t Horizontal tab
v Vertical tab
 Backslash
' Single quotation mark
" Double quotation mark
? Question mark
0 Null character
For example: n is used for a newline. The backslash  causes escape from the normal way the
characters are handled by the compiler.
5. String Literals
A string literal is a sequence of characters
enclosed in double-quote marks. For example:
"good" //string constant
"" //null string constant"
" //string constant of six white
space
"x" //string constant having a
single character.
"Earth is roundn" //prints string with a
newline
Constants
If you want to define a variable whose value cannot be
changed, you can use the const keyword. This will create a
constant. For example,
const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be
changed.
const double PI = 3.14;PI = 2.9; //Error
C Data Types
In C programming, data types are declarations for
variables. This determines the type and size of data
associated with variables. For example,
int myVar;
Here, myVar is a variable of int (integer) type.
The size of int is 4 bytes.
Basic types
Here's a table containing commonly used types in C programming for quick access.
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
1. int
Integers are whole numbers that can have both zero, positive
and negative values but no decimal values.
For example, 0, -5, 10
We can use int for declaring an integer variable.
int id;
Here, id is a variable of type integer.
You can declare multiple variables at once in C programming.
For example,
int id, age;
The size of int is usually 4 bytes (32 bits).
And, it can take 232 distinct states from -2147483648 to 2147483647.
2. float and double
float and double are used to hold real numbers.
float salary;
double price;
In C, floating-point numbers can also be represented in
exponential. For example,
float normalizationFactor = 22.442e2;
What's the difference between float and double?
The size of float (single precision float data type) is 4 bytes.
And the size of double (double precision float data type) is 8 bytes
3. char
Keyword char is used for declaring character type
variables. For example,
char test = 'h';
The size of the character variable is 1 byte.
Void
void is an incomplete type. It means "nothing" or
"no type". You can think of void as absent.
For example, if a function is not returning
anything, its return type should be void.
Note that, you cannot create variables of void type
short and long
If you need to use a large number, you can use a type
specifier long.
long a;
long long b;
long double c;
Here variables a and b can store integer values.
And, c can store a floating-point number.
If you are sure, only a small integer ([−32,767,
+32,767] range) will be used, you can use short.
short d;
You can always check the size of a variable
using the sizeof() operator.
#include <stdio.h>
int main()
{
short a;
long b;
long long c;
long double d;
printf("size of short = %d bytesn", sizeof(a));
printf("size of long = %d bytesn", sizeof(b));
printf("size of long long = %d bytesn", sizeof(c));
printf("size of long double= %d bytesn", sizeof(d));
return 0;
}
signed and unsigned
In C, signed and unsigned are type modifiers. You
can alter the data storage of a data type by using
them:
•signed - allows for storage of both positive and
negative numbers
•unsigned - allows for storage of only positive
numbers
For example:
// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
// invalid code: unsigned int cannot hold negative
integersunsigned int num = -35;
LO.2.
C Input / Output (I/O)
Example explained
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.
Line 2: A blank line. C ignores white space. But we use
it to make the code more readable.
Line 3: Another thing that always appear in a C
program, is main(). This is 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".
Note that: Every C statement ends with a semicolon ;
Note: 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.
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.
C Output (PrintText)
In C programming, printf() is one of the main output
function. The function sends formatted output to the screen.
Example 1:
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
Output
C Programming
Example 2: Integer Output
#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Output
Number = 5
NOTE: We use %d format specifier to print int types. Here, the %d inside
the quotations will be replaced by the value of testInteger.
Example 3: float and double Output
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;
printf("number1 = %fn", number1);
printf("number2 = %lf", number2);
return 0;
}
Output
number1 = 13.500000
number2 = 12.400000
NOTE: To print float, we use %f format specifier. Similarly,
we use %lf to print double values.
Example 4: Print Characters
#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}
Output
character = a
NOTE: To print char, we use %c format specifier.
To insert a new line, you can use the n
character: 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.
New Lines
C 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.
Types of Comments
There are two ways to add comments in C:
•// - Single Line Comment
/*...*/ - Multi-line Comment
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:
This example uses a single-line comment at the end of a line
of code:
// This is a comment
printf("Hello World!");
printf("Hello World!"); // This is a comment
C Multi-line Comments
Multi-line comments start with /* and ends with
*/.
Any text between /* and */ will be ignored by the
compiler:
/* The code below will print the words
Hello World! to the screen, and it is
amazing */
printf("Hello World!");
Use of Comments in C
1. Make Code Easier to Understand
If we write comments on our code, it will be easier
to understand the code in the future. Otherwise you
will end up spending a lot of time looking at our own
code and trying to understand it.
Comments are even more important if you are
working in a group. It makes it easier for other
developers to understand and use your code.
2. Using Comments for debugging
While debugging there might be situations where
we don't want some part of the code.
C Operators
- An operator is a symbol that operates on a value
or a variable.
C divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% modulus / remainder after division (modulo division)
++ increment (Increases the value of a variable by 1)
- - decrement (Increases the value of a variable by 1)
1. Arithmetic Operators
- An arithmetic operator performs mathematical
operations such as addition, subtraction, multiplication,
division etc on numerical values (constants and
variables).
#include <stdio.h>
int main()
{
int a,b,c;
printf("first number: n”);
scanf(“%d”, &a);
printf("second number: n”);
scanf(“%d”, &b);
c = a + b;
printf("answer:”, c);
return 0;
}
First number:
6
Second number:
10
answer: 16
Example: CODE OUTPUT
2. C Increment and Decrement
Operators
C programming has two operators
increment ++ and decrement -- to change the
value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1. These
two operators are unary operators, meaning
they only operate on a single operand.
// Working of increment and decrement
operators #include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
Example: CODE OUTPUT
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the
operators ++ and -- are
used as prefixes. These
two operators can also
be used as postfixes
like a++ and a--.
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
3. Assignment Operator
An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =.
#include <stdio.h>
int main()
{
int x = 10;
x += 5;
printf("%d", x);
return 0;
}
15
Example: CODE OUTPUT
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal
to
5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
4. C Relational / Comparison Operators
A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
Relational operators are used in decision-making and loops.
#include <stdio.h>
int main()
{
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1
(true) because 5 is greater than 3
return 0;
}
1
Example: CODE OUTPUT
Operator Meaning Example
&& Logical AND.True only if all
operands are true
If c = 5 and d = 2 then,
expression ((c==5) && (d>5))
equals to 0.
|| Logical OR.True only if either
one operand is true
If c = 5 and d = 2 then,
expression ((c==5) || (d>5))
equals to 1.
! Logical NOT.True only if the
operand is 0
If c = 5 then, expression
!(c==5) equals to 0.
5. C Logical Operators
An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C
programming.
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", result);
result = !(a != b);
printf("!(a != b) is %d n", result);
result = !(a == b);
printf("!(a == b) is %d n", result);
return 0;
}
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
OUTPUT:
Explanation of logical operator program
 (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
 (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
 (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
 (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
 !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
 !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
6. The sizeof operator
The sizeof is a unary operator that returns the size of data
(constants, variables, array, structure, etc).
include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytesn",sizeof(a));
printf("Size of float=%lu bytesn",sizeof(b));
printf("Size of double=%lu bytesn",sizeof(c));
printf("Size of char=%lu byten",sizeof(d));
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

More Related Content

DOCX
C programming tutorial
PDF
UNIT1 PPS of C language for first year first semester
PPTX
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
PPT
All C ppt.ppt
PPT
Unit 4 Foc
PPT
Introduction to C Programming
PPT
Session02 c intro
PPT
Structured Programming with C - Data Types.ppt
C programming tutorial
UNIT1 PPS of C language for first year first semester
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
All C ppt.ppt
Unit 4 Foc
Introduction to C Programming
Session02 c intro
Structured Programming with C - Data Types.ppt

Similar to LESSON1-C_programming (1).GRADE 8 LESSONpptx (20)

PDF
POLITEKNIK MALAYSIA
PPT
C presentation book
PPT
Introduction to C
PPT
C programming and problem solving for real time solution
PPT
Programming in C-To study basics of C programming Language.
PPT
Basics of C.ppt VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
PPT
Basics of C programming powerpoint p.ppt
PPT
Basics of C.pptArray.pptxArray.pptxArray.pptx
PPT
Basics of C programming concept and type
PPT
Basics of C.ppt
PPT
Basics of C.ppt
PPT
Basics of C
PPTX
Basic of Structered Programming in C psd
PPT
Basics of C.ppt
PPT
Basics of C.ppt
PPT
Basics of C.ppt
PPT
Basics of C.ppt
PPT
Funa-C.ppt
PPT
Basics of C.ppt
PPT
Basics of C (1).ppt
POLITEKNIK MALAYSIA
C presentation book
Introduction to C
C programming and problem solving for real time solution
Programming in C-To study basics of C programming Language.
Basics of C.ppt VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Basics of C programming powerpoint p.ppt
Basics of C.pptArray.pptxArray.pptxArray.pptx
Basics of C programming concept and type
Basics of C.ppt
Basics of C.ppt
Basics of C
Basic of Structered Programming in C psd
Basics of C.ppt
Basics of C.ppt
Basics of C.ppt
Basics of C.ppt
Funa-C.ppt
Basics of C.ppt
Basics of C (1).ppt
Ad

Recently uploaded (20)

PDF
Understand the Gitlab_presentation_task.pdf
PPTX
Cyber Hygine IN organizations in MSME or
PPTX
TITLE DEFENSE entitle the impact of social media on education
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PDF
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
PDF
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PPT
Ethics in Information System - Management Information System
PDF
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
PPTX
curriculumandpedagogyinearlychildhoodcurriculum-171021103104 - Copy.pptx
PPT
250152213-Excitation-SystemWERRT (1).ppt
PDF
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
PPTX
newyork.pptxirantrafgshenepalchinachinane
PPTX
Database Information System - Management Information System
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PDF
Uptota Investor Deck - Where Africa Meets Blockchain
PPTX
The-Importance-of-School-Sanitation.pptx
PDF
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
PPTX
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
Understand the Gitlab_presentation_task.pdf
Cyber Hygine IN organizations in MSME or
TITLE DEFENSE entitle the impact of social media on education
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
Alethe Consulting Corporate Profile and Solution Aproach
Ethics in Information System - Management Information System
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
curriculumandpedagogyinearlychildhoodcurriculum-171021103104 - Copy.pptx
250152213-Excitation-SystemWERRT (1).ppt
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
newyork.pptxirantrafgshenepalchinachinane
Database Information System - Management Information System
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
Uptota Investor Deck - Where Africa Meets Blockchain
The-Importance-of-School-Sanitation.pptx
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
Ad

LESSON1-C_programming (1).GRADE 8 LESSONpptx

  • 2. What is C programming? C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.
  • 3. 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 • Easy to learn • Structured language • It produces efficient programs • It can handle low-level activities • It can be compiled on a variety of computer platforms
  • 4. Get StartedWith C 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
  • 5. C Keywords and Identifiers •Character set -A character set is a set of alphabets, letters and some special characters that are valid in C language. Alphabets Uppercase: A B C ................................... X Y Z Lowercase: a b c ...................................... x y z
  • 7. C Keywords •Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example: • Here, int is a keyword that indicates money is a variable of type int (integer). • As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C. int money;
  • 9. C Identifiers • Identifier refers to name given to entities such as variables, functions, structures etc. • Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program. For example:
  • 10. C Identifiers int money; double accountBalance; Here, money and accountBalance are identifiers. Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.
  • 11. Rules for naming identifiers 1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. 2. The first letter of an identifier should be either a letter or an underscore. 3. You cannot use keywords like int, while etc. as identifiers. 4. There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the identifier is longer than 31 characters. •You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers that make sense.
  • 12. CVariables, Constants and Literals Variables •In programming, a variable is a container (storage area) to hold data. •To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:
  • 13. int playerScore = 95; Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95. The value of a variable can be changed, hence the name variable. char ch = 'a';// some codech = 'l';
  • 14. Rules for naming a variable •1. A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. •2. The first letter of a variable should be either a letter or an underscore. •3. There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.
  • 15. Note: You should always try to give meaningful names to variables. For example: firstName is a better variable name than fn. C is a strongly typed language. This means that the variable type cannot be changed once it is declared. For example: int number = 5; // integer variable number = 5.5; // errordouble number; // error Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need to declare its type to either double or float.
  • 16. Literals Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc. Here, 1, 2.5 and 'c' are literals. 1. Integers 2. Float-point 3. Characters 4. Escape Sequences 5. String Literals
  • 17. 1. Integers An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming:  decimal (base 10)  octal (base 8)  hexadecimal (base 16) For example: Decimal: 0, -9, 22 etcOctal: 021, 077, 033 etc Hexadecimal: 0x7f
  • 18. 2. Floating-point Literals A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example: 2.0 0.0000234 -0.22E-5 Note: E-5 = 10-5
  • 19. 3. Characters A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
  • 20. 4. Escape Sequences Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequences are used.
  • 21. Escape Sequences Escape Sequences Character b Backspace f Form feed n Newline r Return t Horizontal tab v Vertical tab Backslash ' Single quotation mark " Double quotation mark ? Question mark 0 Null character For example: n is used for a newline. The backslash causes escape from the normal way the characters are handled by the compiler.
  • 22. 5. String Literals A string literal is a sequence of characters enclosed in double-quote marks. For example: "good" //string constant "" //null string constant" " //string constant of six white space "x" //string constant having a single character. "Earth is roundn" //prints string with a newline
  • 23. Constants If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example, const double PI = 3.14; Notice, we have added keyword const. Here, PI is a symbolic constant; its value cannot be changed. const double PI = 3.14;PI = 2.9; //Error
  • 24. C Data Types In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example, int myVar; Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
  • 25. Basic types Here's a table containing commonly used types in C programming for quick access. Type Size (bytes) Format Specifier int at least 2, usually 4 %d, %i char 1 %c float 4 %f double 8 %lf short int 2 usually %hd unsigned int at least 2, usually 4 %u long int at least 4, usually 8 %ld, %li long long int at least 8 %lld, %lli unsigned long int at least 4 %lu unsigned long long int at least 8 %llu signed char 1 %c unsigned char 1 %c long double at least 10, usually 12 or 16 %Lf
  • 26. 1. int Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0, -5, 10 We can use int for declaring an integer variable. int id; Here, id is a variable of type integer. You can declare multiple variables at once in C programming. For example, int id, age; The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647.
  • 27. 2. float and double float and double are used to hold real numbers. float salary; double price; In C, floating-point numbers can also be represented in exponential. For example, float normalizationFactor = 22.442e2; What's the difference between float and double? The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes
  • 28. 3. char Keyword char is used for declaring character type variables. For example, char test = 'h'; The size of the character variable is 1 byte.
  • 29. Void void is an incomplete type. It means "nothing" or "no type". You can think of void as absent. For example, if a function is not returning anything, its return type should be void. Note that, you cannot create variables of void type
  • 30. short and long If you need to use a large number, you can use a type specifier long. long a; long long b; long double c; Here variables a and b can store integer values. And, c can store a floating-point number. If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use short. short d;
  • 31. You can always check the size of a variable using the sizeof() operator. #include <stdio.h> int main() { short a; long b; long long c; long double d; printf("size of short = %d bytesn", sizeof(a)); printf("size of long = %d bytesn", sizeof(b)); printf("size of long long = %d bytesn", sizeof(c)); printf("size of long double= %d bytesn", sizeof(d)); return 0; }
  • 32. signed and unsigned In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them: •signed - allows for storage of both positive and negative numbers •unsigned - allows for storage of only positive numbers For example: // valid codes unsigned int x = 35; int y = -35; // signed int int z = 36; // signed int // invalid code: unsigned int cannot hold negative integersunsigned int num = -35;
  • 33. LO.2. C Input / Output (I/O)
  • 34. Example explained 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. Line 2: A blank line. C ignores white space. But we use it to make the code more readable. Line 3: Another thing that always appear in a C program, is main(). This is 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".
  • 35. Note that: Every C statement ends with a semicolon ; Note: 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. 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.
  • 36. C Output (PrintText) In C programming, printf() is one of the main output function. The function sends formatted output to the screen. Example 1: #include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming"); return 0; } Output C Programming
  • 37. Example 2: Integer Output #include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; } Output Number = 5 NOTE: We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the value of testInteger.
  • 38. Example 3: float and double Output #include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %fn", number1); printf("number2 = %lf", number2); return 0; } Output number1 = 13.500000 number2 = 12.400000 NOTE: To print float, we use %f format specifier. Similarly, we use %lf to print double values.
  • 39. Example 4: Print Characters #include <stdio.h> int main() { char chr = 'a'; printf("character = %c", chr); return 0; } Output character = a NOTE: To print char, we use %c format specifier.
  • 40. To insert a new line, you can use the n character: 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. New Lines
  • 41. C 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. Types of Comments There are two ways to add comments in C: •// - Single Line Comment /*...*/ - Multi-line Comment
  • 42. 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: This example uses a single-line comment at the end of a line of code: // This is a comment printf("Hello World!"); printf("Hello World!"); // This is a comment
  • 43. C Multi-line Comments Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by the compiler: /* The code below will print the words Hello World! to the screen, and it is amazing */ printf("Hello World!");
  • 44. Use of Comments in C 1. Make Code Easier to Understand If we write comments on our code, it will be easier to understand the code in the future. Otherwise you will end up spending a lot of time looking at our own code and trying to understand it. Comments are even more important if you are working in a group. It makes it easier for other developers to understand and use your code. 2. Using Comments for debugging While debugging there might be situations where we don't want some part of the code.
  • 45. C Operators - An operator is a symbol that operates on a value or a variable. C divides the operators into the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Logical operators • Bitwise operators
  • 46. Operator Meaning of Operator + addition or unary plus - subtraction or unary minus * multiplication / division % modulus / remainder after division (modulo division) ++ increment (Increases the value of a variable by 1) - - decrement (Increases the value of a variable by 1) 1. Arithmetic Operators - An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
  • 47. #include <stdio.h> int main() { int a,b,c; printf("first number: n”); scanf(“%d”, &a); printf("second number: n”); scanf(“%d”, &b); c = a + b; printf("answer:”, c); return 0; } First number: 6 Second number: 10 answer: 16 Example: CODE OUTPUT
  • 48. 2. C Increment and Decrement Operators C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
  • 49. // Working of increment and decrement operators #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } Example: CODE OUTPUT ++a = 11 --b = 99 ++c = 11.500000 --d = 99.500000 Here, the operators ++ and -- are used as prefixes. These two operators can also be used as postfixes like a++ and a--.
  • 50. Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b 3. Assignment Operator An assignment operator is used for assigning a value to a variable. The most common assignment operator is =.
  • 51. #include <stdio.h> int main() { int x = 10; x += 5; printf("%d", x); return 0; } 15 Example: CODE OUTPUT
  • 52. Operator Meaning of Operator Example == Equal to 5 == 3 is evaluated to 0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to 0 != Not equal to 5 != 3 is evaluated to 1 >= Greater than or equal to 5 >= 3 is evaluated to 1 <= Less than or equal to 5 <= 3 is evaluated to 0 4. C Relational / Comparison Operators A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision-making and loops.
  • 53. #include <stdio.h> int main() { int x = 5; int y = 3; printf("%d", x > y); // returns 1 (true) because 5 is greater than 3 return 0; } 1 Example: CODE OUTPUT
  • 54. Operator Meaning Example && Logical AND.True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. || Logical OR.True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. ! Logical NOT.True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0. 5. C Logical Operators An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
  • 55. // Working of logical operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d n", result); result = !(a != b); printf("!(a != b) is %d n", result); result = !(a == b); printf("!(a == b) is %d n", result); return 0; }
  • 56. (a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0 OUTPUT: Explanation of logical operator program  (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).  (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).  (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).  (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).  !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).  !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
  • 57. 6. The sizeof operator The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc). include <stdio.h> int main() { int a; float b; double c; char d; printf("Size of int=%lu bytesn",sizeof(a)); printf("Size of float=%lu bytesn",sizeof(b)); printf("Size of double=%lu bytesn",sizeof(c)); printf("Size of char=%lu byten",sizeof(d)); return 0; }
  • 58. Output Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte