C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Exercise 2

C Programming

In this exercise section of the tutorial we will write some C program.

Write a program in C to multiple two integer values and print the result

To multiple we use the * multiplication operator.

/**
 * file: multiply.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find the product of two numbers
 */

#include <stdio.h>
int main(void)
{
  int a = 10, b = 20;
  int product = a * b;
  printf("Product of axb = %d\n", product);
  return 0;
}

Output

Product of axb = 200

Write a program in C to find the area of a rectangle

Formula to find the area of a rectangle is length * width.

For this example we will use the floating point data type to store the value of the sides and the answer.

To print floating point value we pass "%f" in the printf( ) function.

/**
 * file: multiply.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find the area of rectangle
 */

#include <stdio.h>
int main(void)
{
  float length = 12.34, width = 56.78;
  float area = length * width;
  printf("Area: %f\n", area);
  return 0;
}

Output

Area: 700.665222

Write a program in C to find the speed of a train if it covers 300 km in 2 hours

Formula to find speed is speed = distance/time.

To divide numbers we use the / division operator.

Click here to learn about Speed Distance Time.

/**
 * file: speed.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find speed
 */

#include <stdio.h>
int main(void)
{
  float
    distance = 300,
    time = 2;
  float speed = distance / time;
  printf("Speed: %f\n", speed);
  return 0;
}

Output

Speed: 150.000000

Write a program in C to convert 100 degree Celsius into Fahrenheit

Formula to convert temperature from Celsius to Fahrenheit is given below.

F = C * 1.8 + 32

Where F is temperature in degree Fahrenheit and C is temperature in degree Celsius.

/**
 * file: temp.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find temp
 */

#include <stdio.h>
int main(void)
{
  float temp_c = 100;
  float temp_f = temp_c * 1.8 + 32;
  printf("%f deg C = %f deg F\n", temp_c, temp_f);
  return 0;
}

Output

100.000000 deg C = 212.000000 deg F