Contact : +88 01840-723661

|

Email : info@jakafind.com

C Programming Learning Tutorial P001

C Programming Learning Tutorial P001

 

# What is C Language?

C language is a structured, middle-level programming language developed by Dennis Ritchie.

@. Father: Dennis Ritchie.

@. Institution: Bell Laboratory.

@. System: Unix operating system.

@. From: BCPL -> B -> C

@. Use: Making operating system and gaming.

#. BCPL: Basic Combined Programming Language.

@. Some Topics of C language:

01 C Variable                          06. C If…else (Condition)

02 C Data Type                       07. C Switch

03 C Constant                         08. C Loop (for, while, do while, nested loop)

04 C Operators                        09. Recursion

05 C Booleans                          10. Array

# What is C Token?

A token is the smallest element of a program that is meaningful to the compiler.

Tokens can be classified as follows:

❏ Keywords
❏ Identifiers
❏ Constants
❏ Strings
❏ Special Symbols
❏ Operators

# What is Constant?

Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.

Constants refer to fixed values.


#include <stdio.h>
int main()
{
const int minutesPerHour = 60;
const float PI = 3.14;
printf("%d\n", minutesPerHour);
printf("%f\n", PI);
return 0;
}

Special Symbols

❏ Brackets[]
❏ Parentheses()
❏ Braces{}
❏ comma (, )
❏ semi colon (;)
❏ asterick (*)
❏ assignment operator(=)
❏ pre processor(#)

Operators

❏ Unary Operators (++, –)
❏ Binary Operators
❏ Arithmetic operators
❏ Relational Operators
❏ Logical Operators
❏ Assignment Operators
❏ Conditional Operators
❏ Bitwise Operators
❏ Ternary Operators (? )

Variables

Variables or Identifiers are the names that refer to sections of memory into which data can be stored.

Mainly Two types

❏ Local or internal Variables
❏ Global or external Variables

Skeleton of C Programming


#include <stdio.h>
int main ()
{
printf ("Hello Jakaria!\n");
return 0;
}


 

@. To print a new line use = \n

@. To print a new tab use = \t

@. If you want to add a extra header: [#include <limits.h>]

 

Data Type and Variables

No Data Type Keyword Size in bytes Range Place hold
01 Character char 1 -128 to 127 %c
02 Unsigned Character Unsigned char 1 0 to 255 %c
03 Integer int 2 or 4

-32768 to 32767 or

-2147483648 to 2147483647

%d
04 Unsigned Integer Unsigned int 2 or 4

0 to 65535 or

0 to 4,294,967,295

%u
05 Long Integer long int 4

-2147483648 to 2147483647

 

%d
06 Long Long Integer long long int 8

(-2^63) to (-2^63-1)

 

%d
07 Float float 4 1.2E-38 to 3.4E +38 %f
08 Double double 8 2.3E-308 to 1.7E +308 %lf

 

@. 1 Bit = 8 Byte

Use some variables


#include <stdio.h>
int main()
{
printf ("My account balance is %d taka\n", 50000);
printf("%d+%d=%d\n", 10, 20, 30);
printf("The star character is : %c\n", '*');
printf("My height is %f feet and weight is %d kg.\n", 5.6, 60); int x = 10; int y = 20;
printf("%d+%d=%d\n", x,y, x+y); x=x* y-20;
printf("Final value of x is %d \n", x);
return 0;
}

Details on Data Type

@.     Character – %c:

Character is worked by only alphabet. Its value is in 0 – 256.

Only 25 letters write by Character.

Signed char and Unsigned char = 1 byte

@.     Integer – %d:

Integer is worked by integer numbers. Its value is in 0 to 65535.

int and Unsigned int = 2 bytes

@.     Float – %f:

Integer is worked by Fraction and decimal numbers.

It is showed only 6 digits.

Float = 4 or 32 bytes

@.     Double: %lf

Double is worked by big decimal and scientific numbers more than float numbers.    (6.44, 8.11, 2*10^2, 5.2*10^14)

It is showed only 16 digits.

 

Operators, Precedence and Associativity

x = y + z

y = 5 – 5

Here,

Operator:  +, -, =, *, /, %, ++, –

Operand: x, y, z

Arithmetic Operators:

These are the operators used to perform arithmetic or mathematical operations on operands.

#. Arithmetic Operators are two types:

  1. Unary Operator
  2. Binary Operator

#. Unary Operator:

Operators that operates or works with a single operand are unary operators.

Such as: (z++, –x)

#. Binary Operator:

Operators that operates or works with two operand are binary operators.

Such as: (+, -, *, /, %)

#. Assignment Operator:

Such as: =, +=, -=, *=, /=. %=, a=b, a+=b, a-=b, a*=b, a/=b, a%=b

#. Relation Operator:

Relation Operators are used for compassion.

Checking if one operand is equal to other operand or not, an opearand is gratien than other operand or not etc.

Such as: (==, >=, <=, <, >, !=)

• > greater than
• >= greater than or equal to
• < less than
• <= less than or equal to
• = = equal to
• != not equal to

@. Logical Operators:

Logical Operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration.

Such as: Logical AND – &&, Logical OR –  ||, Logical NOT – !

The result of a logical operator is a boolean value either true of false.

& Logical AND
| Logical OR
^ Logical XOR
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND Assignment
|= OR Assignment
^ = XOR Assignment
= = Equal to
!= Not equal to
?: Ternary if-then-else

#. Incremental or Decremental Operator:

Such as: ++. —

#. Bitwise Operator:

Such as: &, |, ^, <<, >>, ~

~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Shift Right
>>> Shift Right zero fill
<< Shift left
& = Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise XOR Assignment
>>= Shift Right Assignment
>>>= Shift Right zero fill Assignment
<<= Shift Left Assignment

#. Conditional Operator:

Such as: exp1? exp2: exp3     [ here exp1, exp2, and exp3 is expression]

(expression) ? value if true : value if false
(a > b) ? a : b;
it is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is
true the first value, a, is returned. If it is false, the second value, b, is returned.

#. Misc Operator:

Such as: size(), &, [,]

To input data (use of scanf)


#include <stdio.h>
int main ()
{
int a;
int b;
printf("Enter First Integer Number: ");
scanf("%d", &a);
printf("Enter Second Integer Number: ");
scanf("%d", &b);
printf("Sum Two Integrer = %d", a+b);
return 0;
}

C Condition (use of if…else)


#include <stdio.h>
int main ()
{
int x;
printf("Enter  Your Number: ");
scanf("%d", &x);
if (x>50){
printf("%d is greater than 50 \n" x);}
else{
printf("%d is less than 50 \n" x);}
return 0;
}

C Condition (use of if…else if…else)


#include <stdio.h>
int main () {
int age;
printf("Enter value of age: ");
scanf("%d", &age);
if (age<2){
printf("Intant\n");
}
else if (age<10){
printf("Child\n");
} 
else if (age<20){
printf("Teenage\n"); 
}
else if (age<30){
printf("Adult\n");
}
else {
printf("%d\n");
}
return 0; }

C Switch Case


#include <stdio.h>
int main () {
int x;
printf("Enter One Integer Number (1-5): ");
scanf("%d", &x);
switch(x){
case 1:
printf("This ID is found: 1\n");
break;
case 2:
printf("This ID is found: 2\n");
break;
case 3:
printf("This ID is found: 3\n");
break;
case 4:
printf("This ID is found: 5\n");
break;
case 5: 
printf("This ID is found: 5\n");
break;
default:
printf("This ID is not found.\n"); }
return 0;
}

@. Case 1 = case 2 +4 use kora jay but case 2 + x use kora jay na.

@. Switch case just char and int use kora jay But Float and double use kora jayna.

 

C Booleans


#include <stdio.h>
#include <stdbool.h>
int main()
{
bool jakafindisgood = true;
bool jakabuzzisalsogood = false;
printf("%d", jakafindisgood == jakabuzzisalsogood);
return 0;
}

C While Loop


#include <stdio.h
int main()
{
int i = 0;
while (i < 20) {
printf("%d", i);
i++;
}
return 0;
}

C Do While Loop


#include <stdio.h
int main()
{
int i = 0;
do (i < 20) {
printf("%d", i);
i++;
}
while (i < 20);
return 0;
}

 

C For Loop


#include <stdio.h>
int main()
{ int i;
for(i=1; i<=20; i++)
{
printf("%d", i);
}
return 0;
}

 

C Nested For Loop


#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++)
{
printf("%d\n", i);
for (j = 10; j <= 20; j++) {
printf("%d ",j);
}
printf("\n");
}
return 0;
}

C Break


#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
return 0;
}

 

C  Continue


#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
return 0;
}

 

 

Thanks for your time!

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *