0% found this document useful (0 votes)
0 views

Learn_C_Using_Single_Example

The document provides a comprehensive guide on learning C programming through various examples, covering key concepts such as variables, arrays, functions, pointers, structures, unions, preprocessor directives, and enumerated data types. Each section includes code snippets demonstrating the implementation of these concepts. The document is authored by S. Gavaskar and Preethi T, aimed at assisting students in understanding fundamental programming techniques in C.

Uploaded by

gavaskar.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Learn_C_Using_Single_Example

The document provides a comprehensive guide on learning C programming through various examples, covering key concepts such as variables, arrays, functions, pointers, structures, unions, preprocessor directives, and enumerated data types. Each section includes code snippets demonstrating the implementation of these concepts. The document is authored by S. Gavaskar and Preethi T, aimed at assisting students in understanding fundamental programming techniques in C.

Uploaded by

gavaskar.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Learning C by Single Examples

1.Variable:
Example:
void main()
{
int number1,number2,sum; //variable declaration
number1=10;
number2=20;
sum=number1+number2;
printf("\n Sum of two numbers using variable is: %d",sum);
}
2.Array:
Example:
void main()
{
int numbers[2]={10,20}; //declaration of data into an array
int sum=numbers[0]+numbers[1];
printf("\n Sum of two numbers using array is %d",sum);
}
Function:
3.Function with no argument and no return value:
Example:
#include<stdio.h>
void sum() //function declaration
{
int number1,number2,sum;
number1=10;
number2=20;
sum=number1+number2;
printf("\nSum of two numbers using function with no
arguments and no return values is %d",sum);
}
void main()
{
sum();
}
4.Function with no argument and with return value
Example:
#include<stdio.h>
void main()
{
int add;
add=sum(); //function calling
printf("\n Sum of two numbers using function with no
arguments and with return value is %d",add);
}
int sum()
{
int number1=10;
int number2=20;
return(number1+number2);
}
5.Function with argument and no return value:
Example:
#include<stdio.h>
void addfn(int num1,int num2) //function with arguments
{
int sum=num1+num2;
printf("\nSum of two numbers using function with arguments
and no return value is %d",sum);
}
void main()
{
int number1,number2;
number1=10;
number2=20;
addfn(number1,number2); //function call
}
6. Function with argument and with return value:
Example:
#include<stdio.h>
int addfn(int number1,int number2)
{
int sum=number1+number2;
return sum; //return statement
}
void main()
{
int number1,number2;
number1=10;
number2=20;
printf("\n Sum of two numbers using function with argument
and return value is %d",addfn(number1,number2));
}
7.Pointers:
Example:
void main()
{
int *pointer1,*pointer2; //pointer variables
int number1,number2,sum;
number1=10;
number2=20;
pointer1=&number1;
pointer2=&number2;
sum=*pointer1+*pointer2;
printf("\nSum of two numbers using pointer is %d",sum);
}
8.Pointers and Arrays:
Example:
int numbers[2],sum=0;
void main()
{
numbers[0]=10;
numbers[1]=20;
sum=*(numbers+0)+*(numbers+1); //adding the first and
second position of the data in an array
printf("\nSum of two numbers using pointers and array is
%d",sum);
}
9.Pointers and Function:
Example:
void addfun(int *pointer1,int *pointer2) //function with arguments
{ as pointer variables
int sum=*pointer1+*pointer2;
printf("\nSum of two numbers using pointer and function is
%d",sum);
}
void main()
{
int number1,number2;
number1=10;
number2=20;
addfun(&number1,&number2);
}
10. Structures:
Example:
struct addstruct
{
int number1,number2;
}struct1; //structure object
void main()
{
int sum;
struct1.number1=10;
struct1.number2=20;
sum=struct1.number1+struct1.number2;
printf("\nSum of two numbers using structures is %d",sum);
}
Pointers and Structures:
11.Using Dot Operator:
Example:
struct addstruct //structure declaration
{
int number1,number2;
};
void main()
{
struct addstruct *pointer,number3;
int sum;
pointer=&number3; //assigning location of the structure
variable to the pointer variable
(*pointer).number1=20;
(*pointer).number2=10;
sum=(*pointer).number1+(*pointer).number2;
printf("\nSum of two numbers using pointers and
structures(using dot operator) is %d",sum);
}
12.Using Arrow Operator:
Example:
struct addstruct
{
int number1,number2;
};
void main()
{
struct addstruct *pointer,number3;
int sum;
pointer=&number3;
pointer->number1=20; //assigning data to the structure
pointer->number2=10; variable
sum=pointer->number1+pointer->number2;
printf("\nSum of two numbers using pointers and
structures(using arrow operator) is %d",sum);
}
13.Using Function Method 1
(Passing Structure by Value):
Example:
struct addstruct
{
int number1,number2;
};
void addstr(struct addstruct strnumber);
void main()
{
struct addstruct struct1;
struct1.number1=10;
struct1.number2=20;
addstr(struct1);
}
void addstr(struct addstruct strnumber)
{
int sum=strnumber.number1+strnumber.number2;
printf("Sum of two numbers using function method1(passing
methods by value) is %d",sum);
}
14.Using Function Method 2
(Passing Structure by Reference):
Example:
struct addstruct
{
int number;
};
void add(struct addstruct num1,struct addstruct num2,struct
addstruct *pointer1);
void main()
{
struct addstruct number1,number2,sum;
number1.number=10;
number2.number=20;
add(number1,number2,&sum);
printf("\n Sum of two numbers using function method2(passing
structure by reference) is %d",sum.number);
}
void add(struct addstruct num1,struct addstruct num2,struct
addstruct *pointer1)
{
pointer1->number=num1.number+num2.number;
}
15.Union:
Example:
union addunion //union declaration
{
int number1,number2;
}objunion; //union object
void main()
{
int sum;
objunion.number1=15; //assigning data using union object
objunion.number2=15;
sum=objunion.number1+objunion.number2;
printf("\nSum of two numbers using union is %d",sum);
}
16.Preprocessor:
Example:
#define number1 20 //symbolic constant
# define number2 10
void main()
{
int sum=number1+number2;
printf("\nSum of two numbers using preprocessor is %d",sum);
}
17.Enumerated Data Type (or) User Defined Data type:
Example:
void main()
{
int sum;
enum numbers{number1=10,number2=20};
enum number num1; //creating variables for the
enum number num2; enumerated data type
num1=number1;
num2=number2;
sum=num1+num2;
printf("\nSum of two numbers using enumerated data type is
%d",sum);
}

Submitted by:

S.Gavaskar,
Ast.Professor Dept of CSE
&
Preethi T
Roll No:101071
Final year student,
Dept of CSE

You might also like