C

Module 1

index

no name
00 Basics of C language
01 hello world
02 Get value from user
03 Multiple Source File
04 Variables
05 Operators
06 Formate Spacifier
07 Constant
08 Escape sequence
09 If else
10 Switch Case

Basics

C language comments :

// this is single line comment
/* 
This is multiple line comment

You can writ anything hear... compilier just ignore this..
*/

Basic rules:

Basic Structuer Of C language

C language basic structure

Compiling and Linking

GCC Compiler:

Compiling:

Linking:

Q&A

Hello World

printf

#include <stdio.h>

int main()
{
    printf("hello world\n");
    return 0;
}

Basic calculation with c

int a, b;
a = 34;
b = 6;
printf("a - b = %d\n", a-b);
printf("a + b = %d\n", a+b);
printf("a * b = %d\n", a*b);
printf("a / b = %d\n", a/b);

Exercise with answer 🔬

Attachments 📎

Get value from user

#include<stdio.h>

int main()
{
    int a; // define a
    printf("Enter first Value\n");
    scanf("%d", &a); // get value from user
    printf("Our value of a is %d", a); // output
    return 0;
}

Exercise with answer 🔬

Attachments 📎

Multiple Source File

cc filename-1.c ... filename-n.c
cc -c mod1.c
cc -c mod2.c
cc mod1.o mod2.o
cc mod1.c mod2.o

Exp:

main.c:

#include <stdio.h>
int multiplication();

int main(int argc, char const *argv[])
{
    int x;
    printf("enter the value for multiplication: ");
    scanf("%d", &x);
    multiplication(x);
    return 0;
}

multipli.c:

#include <stdio.h>
#define MAX 10

int multiplication(int x)
{
    int i = 1;
    while (i <= MAX)
    {
        printf("%3d x %2d = %5d \n", x, i, x*i);
        i++;
    }
}
cc main.c multipli.c

Attachments 📎

Variables

A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

The example of declaring the variable is given below:

int a;  
float b;  
char c; 

// assigning values method 1
// first declare then assign
int a;
a = 44;
// method 2
// declare and asign at same time
int a = 44;
float b = 43.33;
float c = 48.44f; // only float support f after value, This helps to know this is flot value.
                  // output are always same with or without f.

Hear, a, b, c are variables. The int, float, char are the data types.

Rules for defining variables

Types of Variables in C

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

External variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

ex:

myfile.h

extern int x=10;//external variable (also global)  

program1.c

#include "myfile.h"  
#include <stdio.h>  
void printValue(){  
    printf("Global variable: %d", global_variable);  
}  

Attachments 📎

Operators

An Operators is a symbol used to perform operations in given programming language.

Types of operators

  1. Arithmetic operators
  2. Relation Operators
  3. Logical Operators
  4. Bit wise operators
  5. Assignment operators
  6. Miscellaneous

Arithmetic operators

Operators Description
+ Addition
- Subtraction
* Multiplication
/ Division
% modules

Relation operator

Operators Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than
<= Less than or equal to

Note: mostly this operators are used in if, else statement.

Logical operator

Operators Description Example
&& Logical “AND” operator, both the operands are non-zero then condition is true. a&&b
Logical “OR” operator, if any of these two operands is none-zero, Then condition become true. a∥b
! Logical “NOT” operator, It is used to reverse the logical state of its operand, if condition is true. !a

Bit wise operators

Value A Value B A&B A∣B A^B
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assignment operators

Operators Description
= Simple assignment operator assign value from right side operands
+= Add AND assignment operator. It’s adds the right operand to the left operands and assign the result to the left operands
-= Subtract AND assignment operator. It subtracts the right operands from the left to the result is assignment to the left operand.
*= Multiply AND assignment operator. It multiples the right operands from the left to the result is assignment to the left operand.
/= Divide AND assignment operator. It divide the right operands from the left to the result is assignment to the left operand.

Miscellaneous

Operators Description Example
sizeof() Returns the size of variable. sizeof(a), where a is an integer, will return in’s size on that architachure.
& Returns the address of variable. &a; return the actual address of the variable.
* Pointer variable *a;
?: Conditonal expression if condition is true, The value X; Otherwise value Y

Operators PRECEDENCE IN C

c operators precedence

Attachments 📎

Format Specifier

format work
%c character print
%d integer print
%f float print
%l long print
%lf double print
%LF long double print

Attachments 📎

Constant

The #define Directive:

Attachments 📎

Escape sequence

escap sequance box

Example:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    printf("\t Horizontal tab\n");
    return 0;
}

If else

Decison Making

if Statement

Exp:

if(i == 5)
{
  printf("You entered 5 as value");
}

Flow Chart:

If statement flow chart

if else statement

Exp:

if (i<j)
{
  max = j;
}
else
{
  max = i;
}

Flow Chart:

If else statement flow chart

Cascaded if

Exp:

if (n<0) {
  printf("n is less than 0\n");
}
else if (n ==0 ){
    printf("n is equalt to 0\n");
  }
else {
  printf("n is greater than 0\n");
}

Flow Chart:

else if cascaded

Nested if else

Exp:

if (i > j){
    if(i > k){
      max = i;
    }
    else{
      max = k;
    }
}
else
  if( j > k){
    max = j;
  }
  else {
    max = k;
  }

Flow Chart:

else if nested

Exercise with answer 🔬

Switch Case Statement

Rules:

  1. Switch expression must be int or char.
  2. Case value must be int or char.
  3. Case must come inside switch.
  4. Break is not a must.

Note: If break is not added, code will execute below cases also.

Types:

  1. Switch case without break.
  2. Switch case.
  3. Nested switch case

Syntax:

switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases

Switch case without break

ex:

switch (n)
    {
    case 3:
        printf("You entered 3\n");
    case 5:
        printf("You entered 5\n");
    case 8:
        printf("You entered 8\n");
    default:
        printf("Please enter 3, 5 or 8 only.\n");
    }

Switch case

ex:

switch (n)
{
case 3:
    printf("You entered 3\n");
    break;
case 5:
    printf("You entered 5\n");
    break;
case 8:
    printf("You entered 8\n");
    break;
default:
    printf("Please enter 3, 5 or 8 only.\n");
    break;
}

Nested switch case

switch (age)
{
case 22:
    printf("your age is 22\n");
    switch (marks) // Nested switch
    {
    case 33:
        printf("your maaks is 33\n");
        break;
    default:
        printf("your marks is not 33\n");
        break;
    }
    break;
case 17:
    printf("your age is 17\n");
    break;
default:
    printf("Please enter valid age\n");
}

Attachments 📎