[go: up one dir, main page]

0% found this document useful (0 votes)
3 views25 pages

"Hello World!": #Include Main (Printf )

The document provides an overview of C programming syntax, including basic structure, data types, operators, control flow statements, loops, arrays, strings, user input, memory addresses, pointers, and functions. It explains key concepts such as the main function, printf for output, scanf for input, and how to declare and manipulate variables and arrays. Additionally, it covers conditional statements and loops, highlighting their usage and syntax in C programming.

Uploaded by

shochho020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

"Hello World!": #Include Main (Printf )

The document provides an overview of C programming syntax, including basic structure, data types, operators, control flow statements, loops, arrays, strings, user input, memory addresses, pointers, and functions. It explains key concepts such as the main function, printf for output, scanf for input, and how to declare and manipulate variables and arrays. Additionally, it covers conditional statements and loops, highlighting their usage and syntax in C programming.

Uploaded by

shochho020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

C Syntax

#include <stdio.h>

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

এটা হেচ্ছে ব্যািসক িসনট্যাক্সে। এখােন,

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 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. এই ফাংশন এমন না য খািল main() নােমর ই হেব। অেনক ফাংশন বানােনা
যােব। িকন্তু নরমািল ব্যািসক আমরা main() এ কির।

Line 4: printf() is a function used to output/print text to the screen. In our example, it will output
"Hello World!". printf() ও ফাংশন। এই ফাংশন এর কাজ ই হেচ্ছে িপ্রিন্ট করা। এটা হেচ্ছে িস এর লাইেব্রেির ফাংশন। এমন অেনক লাইেব্রেির
ফাংশন আেছ।

Line 5: return 0 ends the main() function. Return 0 মােন মইন ফাংশন থেক ০ এরর দখাইেস।
C Statements
printf("Hello World!");

It is important that you end the statement with a semicolon ;

If you forget the semicolon (;), an error will occur and the program will not run:

Most C programs contain many statements.

printf("Hello World!");
printf("Have a good day!");
return 0;

সহজ ভাষায় প্রিেত্যক লাইেনর পর আমরা


কােলান দেবা। মাস্ট।
এটা বাংলার দািড়র মেতা।
C Output (Print Text)
#include <stdio.h>

int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0; Escape Description
} Sequence

C New Lines \t Creates a horizontal tab

#include <stdio.h>

int main() { \\ Inserts a backslash


printf("Hello World!\n"); character (\)
printf("I am learning C.");
return 0;
}
\" Inserts a double quote
character
C Variables
Declaration Syntax Format Specifier printf() Syntax scanf() Syntax
Type

Simple / int x; % d printf("%d", x); scanf("%d", &x);


Basic (int)

Initialization int x %d printf("%d", x); (Not needed, already


= 10; initialized)

Multiple int a, %d %d %d printf("%d %d %d", a, scanf("%d %d %d", &a,


Declaration b, c; b, c); &b, &c);

Const const %d printf("%d", MAX); ❌ Cannot input into const


Variable int
MAX =
100;

Array (int[]) int %d (each element) printf("%d", nums[i]); scanf("%d", &nums[i]);


nums[5
];
C Data Types
Classification

Data types specify what kind of data a variable can hold.

● Primary (or Primitive): int, char, float, double

● Derived: array, pointer, structure, union

● User-defined: struct, union, enum, typedef

● Void: special type with no value


C Operators Arithmetic operators

Assignment operators

Comparison operators

Logical operators

Arithmetic Operators Bitwise operators

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x


Relational Operators
Operator Name Description Example

== Equal to True if values are equal a == b

!= Not equal to True if values are not equal a != b

> Greater than True if left is greater than right a > b

< Less than True if left is less than right a < b

>= Greater or equal True if left is >= right a >= b

<= Less or equal True if left is <= right a <= b


Assignment Operators
Operator Name Description Example

= Assignment Assigns right side value to left side variable a = 10

+= Add and assign Adds and assigns result a += 5 (a = a + 5)

-= Subtract and assign Subtracts and assigns result a -= 3 (a = a - 3)

*= Multiply and assign Multiplies and assigns result a *= 2 (a = a * 2)

/= Divide and assign Divides and assigns result a /= 4 (a = a / 4)

