README
M-1-Basic-Syntax-variable-data-types-for-c
why c?
- Its too structured and mother of programming language
- There is no built in library or function so we can learn from 0.
1-1 Introduction
- what we will learn?
- Basic Structure of C
- Print in C
- Taking Input in C
- Variable and Data Types
- Data Types Limitation
- Variable naming Rules
1-2 Structure Of C Programming
- it has two parts
- Head Part - hold the header files (saves time and make us avoid some complicated code running behind the scene)
- Main Part - Holds the main code
1-2 First C Program
- c programming syntax
# include <stdio.h>
int main ()
{
printf("hello world"); // we have not wrote the function of printing its coming from header file
return 0;
}
- the stdio means the standard input and output
Running Our First C Code
# include <stdio.h>
int main () // this is the main function and its a function return type
{
printf("hello world"); // we have not wrote the function of printing its coming from header file
return 0; // return 0 means the code has run successfully
}
- c programming starts to execute from main function
Printing in C
#include<stdio.h>
int main()
{
printf("I am Sazid\n"); // for printing with brake \n is used
printf("demon\n");
printf("dem\ton");
return 0;
}
- for printing with brake \n is used
- for the Tab Space we can use \t
Variable and Data Type
- int -> -100, 0, 100
- float -> -2.5, 5.46
- char -> 'a', 'A', '@', 'H'
- boolean -> True or False
Variables in C
data-type variable name
#include<stdio.h>
int main()
{
int num; // declaring avariable will be stored in memory and we will assign data.
num=10;
// or we can also write
int num1 = 10;
num1 = 20;
float f = 3.56;
char c = 'i am sazid';
return 0;
}
-
variable is stored in memory and value is assigned and the code will be executed line by line
-
int holds 4 byte space in memory, float holds 4 byte space in memory, char holds 1 byte space in memory
-
1 byte means 8 bit. 010101(here is 6 bit) as compute understand 0 and 1 these are bit.
-
1024 bit means 1 kb
-
1024 kb means 1 mb
-
1024 mb means 1 gb
-
1024 gb means 1 tb
print a variable
#include<stdio.h>
int main()
{
int num; // declaring avariable will be stored in memory and we will assign data.
num=10;
// or we can also write
int num1 = 10;
num1 = 20;
float f = 3.56;
char c = 'i am sazid';
printf("num1"); // we cant print like this
return 0;
}
- we cant print like this We need format specifier for printing variables
- int -> %d
- float -> %f
- char -> %c
#include<stdio.h>
int main()
#include<stdio.h>
int main()
{
int num; // declaring avariable will be stored in memory and we will assign data.
num=10;
// or we can also write
int num1 = 10;
num1 = 20;
float f = 3.56;
char c = 'i am sazid';
// printf("num1"); // we cant print like this
printf("%d", num1);
printf("%f", f);
printf("%2f", f);
printf("%c", c);
return 0;
}
Boolean in c
- boolean datatype is hold in another header
#include<stdbool.h>
#include<stdio.h>
#include<stdbool.h>
int main()
{
bool b;
b = false;
printf("%d", b); // boolean has no format specifier so we will be using the integer specifier as it only about 0 and 1
return 0;
}
how to take input
- for taking input we need a function name scanf() same as printf. we also need a format specifier for this
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a); // it will take file and keep in a . its like as int is defined and defined a garbage value (0 or other) and the scanf will replace the taken input value. & meas the address of a . its responsible for changing the value
printf("%d", a);
return 0;
}
#include <stdio.h>
int main()
{
int a;
float f;
char c;
scanf("%d", &a); // it will take file and keep in a . its like as int is defined and defined a garbage value (0 or other) and the scanf will replace the taken input value. & meas the address of a . its responsible for changing the value
scanf("%f", &f);
scanf(" %c", &c);
printf("%d\n", a);
printf("%c\n", c);
printf("%f\n", f);
// we can also take multiple fields as well
scanf("%d %f %c", &a, &f, &c);
printf("%d\n%f\n%c\n", a, f, c);
return 0;
}
data type limitations
- data types has a fixed size. like large integer we have to use long long int -> size 8 byte
- for large float we have to use double -> 8 byte
lets calculate the size of the data types or data
- 1 bit mean either 0 or 1
- 2 bit means -> 00, 01, 10, 11 we can keep 4 numbers
- 3 bit means -> 000, 001, 010, 011, 100, 101, 110, 111
Lets make a pattern
- 2 power is the base 1 bit means 2 power 1
- 32 bits means 2 power 32 .
- we can keep 10 to the power 9 value max to a integer if more we have to use long long int it can take 10 to the power 18
#include <stdio.h>
int main()
{
long long int a;
printf("%lld", a); // specifier will be lld
return 0;
}
- for float we also have limitation
- we have to use double
#include <stdio.h>
int main()
{
double f;
printf("%lf", f);
return 0;
}
naming of variable
- rahim
- _rahim
- @rahim -> we cant do it
- 1243rahim -> we cant do it
- rahim 123 -> we cant do it
- rahim_123
- rahim123
- rahim,karim -> we cant use it
- int -> we can do it (or any c reserved words)