C-Programing Note PDF by Nafi
C-Programing Note PDF by Nafi
• Syntex:
Input Output
#include <stdio.h> Hello world
int main() {
printf(“Hello world”);
return 0;
}
Input Output
#include <stdio.h> hello world
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.
2. 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.
3. printf() is a function used to output/print text to the screen. In our example it will
output "Hello world".
1|C Programming
We can use as many printf() functions as we want. However, it does not insert a new line
at the end of the output:
Input Output
#include <stdio.h> IamlearningC programming
int main() {
printf("I");
printf("am");
printf("learning");
printf("C programming");
return 0;
}
• New lines:
Input Output
#include <stdio.h> I
am
int main() { learning
printf("I\n"); C prgramming
printf("am\n");
printf("learning\n");
printf("C programming\n");
return 0;
}
we can also output multiple lines with a single printf() function. [Harder to read]
Input Output
#include <stdio.h> My
name
int main() is
{printf("My\nname\nis\nnafi");return 0;} Nafi
2|C Programming
Two \n (\n\n) characters after each other will create a blank line
Input Output
#include <stdio.h> Cristiano
Commenting
Input Output
#include <stdio.h> Hello world
int main() {
printf(“Hello world”);
// This is a comment
return 0;
}
Input Output
#include <stdio.h> Hello world
int main() {
printf(“Hello world”);
/* This is a comment
I want to learn c
programming*/
return 0;
}
3|C Programming
C Variables
Variables are containers for storing data values, like numbers and characters.
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
For example :
int my_age = 20;
float my_cgpa = 3.5;
char my_name = “Nafi”;
• Output Variables:
Format specifier: 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.
For example, to output the value of an int variable, use the format specifier %d surrounded
by double quotes (""), inside the printf() function, o print other types, use %c for char
(%s for string) and %f for float. Int = %d
Float = %f
Char = %c , %s (string)
Input Output
#include <stdio.h> 20
N
int main() { 53.500000
char my_name = “N”;
int my_age = 20;
float my_weight = 53.5;
printf(“%d/n”, my_age);
printf(“%c/n”, my_name);
printf(“%f/n”, my_weight);
}
4|C Programming
When writing a string to a variable, the limit of the string should be enclosed in [].
and use %s
Limit = Total word in sentence + 1 (Nafi = 4; 4+1 = 5)
Input Output
#include <stdio.h> My name is Nafi
int main() {
char name[5] = “Nafi”;
printf(“My name is %s”,name);
}
To combine both text and a variable, separate them with a comma inside
the printf() function
Input Output
#include <stdio.h> My age
int main() {
int age = 20;
printf(“My age is %d”,age);
}
To print different types in a single printf() function, we can use the following
Input Output
#include <stdio.h> My name is
Nafi and My
int main() { age is 20
char name = “Nafi”
int age = 20;
printf(“My name is %s and my age is %d”,name,age);
}
5|C Programming
• Varriables name
Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
• Multiple Variable
▪ To declare more than one variable of the same type, use a comma-separated list
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
▪ We can also assign the same value to multiple variables of the same type
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);
Input Output
#include <stdio.h> Summation : 45
int main() {
int x, y, sum;
x= 10,y = 15,z = 20;
sum = x+y+z;
printf(“Summation : %d”,sum);
}
6|C Programming
C Data Types
Input Output
#include <stdio.h> 10.550000
3.141600
int main() {
float x = 10.55;
double y = 3.1416;
printf(“%f\n%f”,x,y);
}
7|C Programming
If we want to remove the extra zeros (set decimal precision), we can use a dot (.) followed
by a number that specifies how many digits that should be shown after the decimal point.
Input Output
#include <stdio.h> 10.55
3.1416
int main() {
float x = 10.55;
double y = 3.1416;
printf(“%.2f\n%.4f”x,y);
}
Type Conversion
Sometimes, We have to convert the value of one data type to another type. This is
known as type conversion.
For example, if we try to divide two integers, 5 by 2, we would expect the result to be 2.5.
But since we are working with integers (and not floating-point values), the following
example will just output 2.
To get the right result, we need to know how type conversion works.
• Implicit Conversion
Implicit conversion is done automatically by the compiler when we assign a value of one
type to another. (automatic conversion int to float)
Input Output
#include <stdio.h> 5.00
int main() {
float a = 5;
printf(“%.2f”,a);
}
8|C Programming
(automatic conversion float to int )
Input Output
#include <stdio.h> 11
int main() {
int a = 11.11;
printf(“%d”,a);
}
if we divide two integers: 5 by 2, we know that the sum is 2.5. But if we put the answer
as float, but the result will only display 2.000000. Because 5 and 2 are still
integers in division.
Input Output
#include <stdio.h> 2.000000
int main() {
int a,b;
a = 5, b = 2;
float ans = a/b;
printf(“%f”,ans);
}
• Explicit Conversion
Explicit conversion is done manually by placing the type in parentheses () in front of the
value.
Considering our problem from the example above, we can now get the right result.
Input Output
#include <stdio.h> 2.5
int main() {
int a,b;
a = 5, b = 2;
float ans = (float) a/b;
printf(“%.1f”,ans);
}
9|C Programming
Constants
If we don't want others (or yourself) to change existing variable values, you can use
the const keyword.
Input Output
#include <stdio.h> Error
int main() {
const int my_age = 20;
int my_age = 21;
printf(“%d”,my_age);
}
Note: When we declare a constant variable, it must be assigned with a value. However it
will not work
C Operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
• Misc.
10 | C P r o g r a m m i n g
• Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
• Assignment Operators
Assignment operators are used to assign values to variables.
11 | C P r o g r a m m i n g
• Comparison/Relational Operators
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.
• Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values.
Input Output
#include <stdio.h> 4r
int main() {
int my_age = 20;
printf(“%lu”,sizeof(my_age));
}
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.
12 | C P r o g r a m m i n g
• Bitwise Operator
In the arithmetic-logic unit (which is within the CPU), mathematical operations like:
addition, subtraction, multiplication and division are done in bit-level. To perform bit-level
operations in C programming, bitwise operators are used.
Input Output
#include <stdio.h> //1st step:
What is the number :
int main() {
int a ; /*2nd step: enter the number
printf(“What is the number : ”); and press Enter*/
scanf(“%d”,&a); What is the number : 10
printf(“The user input is : %d,a); The user input is : 10
13 | C P r o g r a m m i n g
C Booleans
Very often, in programming, you will need a data type that can only have one of two values,
like:
• YES / NO
• ON / OFF
• TRUE / FALSE
• Boolean Variables
n C, the bool type is not a built-in data type, like int or char.
It was introduced in C99, and you must import the following header file to use it:
#include <stdbool.h>>
▪ A boolean variable is declared with the bool keyword and can only take the
values true or false
Before trying to print the boolean variables, we should know that boolean values are
returned as integers:
Input Output
#include <stdio.h> 1
#include <stdbool.h> 0
int main() {
bool ProgrammingIsFun = true; //1 means true
bool SociologyIsBoring = false; //0 means false
printf(“%d\n”,ProgrammingIsFun);
printf(“%d”,SociologyIsBoring);
}
Note: Remember to include the <stdbool.h> header file when working with bool variables.
14 | C P r o g r a m m i n g
• Comparing Values and Variables:
We can compare two values using comparison operator, and it will helps us to find answer
and make decisions.
Input Output
#include <stdio.h> 0
1
int main() { 1
int a,b,c ; 0
a = 10;
b = 12;
c = 10;
printf(“%d\n”, a > b); //false
printf(“%d\n”, a < b); //true
printf(“%d\n”, a == c); //true
printf(“%d\n”, b == c); //false
}
15 | C P r o g r a m m i n g
C If…….Else
We have already learned that C supports the usual logical conditions from mathematics:
We can use these conditions to perform different actions for different decisions.
• The if Statement:
Use the if statement to specify a block of code to be executed if a condition is true.
Input Output
#include <stdio.h> a is greater than br
int main() {
int a = 5;
int b = 2;
if (a > b){
print(“a is greater than b”);
}
}
16 | C P r o g r a m m i n g
• The else Statement:
Use the else statement to specify a block of code to be executed if the condition is false.
Input Output
#include <stdio.h> b is greater than ar
int main() {
int a = 2;
int b = 5;
if (a > b){
printf(“a is greater than b”);
}
else {
printf(“b is grater than a”);
}
}
Input Output
#include <stdio.h> a is equal b
int main() {
int a = 5;
int b = 5;
if (a > b){
printf(“a is greater than b”);
}
else if (a == b){
printf(“a is equal b”);
}
else {
printf(“b is grater than a”);
}
}
17 | C P r o g r a m m i n g
• C switch Statement:
Instead of writing many if..else statements, you can use the switch statement.
Break :
When C reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more
testing
Default:
Default works like else in if else statement. If use default keyword in switch block. It will
specifies some code to run if there is no case match.
18 | C P r o g r a m m i n g
C While Loop
Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more
readable.
• While Loop
The while loop loops through a block of code as long as a specified condition is true
Syntax:
While (condition){
//code block to be execute
}
Input Output
#include <stdio.h> 0
int main() { 1
int i = 0; 2
while (i<5){ 3
printf(“%d\n”,i); 4
i++; //implement
}
}
Note: Do not forget to increase the variable used in the condition (i++), otherwise the
loop will never end!
19 | C P r o g r a m m i n g
• The Do While loop
The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax
do {
//code block to be executed
}
while (condition)
Input Output
#include <stdio.h> 0
1
int main() { 2
int i = 0; 3
4
do {
printf(“%d\n”,i);
i++; //implement
}
Do not forget to increase the variable used in the condition, otherwise the loop will never
end!
20 | C P r o g r a m m i n g
C For Loops
• For loop
When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while loop.
Syntax
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Input Output
#include <stdio.h> 0
1
int main() { 2
for (int i = 0;i < 5; i++){ 3
printf(“%d\n”,i); 4
}
}
int i = 0,
for ( ; i < 5; i++){
printf(“%d\n”,i);
21 | C P r o g r a m m i n g
• Nasted Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop"
Input Output
#include <stdio.h> Outer: 1
1
int main() { 2
3
//Outer Outer: 2
for (int i = 1;i <= 2; i++){ 1
printf(“Outer: %d\n”,i); 2
3
//inner
for (int j = 1 ; j <= 3; j++){
printf(“%d\n”,j);
}
}
22 | C P r o g r a m m i n g