Lakindu Kariyawasam
I’m an IT enthusiast, FOSS lover, eager to try new technologies, good in problem solving, Excellent in teamwork management & communicating.
C is a structured Programming language
C is case sensitive
}
4 different names
name
Name
NAME
naMe
#include <stdio.h>
int main ()
{
//print something
printf("Welcome to C");
}
header
main function
comment
body
Comments
Single line comment
Multi line comment
/* This is
a multi line
comment*/
//single line comment
Data Types | Storage size ( bytes) |
printf( ) conversion specification |
scanf( ) conversion specification |
---|---|---|---|
int | 2 or 4 | %d | %d |
char | 1 | %c | %c |
float | 4 | %f | %f |
double | 8 | %f | %f |
#include <stdio.h>
int main() {
printf("Hello world!");
return 0;
}
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d",a);
return 0;
}
Variables
(Memory locations)
Initialization
#include <stdio.h>
int main() {
int number;
}
#include<stdio.h>
int main()
{
int number = 10;
}
Data type variable name
int x ;
Declaration
Data type variable name value
int x = 10;
Variable Names – Rules
Underscore(_)
Capital Letters ( A – Z )
Small Letters ( a – z )
Digits ( 0 – 9 )
Should not be a reserved word
Should start with a letter or an underscore
No other special charaters are allowed including space, commas
Variable names are case sensitive
Use allowed Characters
Operators
Arithmetic
#include <stdio.h>
int main() {
int a = 20;
int b = 10;
printf("addition is: %d \n",a+b);
printf("substraction is: %d \n",a-b);
printf("multiplication is: %d \n",a*b);
printf("division is: %d \n",a/b);
}
Output
addition is: 30
substraction is: 10
multiplication is: 200
division is: 2
Operators
Relational
less than
Greater than
less than or equal to
Greater than or equal to
is equal to
is not equal to
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
printf("Line 1 - a is equal to b\n" );
}
else {
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
}
else {
printf("Line 2 - a is not less than b\n" );
}
if ( a > b ) {
printf("Line 3 - a is greater than b\n" );
}
else {
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than or equal to b\n" );
}
if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
Output
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
Operators
Logical
AND
OR
NOT
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a < 10 && b < 100 ) {
printf("Line 1 - Condition is true\n" );
}
if ( a > 10 || b >10 ) {
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */
a = 8;
b = 10;
if ( a > 10 && b > 20 ) {
printf("Line 3 - Condition is true\n" );
}
else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a <10) && (b > 5) ) {
printf("Line 4 - Both conditions are true\n");
}
else
{
printf("Line 4 - Both conditions are true. \n"
"But, status is inverted as false\n");
}
}
Output
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Both conditions are true.
But, status is inverted as false
Operators
Assignment
Assignment (continued)
#include <stdio.h>
main() {
int a = 6;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}
Output
Line 1 - = Operator Example, Value of c = 6
Line 2 - += Operator Example, Value of c = 12
Line 3 - -= Operator Example, Value of c = 6
Line 4 - *= Operator Example, Value of c = 36
Line 5 - /= Operator Example, Value of c = 6
Line 6 - %= Operator Example, Value of c = 2
Line 7 - <<= Operator Example, Value of c = 8
Line 8 - >>= Operator Example, Value of c = 2
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2
Operators
Misc
- Conditional Expression
- Returns the size of a variable (Bytes)
- Returns the address of a variable
- Pointer to a variable
#include <stdio.h>
main() {
int a = 4;
short b;
double c;
int* ptr;
/* example of sizeof operator */
printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
/* example of & and * operators */
ptr = &a; /* 'ptr' now contains the address of 'a'*/
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);
/* example of ternary operator */
a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
printf( "Value of b is %d\n", b );
}
Output
Line 1 - Size of variable a = 4
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20
pre - changes before using it in a expression
post - changes after using it in a expression
#include <stdio.h>
int main()
{
int x = 10;
int a;
a = ++x;
//Pre Increment Operation
// Value of a will change
printf("%d" \n, a);
// Value of x change before execution of a=++x;
printf("%d", x);
return 0;
}
Output
11
11
#include <stdio.h>
int main()
{
int x = 10;
int a;
a = x++;
//Post Increment Operation
// Value of a will not change
printf("%d" \n, a);
// Value of x change after execution of a=x++;
printf("%d", x);
return 0;
}
Output
10
11
for ( initialization ; condition checking ; increment )
{
set of statment
}
for(int a = 5 ; a < 10 ; a++)
{
printf("Hello World");
}
while( condition )
{
Statement ;
}
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
do
{
Set of statement ;
} while ( condition ) ;
int x = 1;
do{
printf("%d\n", x);
x++;
} while(x <= 5);
If (condition)
{
//execution if condition is true
Statement 1 ;
}
else
{
//execution if condition is false
Statement 2 ;
}
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
if / else (example)
{
case 1 : //statements
break;
case 2 : // statement ;
break ;
case 3 : //statement ;
break ;
default : // default statements
}
#include <stdio.h>
int main () {
char grade;
scanf("%c",&grade);
switch(grade) {
case 'A' :
printf("Excellent!" );
break;
case 'B' :
printf("Well done" );
break;
case 'C' :
printf("You passed" );
break;
case 'F' :
printf("Better try again" );
break;
default :
printf("Invalid grade" );
}
}
Conditional statement (example)
#include<stdio.h>
int main()
{
//statment
}
int area(int x,int result)
{
//statment
return 0;
}
return type
Parameters
Function name
#include <stdio.h>
int main(){
//isalpha( ) function in C language checks
//whether given character is alphabetic or not
char ch;
printf("Enter any character\n");
scanf("%c", &ch);
if ( isalpha ( ch ) )
printf ( "\nEntered character is alphabetic" ) ;
else
printf ( "\nEntered character is not alphabetic" ) ;
}
No return type no parameters
No return type with parameters
With return type no parameters
With return type with parameters
#include<stdio.h>
void myFunction(); // function declaration
int main()
{
myFunction(); // function call
return 0;
}
void myFunction() // function definition
{
//your code
}
No return type no parameters
#include<stdio.h>
void greatNum(); // function declaration
int main()
{
greatNum(); // function call
return 0;
}
void greatNum() // function definition
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j) {
printf("The greater number is: %d", i);
}
else {
printf("The greater number is: %d", j);
}
}
No return type with parameters
#include<stdio.h>
void greatNum(int a, int b); // function declaration
int main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
greatNum(i, j); // function call
return 0;
}
void greatNum(int x, int y) // function definition
{
if(x > y) {
printf("The greater number is: %d", x);
}
else {
printf("The greater number is: %d", y);
}
}
With return type no parameter
#include<stdio.h>
int greatNum(); // function declaration
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum() // function definition
{
int i, j, greaterNum;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j) {
greaterNum = i;
}
else {
greaterNum = j;
}
// returning the result
return greaterNum;
}
with return type with parameters
#include<stdio.h>
int greatNum(int a, int b); // function declaration
int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum(int x, int y) // function definition
{
if(x > y) {
return x;
}
else {
return y;
}
}
An array is a variable that can store multiple values
Array index start with 0
data type array name [array size]
int number [5] ;
How to declare an array ?
1
0
2
3
4
index
element
#include<stdio.h>
int main()
{
int array [5] = { 5, 10, 8, 2, 4, 12 };
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", numbers[i]);
}
}
#include <stdio.h>
int main() {
int array[5];
printf("Enter 5 integer numbers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; i++) {
scanf("%d", &array[i]);
}
printf("Print numbers\n ");
// print it
for(int i = 0; i < 5; i++) {
printf("%d\n", array[i]);
}
return 0;
}
Take 5 inputs from user and store them in a array and print the array
Two Dimensional Arrays
[0,0] | [0,1] | [0,2] | [0,3] |
---|---|---|---|
[1,0] | [1,1] | [1,2] | [1,3] |
[2,1] | [2,1] | [2,2] | [2,3] |
[3,0] | [3,1] | [3,2] | [3,3] |
column
row
leave us your comment !
By Lakindu Kariyawasam
This is a beginner guide to understand C programming language concepts and theories. #DiveIntoC #FOSSNSBM
I’m an IT enthusiast, FOSS lover, eager to try new technologies, good in problem solving, Excellent in teamwork management & communicating.