%= Modulus and assign Finds remainder and assigns a %= 3 (a = a % 3)


C If ... Else
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >=
b
● Equal to a == b
● Not Equal to: a != b

C has the following conditional statements:

● Use if to specify a block of code to be executed, if a specified condition is true


● Use else to specify a block of code to be executed, if the same condition is false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed

int x = 20;
int y = 18;
if (x > y) { If clause
printf("x is greater than y");
}
int time = 20;
if (time < 18) { Else clause
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."

int time = 22;


if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day."); Else…if clause
} else {
printf("Good evening.");
}
// Outputs "Good evening."
C Switch int day = 4;

switch (day) {
This is how it works: case 1:
printf("Monday");
● The switch expression is evaluated once break;
case 2:
● The value of the expression is compared with the values of each
printf("Tuesday");
case break;
● If there is a match, the associated block of code is executed case 3:
● The break statement breaks out of the switch block and stops the printf("Wednesday");
execution break;
● The default statement is optional, and specifies some code to run case 4:
printf("Thursday");
if there is no case match
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}

// Outputs "Thursday" (day 4)


C While Loop
The while loop loops through a block of code as long as a specified condition is true:

int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}

C 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.

int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
C For Loop
int i;

for (i = 0; i < 5; i++) {


printf("%d\n", i);
}

● Statement 1 sets a variable before the loop starts: int i = 0


● Statement 2 defines the condition for the loop to run: i < 5. If the condition is true, the loop will start over
again, if it is false, the loop will end.
● Statement 3 increases a value each time the code block in the loop has been executed: i++

C Break and Continue


break statement used to "jump out" of a switch statement. The break statement can also be used to jump out of a
loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next
iteration in the loop.
C Arrays
● Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
● To create an array, define the data type (like int) and specify the name of the array followed by square brackets
[].
● To insert values to it, use a comma-separated list inside curly braces, and make sure all values are of the same
data type:

int myNumbers[] = {25, 50, 75, 100};

● To access an array element, refer to its index number.


● Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
● This statement accesses the value of the first element [0] in myNumbers:

int myNumbers[] = {25, 50, 75, 100};


printf("%d", myNumbers[0]);

// Outputs 25

● To change the value of a specific element, refer to the index number:

myNumbers[0] = 33;
● I can also print array values using loop with the for loop.

int myNumbers[] = {25, 50, 75, 100};


int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

● Another common way to create arrays, is to specify the size of the array, and add elements later:

// Declare an array of four integers:


int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;

● It is important to note that all elements in an array must be of the same data type.
● One-dimensional:

int arr[5];

● Two-dimensional:

int matrix[3][4];

● Higher-dimensional:

int tensor[2][3][4];
C Strings
সহজ বাংলায় char ডাটা টাইপ + এের
Strings are used for storing text/characters. এর িমক্সেড ভাশর্শন হেচ্ছে িস্ট্রিং। এেরেত
For example, "Hello World" is a string of characters. সইম ডাটা টাইেপর অেনক িকছু িনেত
char greetings[] = "Hello World!";
পাির। সা এখােন char ডটা টাইেপর
অেনক িকছু িনিচ্ছে
● have to use double quotes ("").
● To output the string, you can use the printf() function together with the format specifier %s

char greetings[] = "Hello World!";


printf("%s", greetings);
C User Input
To get user input, we can use the scanf() function:

// Create an int and a char variable


int myNum;
char myChar;

// Ask the user to type a number AND a character


printf("Type a number AND a character and press enter: \n"
);

// Get and save the number AND character the user types
scanf("%d %c", &myNum, &myChar);

// Print the number


printf("Your number is: %d\n", myNum);

// Print the character


printf("Your character is: %c\n", myChar);
● You can also get a string entered by the user:

/ Create a string
char firstName[30];

// Ask the user to input some text


printf("Enter your first name: \n");

// Get and save the text


scanf("%s", firstName);

// Output the text


printf("Hello %s", firstName);

● when working with strings, we often use the fgets() function to read a line of text. Note that you must include
the following arguments: the name of the string variable, sizeof(string_name), and stdin:

char fullName[30];

