This Program Takes A String From User And For Loop Executed Until All Characters Of String Is Checked. If Any Character Inside A String Is Not A Alphabet, All Characters After It Including Null Character Is Shifted By 1 Position Backwards.
C Programming Structure
Structure Is The Collection Of Variables Of Different Types Under A Single Name For Better Handling. For Example: You Want To Store The Information About Person About His/Her Name, Citizenship Number And Salary. You Can Create These Information Separately But, Better Approach Will Be Collection Of These Information Under Single Name Because All These Information Are Related To Person.
Structure Definition In C
Keyword Struct Is Used For Creating A Structure.
Syntax Of Structure
Struct Structure_Name
{
Data_Type Member1;
Data_Type Member2;
.
.
Data_Type Memeber;
};
We Can Create The Structure For A Person As Mentioned Above As:
Struct Person
{
Char Name[50];
Int Cit_No;
Float Salary;
};
This Declaration Above Creates The Derived Data Type Struct Person.
Structure Variable Declaration
When A Structure Is Defined, It Creates A User-Defined Type But, No Storage Is Allocated. For The Above Structure Of Person, Variable Can Be Declared As:
Struct Person
{
Char Name[50];
Int Cit_No;
Float Salary;
};
Inside Main Function:
Struct Person P1, P2, P[20];
Another Way Of Creating Sturcture Variable Is:
Struct Person
{
Char Name[50];
Int Cit_No;
Float Salary;
}P1 ,P2 ,P[20];
In Both Cases, 2 Variables P1, P2 And Array P Having 20 Elements Of Type Struct Person Are Created.
Accessing Members Of A Structure
There Are Two Types Of Operators Used For Accessing Members Of A Structure.
1. Member Operator(.)
2. Structure Pointer Operator(->) (Will Be Discussed In Structure And Pointers Chapter)
Any Member Of A Structure Can Be Accessed As: Structure_Variable_Name.Member_Name
Suppose, We Want To Access Salary For Variable P2. Then, It Can Be Accessed As:
P2.Salary
Example Of Structure
Write A C Program To Add Two Distances Entered By User. Measurement Of Distance Should Be In Inch And Feet.(Note: 12 Inches = 1 Foot)
#include<stdio.h>
#include<conio.h>
Struct Distance{
Int Feet;
Float Inch;
}D1,D2,Sum;
Int Main(){
Printf("1st Distance\N");
Printf("Enter Feet: ");
Scanf("%D",&D1.Feet); /* Input Of Feet For Structure Variable D1 */
Printf("Enter Inch: ");
Scanf("%F",&D1.Inch); /* Input Of Inch For Structure Variable D1 */
Printf("2nd Distance\N");
Printf("Enter Feet: ");
Scanf("%D",&D2.Feet); /* Input Of Feet For Structure Variable D2 */
Printf("Enter Inch: ");
Scanf("%F",&D2.Inch); /* Input Of Inch For Structure Variable D2 */
Sum.Feet=D1.Feet+D2.Feet;
Sum.Inch=D1.Inch+D2.Inch;
If (Sum.Inch>12){ //If Inch Is Greater Than 12, Changing It To Feet.
++Sum.Feet;
Sum.Inch=Sum.Inch-12;
}
Printf("Sum Of Distances=%D\'-%.1f\"",Sum.Feet,Sum.Inch);
/* Printing Sum Of Distance D1 And D2 */
Return 0;
}
Output
1st Distance
Enter Feet: 12
Enter Inch: 7.9
2nd Distance
Enter Feet: 2
Enter Inch: 9.8
Sum Of Distances= 15'-5.7"
Keyword Typedef While Using Structure
Programmer Generally Use Typedef While Using Structure In C Language. For Example:
Typedef Struct Complex{
Int Imag;
Float Real;
}Comp;
Inside Main:
Comp C1,C2;
Here, Typedef Keyword Is Used In Creating A Type Comp(Which Is Of Type As Struct Complex). Then, Two Structure Variables C1 And C2 Are Created By This Comp Type.
Structures Within Structures
Structures Can Be Nested Within Other Structures In C Programming.
Struct Complex
{
Int Imag_Value;
Float Real_Value;
};
Struct Number{
Struct Complex C1;
Int Real;
}N1,N2;
Suppose You Want To Access Imag_Value For N2 Structure Variable Then, Structure Member N1.C1.Imag_Value Is Used.
C Programming Structure And Pointer
Pointers Can Be Accessed Along With Structures. A Pointer Variable Of Structure Can Be Created As Below:
Struct Name {
Member1;
Member2;
.
.
};
-------- Inside Function -------
Struct Name *Ptr;
Here, The Pointer Variable Of Type Struct Name Is Created.
Structure's Member Through Pointer Can Be Used In Two Ways:
1. Referencing Pointer To Another Address To Access Memory
2. Using Dynamic Memory Allocation
Consider An Example To Access Structure's Member Through Pointer.
#include<stdio.h>
#include<conio.h>
Struct Name{
Int A;
Float B;
};
Int Main(){
Struct Name *Ptr,P;
Ptr=&P; /* Referencing Pointer To Memory Address Of P */
Printf("Enter Integer: ");
Scanf("%D",&(*Ptr).A);
Printf("Enter Number: ");
Scanf("%F",&(*Ptr).B);
Printf("Displaying: ");
Printf("%D%F",(*Ptr).A,(*Ptr).B);
Return 0;
}
In This Example, The Pointer Variable Of Type Struct Name Is Referenced To The Address Of P. Then, Only The Structure Member Through Pointer Can Can Accessed.
Structure Pointer Member Can Also Be Accessed Using -> Operator.
(*Ptr).A Is Same As Ptr->A
(*Ptr).B Is Same As Ptr->B
Accessing Structure Member Through Pointer Using Dynamic Memory Allocation
To Access Structure Member Using Pointers, Memory Can Be Allocated Dynamically Using Malloc() Function Defined Under "Stdlib.H" Header File.
Syntax To Use Malloc()
Ptr=(Cast-Type*)Malloc(Byte-Size)
Example To Use Structure's Member Through Pointer Using Malloc() Function.
#include<stdio.h>
#include<conio.h>
#Include<Stdlib.H>
Struct Name {
Int A;
Float B;
Char C[30];
};
Int Main(){
Struct Name *Ptr;
Int I,N;
Printf("Enter N: ");
Scanf("%D",&N);
Ptr=(Struct Name*)Malloc(N*Sizeof(Struct Name));
/* Above Statement Allocates The Memory For N Structures With Pointer Ptr Pointing To Base Address */
For(I=0;IC,&(Ptr+I)->A,&(Ptr+I)->B);
}
Printf("Displaying Infromation:\N");
For(I=0;IC,(Ptr+I)->A,(Ptr+I)->B);
Return 0;
}
Output
Enter N: 2
Enter String, Integer And Floating Number Respectively:
Programming
2
3.2
Enter String, Integer And Floating Number Respectively:
Structure
6
2.3
Displaying Information
Programming 2 3.20
Structure 6 2.30
C Programming Structure And Function
In C, Structure Can Be Passed To Functions By Two Methods:
1. Passing By Value (Passing Actual Value As Argument)
2. Passing By Reference (Passing Address Of An Argument)
Passing Structure By Value
A Structure Variable Can Be Passed To The Function As An Argument As Normal Variable. If Structure Is Passed By Value, Change Made In Structure Variable In Function Definition Does Not Reflect In Original Structure Variable In Calling Function.
Write A C Program To Create A Structure Student, Containing Name And Roll. Ask User The Name And Roll Of A Student In Main Function. Pass This Structure To A Function And Display The Information In That Function.
#include<stdio.h>
#include<conio.h>
Struct Student{
Char Name[50];
Int Roll;
};
Void Display(Struct Student Stu);
/* Function Prototype Should Be Below To The Structure Declaration Otherwise Compiler Shows Error */
Int Main(){
Struct Student S1;
Printf("Enter Student's Name: ");
Scanf("%S",&S1.Name);
Printf("Enter Roll Number:");
Scanf("%D",&S1.Roll);
Display(S1); // Passing Structure Variable S1 As Argument
Return 0;
}
Void Display(Struct Student Stu){
Printf("Output\Nname: %S",Stu.Name);
Printf("\Nroll: %D",Stu.Roll);
}
Output
Enter Student's Name: Kevin Amla
Enter Roll Number: 149
Output
Name: Kevin Amla
Roll: 149
Passing Structure By Reference
The Address Location Of Structure Variable Is Passed To Function While Passing It By Reference. If Structure Is Passed By Reference, Change Made In Structure Variable In Function Definition Reflects In Original Structure Variable In The Calling Function.
Write A C Program To Add Two Distances(Feet-Inch System) Entered By User. To Solve This Program, Make A Structure. Pass Two Structure Variable (Containing Distance In Feet And Inch) To Add Function By Reference And Display The Result In Main Function Without Returning It.
#include<stdio.h>
#include<conio.h>
Struct Distance{
Int Feet;
Float Inch;
};
Void Add(Struct Distance D1,Struct Distance D2, Struct Distance *D3);
Int Main()
{
Struct Distance Dist1, Dist2, Dist3;
Printf("First Distance\N");
Printf("Enter Feet: ");
Scanf("%D",&Dist1.Feet);
Printf("Enter Inch: ");
Scanf("%F",&Dist1.Inch);
Printf("Second Distance\N");
Printf("Enter Feet: ");
Scanf("%D",&Dist2.Feet);
Printf("Enter Inch: ");
Scanf("%F",&Dist2.Inch);
Add(Dist1, Dist2, &Dist3);
/*Passing Structure Variables Dist1 And Dist2 By Value Whereas Passing Structure Variable Dist3 By Reference */
Printf("\Nsum Of Distances = %D\'-%.1f\"",Dist3.Feet, Dist3.Inch);
Return 0;
}
Void Add(Struct Distance D1,Struct Distance D2, Struct Distance *D3)
{
/* Adding Distances D1 And D2 And Storing It In D3 */
D3->Feet=D1.Feet+D2.Feet;
D3->Inch=D1.Inch+D2.Inch;
If (D3->Inch>=12) { /* If Inch Is Greater Or Equal To 12, Converting It To Feet. */
D3->Inch-=12;
++D3->Feet;
}
}
Output
First Distance
Enter Feet: 12
Enter Inch: 6.8
Second Distance
Enter Feet: 5
Enter Inch: 7.5
Sum Of Distances = 18'-2.3"
Explaination
In This Program, Structure Variables Dist1 And Dist2 Are Passed By Value (Because Value Of Dist1 And Dist2 Does Not Need To Be Displayed In Main Function) And Dist3 Is Passed By Reference ,I.E, Address Of Dist3 (&Dist3) Is Passed As An Argument. Thus, The Structure Pointer Variable D3 Points To The Address Of Dist3. If Any Change Is Made In D3variable, Effect Of It Is Seed In Dist3 Variable In Main Function.
C Programming Unions
Unions Are Quite Similar To The Structures In C. Union Is Also A Derived Type As Structure. Union Can Be Defined In Same Manner As Structures Just The Keyword Used In Defining Union In Union Where Keyword Used In Defining Structure Was Struct.
Union Car{
Char Name[50];
Int Price;
};
Union Variables Can Be Created In Similar Manner As Structure Variable.
Union Car{
Char Name[50];
Int Price;
}C1, C2, *C3;
Or;
Union Car{
Char Name[50];
Int Price;
};
-------Inside Function-----------
Union Car C1, C2, *C3;
In Both Cases, Union Variables C1, C2 And Union Pointer Variable C3 Of Type Union Car Is Created.
Accessing Members Of An Union
The Member Of Unions Can Be Accessed In Similar Manner As That Structure. Suppose, We You Want To Access Price For Union Variable C1 In Above Example, It Can Be Accessed As C1.Price. If You Want To Access Price For Union Pointer Variable C3, It Can Be Accessed As (*C3).Price Or As C3->Price.
Difference Between Union And Structure
Though Unions Are Similar To Structure In So Many Ways, The Difference Between Them Is Crucial To Understand. This Can Be Demonstrated By This Example:
#include<stdio.h>
#include<conio.h>
Union Job { //Defining A Union
Char Name[32];
Float Salary;
Int Worker_No;
}U;
Struct Job1 {
Char Name[32];
Float Salary;
Int Worker_No;
}S;
Int Main(){
Printf("Size Of Union = %D",Sizeof(U));
Printf("\Nsize Of Structure = %D", Sizeof(S));
Return 0;
}
Output
Size Of Union = 32
Size Of Structure = 40
There Is Difference In Memory Allocation Between Union And Structure As Suggested In Above Example. The Amount Of Memory Required To Store A Structure Variables Is The Sum Of Memory Size Of All Members.
But, The Memory Required To Store A Union Variable Is The Memory Required For Largest Element Of An Union.
What Difference Does It Make Between Structure And Union?
As You Know, All Members Of Structure Can Be Accessed At Any Time. But, Only One Member Of Union Can Be Accessed At A Time In Case Of Union And Other Members Will Contain Garbage Value.
#include<stdio.h>
#include<conio.h>
Union Job {
Char Name[32];
Float Salary;
Int Worker_No;
}U;
Int Main(){
Printf("Enter Name:\N");
Scanf("%S",&U.Name);
Printf("Enter Salary: \N");
Scanf("%F",&U.Salary);
Printf("Displaying\Nname :%S\N",U.Name);
Printf("Salary: %.1f",U.Salary);
Return 0;
}
Output
Enter Name
Hillary
Enter Salary
1234.23
Displaying
Name: F%Bary
Salary: 1234.2
Note: You May Get Different Garbage Value Of Name.
Why This Output?
Initially, Hillary Will Be Stored In U.Name And Other Members Of Union Will Contain Garbage Value. But When User Enters Value Of Salary, 1234.23 Will Be Stored In U.Salary And Other Members Will Contain Garbage Value. Thus In Output, Salary Is Printed Accurately But, Name Displays Some Random String.
Passing Union To A Function
Union Can Be Passed In Similar Manner As Structures In C Programming. Visit This Page To Learn More About: How Structure Can Be Passed To Function In C Programming?
C Program To Store Information(Name, Roll And Marks) Of A Student Using Structure
In This Program, A Structure(Student) Is Created Which Contains Name, Roll And Marks As Its Data Member. Then, A Structure Variable( S ) Is Created. Then, Data (Name, Roll And Marks) Is Taken From User And Stored In Data Members Of Structure Variable S. Finally, The Data Entered By User Is Displayed.
C Program To Store Information Of Single Variable
#include<stdio.h>
#include<conio.h>
Struct Student{
Char Name[50];
Int Roll;
Float Marks;
};
Int Main(){
Struct Student S;
Printf("Enter Information Of Students:\N\N");
Printf("Enter Name: ");
Scanf("%S",S.Name);
Printf("Enter Roll Number: ");
Scanf("%D",&S.Roll);
Printf("Enter Marks: ");
Scanf("%F",&S.Marks);
Printf("\Ndisplaying Information\N");
Printf("Name: %S\N",S.Name);
Printf("Roll: %D\N",S.Roll);
Printf("Marks: %.2f\N",S.Marks);
Return 0;
}
Output
C Program To Store Information Of 10 Students Using Structure
In This Program, A Structure(Student) Is Created Which Contains Name, Roll And Marks As Its Data Member. Then, An Array Of Structure Of 10 Elements Is Created. Then, Data(Name, Roll And Marks) For 10 Elements Is Asked To User And Stored In Array Of Structure. Finally, The Data Entered By User Is Displayed.
Source Code To Store Information Of 10 Students Using Structure
#include<stdio.h>
#include<conio.h>
Struct Student{
Char Name[50];
Int Roll;
Float Marks;
};
Int Main(){
Struct Student S[10];
Int I;
Printf("Enter Information Of Students:\N");
For(I=0;I<10;++I)
{
S[I].Roll=I+1;
Printf("\Nfor Roll Number %D\N",S[I].Roll);
Printf("Enter Name: ");
Scanf("%S",S[I].Name);
Printf("Enter Marks: ");
Scanf("%F",&S[I].Marks);
Printf("\N");
}
Printf("Displaying Information Of Students:\N\N");
For(I=0;I<10;++I)
{
Printf("\Ninformation For Roll Number %D:\N",I+1);
Printf("Name: ");
Puts(S[I].Name);
Printf("Marks: %.1f",S[I].Marks);
}
Return 0;
}
Output
Enter Information Of Students:
For Roll Number 1
Enter Name: Tom
Enter Marks: 98
For Roll Number 2
Enter Name: Jerry
Enter Marks: 89
.
.
.
Displaying Information Of Students:
Information For Roll Number 1:
Name: Tom
Marks: 98
.
.
.
C Program To Store Information Using Structures For N Elements Dynamically
This Program Asks User To Store The Value Of N And Allocates The Memory For The N Structure Variable Dynamically Using Malloc() Function.
Source Code Demonstrate The Dynamic Memory Allocation For Structure
#include<stdio.h>
#include<conio.h>
#Include<Stdlib.H>
Struct Name {
Int A;
Char C[30];
};
Int Main(){
Struct Name *Ptr;
Int I,N;
Printf("Enter N: ");
Scanf("%D",&N);
/* Allocates The Memory For N Structures With Pointer Ptr Pointing To The Base Address. */
Ptr=(Struct Name*)Malloc(N*Sizeof(Struct Name));
For(I=0;IC, &(Ptr+I)->A);
}
Printf("Displaying Infromation:\N");
For(I=0;IC,(Ptr+I)->A);
Return 0;
}
Output
Enter N: 2
Enter String And Integer Respectively:
Programming
22
Enter String, Integer And Floating Number Respectively:
Structure
33
Displaying Information:
Programming 22
Structure 33
C Programming Files
In C Programming, File Is A Place On Disk Where A Group Of Related Data Is Stored.
Why Files Are Needed?
When The Program Is Terminated, The Entire Data Is Lost In C Programming. If You Want To Keep Large Volume Of Data, It Is Time Consuming To Enter The Entire Data. But, If File Is Created, These Information Can Be Accessed Using Few Commands.
There Are Large Numbers Of Functions To Handle File I/O In C Language. In This Tutorial, You Will Learn To Handle Standard I/O(High Level File I/O Functions) In C.
High Level File I/O Functions Can Be Categorized As:
1. Text File
2. Binary File
File Operations
1. Creating A New File
2. Opening An Existing File
3. Reading From And Writing Information To A File
4. Closing A File
Working With File
While Working With File, You Need To Declare A Pointer Of Type File. This Declaration Is Needed For Communication Between File And Program.
File *Ptr;
Opening A File
Opening A File Is Performed Using Library Function Fopen(). The Syntax For Opening A File In Standard I/O Is:
Ptr=Fopen("Fileopen","Mode")
For Example:
Fopen("E:\\Cprogram\Program.Txt","W");
/* --------------------------------------------------------- */
E:\\Cprogram\Program.Txt Is The Location To Create File.
"W" Represents The Mode For Writing.
/* --------------------------------------------------------- */
Here, The Program.Txt File Is Opened For Writing Mode.
Opening Modes In Standard I/O
File Mode Meaning Of Mode During Inexistence Of File
R Open For Reading. If The File Does Not Exist, Fopen() Returns Null.
W Open For Writing. If The File Exists, Its Contents Are Overwritten. If The File Does Not Exist, It Will Be Created.
A Open For Append. I.E, Data Is Added To End Of File. If The File Does Not Exists, It Will Be Created.
R+ Open For Both Reading And Writing. If The File Does Not Exist, Fopen() Returns Null.
W+ Open For Both Reading And Writing. If The File Exists, Its Contents Are Overwritten. If The File Does Not Exist, It Will Be Created.
A+ Open For Both Reading And Appending. If The File Does Not Exists, It Will Be Created.
Closing A File
The File Should Be Closed After Reading/Writing Of A File. Closing A File Is Performed Using Library Function Fclose().
Fclose(Ptr); //Ptr Is The File Pointer Associated With File To Be Closed.
The Functions Fprintf() And Fscanf() Functions.
The Functions Fprintf() And Fscanf() Are The File Version Of Printf() And Fscanf(). The Only Difference While Using Fprintf()And Fscanf() Is That, The First Argument Is A Pointer To The Structure File
Writing To A File
#include <stdio.h>
#include <conio.h>
Int Main()
{
Int N;
File *Fptr;
Fptr=Fopen("C:\\Program.Txt","W");
If(Fptr==Null){
Printf("Error!");
Exit(1);
}
Printf("Enter N: ");
Scanf("%D",&N);
Fprintf(Fptr,"%D",N);
Fclose(Fptr);
Return 0;
}
This Program Takes The Number From User And Stores In File. After You Compile And Run This Program, You Can See A Text File Program.Txt Created In C Drive Of Your Computer. When You Open That File, You Can See The Integer You Entered.
Similarly, Fscanf() Can Be Used To Read Data From File.
Reading From File
#include <stdio.h>
#include <conio.h>
Int Main()
{
Int N;
File *Fptr;
If ((Fptr=Fopen("C:\\Program.Txt","R"))==Null){
Printf("Error! Opening File");
Exit(1); /* Program Exits If File Pointer Returns Null. */
}
Fscanf(Fptr,"%D",&N);
Printf("Value Of N=%D",N);
Fclose(Fptr);
Return 0;
}
If You Have Run Program Above To Write In File Successfully, You Can Get The Integer Back Entered In That Program Using This Program.
Other Functions Like Fgetchar(), Fputc() Etc. Can Be Used In Similar Way.
Binary Files
Depending Upon The Way File Is Opened For Processing, A File Is Classified Into Text File And Binary File.
If A Large Amount Of Numerical Data It To Be Stored, Text Mode Will Be Insufficient. In Such Case Binary File Is Used.
Working Of Binary Files Is Similar To Text Files With Few Differences In Opening Modes, Reading From File And Writing To File.
Opening Modes Of Binary Files
Opening Modes Of Binary Files Are Rb, Rb+, Wb, Wb+,Ab And Ab+. The Only Difference Between Opening Modes Of Text And Binary Files Is That, B Is Appended To Indicate That, It Is Binary File.
Reading And Writing Of A Binary File.
Functions Fread() And Fwrite() Are Used For Reading From And Writing To A File On The Disk Respectively In Case Of Binary Files.
Function Fwrite() Takes Four Arguments, Address Of Data To Be Written In Disk, Size Of Data To Be Written In Disk, Number Of Such Type Of Data And Pointer To The File Where You Want To Write.
Fwrite(Address_Data,Size_Data,Numbers_Data,Pointer_To_File);
Function Fread() Also Take 4 Arguments Similar To Fwrite() Function As Above.
Write A C Program To Read Name And Marks Of N Number Of Students From User And Store Them In A File
#include <stdio.h>
#include <conio.h>
Int Main(){
Char Name[50];
Int Marks,I,N;
Printf("Enter Number Of Students: ");
Scanf("%D",&N);
File *Fptr;
Fptr=(Fopen("C:\\Student.Txt","W"));
If(Fptr==Null){
Printf("Error!");
Exit(1);
}
For(I=0;I
Here, "Stdio.H" Is A Header File And The Preprocessor Replace The Above Line With The Contents Of Header File.
Use Of #Define
Preprocessing Directive #Define Has Two Forms. The First Form Is:
#Define Identifier Token_String
Token_String Part Is Optional But, Are Used Almost Every Time In Program.
Example Of #Define
#Define C 299792458 /*Speed Of Light In M/S */
The Token String In Above Line 2299792458 Is Replaced In Every Occurance Of Symbolic Constant C.
C Program To Find Area Of A Cricle. [Area Of Circle=Πr2]
#include <stdio.h>
#include <conio.h>
#Define Pi 3.1415
Int Main(){
Int Radius;
Float Area;
Printf("Enter The Radius: ");
Scanf("%D",&Radius);
Area=Pi*Radius*Radius;
Printf("Area=%.2f",Area);
Return 0;
}
Output
Enter The Radius: 3
Area=28.27
Syntactic Sugar
Syntactic Sugar Is The Alteration Of Programming Syntax According To The Will Of Programmer. For Example:
#Define Lt <
Every Time The Program Encounters Lt, It Will Be Replaced By <.
Second Form Of Preprocessing Directive With #Define Is:
Macros With Argument
Preprocessing Directive #Define Can Be Used To Write Macro Definitions With Parameters As Well In The Form Below:
#Define Identifier(Identifier 1,.....Identifier N) Token_String
Again, The Token String Is Optional But, Are Used In Almost Every Case. Let Us Consider An Example Of Macro Definition With Argument.
#Define Area(R) (3.1415*R*R)
Here, The Argument Passed Is R. Every Time The Program Encounters Area(Argument), It Will Be Replace By(3.1415*Argument*Argument). Suppose, We Passed (R1+5) As Argument Then, It Expands As Below:
Area(R1+5) Expands To (3.1415*(R1+5)(R1+5))
C Program To Find Area Of A Circle, Passing Arguments To Macros. [Area Of Circle=Πr2]
#Include
#Define Pi 3.1415
#Define Area(R) (Pi*R*R)
Int Main(){
Int Radius;
Float Area;
Printf("Enter The Radius: ");
Scanf("%D",&Radius);
Area=Area(Radius);
Printf("Area=%.2f",Area);
Return 0;
}
Predefined Macros In C Language
Predefined Macro Value
__Date__ String Containing The Current Date
__File__ String Containing The File Name
__Line__ Integer Representing The Current Line Number
__Stdc__ If Follows Ansi Standard C, Then Value Is A Nonzero Integer
__Time__ String Containing The Current Date.
How To Use Predefined Macros?
C Program To Find The Current Time
#Include
Int Main(){
Printf("Current Time: %S",__Time__); //Calculate The Current Time
}
Output
Current Time: 19:54:39
C Program To Demonstrate The Working Of Keyword Long
Keyword Long Is Used For Altering The Size Of Data Type. For Example: The Size Of Int Is Either 2 Bytes Or 4 Bytes But, When Long Keyword Is Used, The Size Of Long Int Will Be Either 4 Bytes Or 8 Bytes. Also, You Can Use Long Long Int. The Size Of Long Long Int Is Generally 8 Bytes. This Program Will Demonstrate The Size Of Keyword Long For My System. It May Be Different In Your System.
Source Code
#include <stdio.h>
#include <conio.h>
Int Main(){
Int A;
Long Int B; /* Int Is Optional. */
Long Long Int C; /* Int Is Optional. */
Printf("Size Of Int = %D Bytes\N",Sizeof(A));
Printf("Size Of Long Int = %Ld Bytes\N",Sizeof(B));
Printf("Size Of Long Long Int = %Ld Bytes",Sizeof(C));
Return 0;
}
Output
Size Of Int = 4 Bytes
Size Of Long Int = 4 Bytes
Size Of Long Long Int = 8 Bytes
In This Program, The Sizeof Operator Is Used For Finding The Size Of Int, Long Int And Long Long Int.
Thus, Int And Long Int For My System Can Hold Values From -231 To 231-1. If I Have To Work On Data Outside This Range, I Have To Use Long Long Int, Which Can Hold Values From -263 To 263-1 .
Similarly, The Long Keyword Can Be Used Double And Floats Types.
Command Line Arguments : - C Programming Tutorial
Command-line arguments are basically arguments sent to the program being called & are given after the name of a program in command-line operating systems and are basically passed in to the program from the operating system.
When the function main() is called then
arc :gives number of command line arguments
argv: full list of command line arguments, or array of arguments
Input file for command line argument
After writing the c program above save the file as CLA.C .
Now go to command prompt and type
CLA arg1 arg2
Output file for command line arguments
Here i entered my name and it prints in this way i.e to the next line.
Similarly, you can enter other arguments as shown below.
A program can take any number of command line arguments, which may be required for the program to run.
No comments:
Post a Comment