printf("Type your full name:


\n");
fgets(fullName,
sizeof(fullName), stdin);

printf("Hello %s", fullName);


C Memory Address
● When a variable is created in C, a memory address is assigned to the variable.
● The memory address is the location of where the variable is stored on the computer.
● When we assign a value to the variable, it is stored in this memory address.
● To access it, use the reference operator (&), and the result represents where the variable is stored:

int myAge = 43;


printf("%p",); // Outputs 0x7ffe5367e044

এখােন য আউটপুট টা পােবা এটা হক্সো


ডিসেমল এ আসেব। আর প্রিেত্যকবার
য সইম আসেব তা না। এেকক বার
এেককটা। এটা আমার কিম্পিউটার অেটা
সট করেব।

আর এইেয &myAge এটা হেচ্ছে


পেয়ন্টার।
C Pointers
int myAge = 43; // an int variable

printf("%d", myAge); // Outputs the value of myAge (43)


printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)

পেয়ন্টার এর কাজ িক? সহেজ বলেল আিম হিচ্ছে একটা ভ্যািরেয়বল। নাম সােয়ম। আমার বাসার এেড্রেস x .
এইেয এেড্রেস টা সইভ রােখ পেয়ন্টার। এন্ড এই এেড্রেস ইউজার দয় না। এইটা অেটা এসাইন হয়। হক্সো
ডিসেমল িদেয়। কাড একচু য়ািল কাজ ও কের এভােব। কান একটা ভ্যািরেয়বল কল করেল ওইটার এেড্রেস
এেক্সেস কের , ওই এেড্রেেসর ভ্যালু িরিট্রিভ কের পাঠায়।
int myAge = 43; // An int variable
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge

// Output the value of myAge (43)


printf("%d\n", myAge);

// Output the memory address of myAge (0x7ffe5367e044)


printf("%p\n", &myAge);

// Output the memory address of myAge with the pointer (0x7ffe5367e044)


printf("%p\n", ptr);

এই পেয়ন্টার িকভােব কাজ করেস?


আমরা একটা পেয়ন্টার ভ্যািরেয়বল িক্রিেয়ট করিস ptr নােমর। that points to an int variable
(myAge).
আর অবশ্যই * িদেয় পেয়ন্টােরর কােজর সময় সইম ডাটা টাইপ রাখেত হেব।
& অপােরটর myAge এর মেমাির এেড্রেস সইভ রােখ। আর সই ভ্যালু টা স্টার হইেস ptr এর িভতর
C Functions
● A function is a block of code which only runs when it is called.
● You can pass data, known as parameters, into a function.
● Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and
use it many times.

Inside main, call myFunction():


main(). printf(), এগুলা িপ্রি
// Create a function িডফাইনড ফাংশন িছল। এখন আমরা
void myFunction() { আমােদর ইচ্ছো মেতা ফাংশন বানােবা
printf("I just got executed!");
এন্ড এর িভতের কাজ করােবা। যা ইচ্ছো
তা। ফাংশেনর মইন পারপাস ই হেচ্ছে
}
main() ফাংশন টােক িক্লিন রাখা।
আমরা জাস্ট মইন ফাংশেন অন্য
int main() { লংিদ ফাংশন কল করেবা। তােত ওই
myFunction(); // call the function ফাংশেনর কাজ গুলা হেব। নাহেল হেব
return 0;
না
}
C Function Parameters
returnType functionName(parameter1, parameter2, parameter3) {
// code to be executed
}

void myFunction(char name[]) { void myFunction(char name[], int age) {


printf("Hello %s\n", name); printf("Hello %s. You are %d years old.\n", name, age);
} }
int main() { int main() {
myFunction("Liam"); myFunction("Liam", 3);
myFunction("Jenny"); myFunction("Jenny", 14);
myFunction("Anja"); myFunction("Anja", 30);
return 0; return 0;
} }

Single Parameters Multiple Parameters


C Structures (structs)
● Structures (also called structs) are a way to group several related variables into
one place.
● Each variable in the structure is known as a member of the structure.
● Unlike an array, a structure can contain many different data types (int, float, এটা কাইন্ড অফ এের ই।
char, etc.).
জাস্ট অেনক গুলা
struct myStructure { িডফােরন্ট ডাটা টাইপ
িনয়া একটা এের।
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';

// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);

return 0;
}

You might also like