C Language Syntax
Main Function Syntax
#include<stdio.h>
void main()
{
//Your Code goes here
}
Syntax For Printing or Outputting .
printf(“Hello World”);
[Note : Every Line in C Language Ends up with ; ]
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
printf(“Hello World”);
}
Declaring a Variable in C :
<data type> <variable name>;
eg :
int no;
char ch;
float no;
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int no;
no=10;
printf(“The no variable is assigned value %d “, no);
}
Syntax For Scanning the variables :
scanf(“%d”,<variable name>);
[Note : You need to have %d (for integer values), %f (for floating values),
%c (for char values), %s (for String values) | Followed by variable name]
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int no;
printf("Enter any number : Note only number else program will hang
up\n");
scanf(“%d”,&no);
printf(“You have entere the %d “,no);
}
Working with Arithmetic Operators & its Syntax :
There are mainly 5 arithmetic operators. They are
1. +
2. –
3. /
4. *
5. %
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int no;
no=10;
int no1;
no1=20;
int r;
r=no+no1;
printf(“The addition of two number is %d\n”,r);
r=no/no1;
printf(“The divisionof two number is %d\n”,r);
r=no*no1;
printf(“The multiplication of two number is %d\n”,r);
r=no-no1;
printf(“The substration of two number is %d\n”,r);
r=no%no1;
printf(“The modul of two number is %d\n”,r);
}
If Condition Syntax :
if(<condition>)
{
}
else if(<condition>)
{
}
else
{
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int no;
no=10;
if(no<20)
{
printf(“Number is less than 20”);
}
else
{
printf(“Number is less than 20”);
}
}
Declaring Array :
int no[<number>];
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int no[3];
no[0]=1;
no[1]=2;
no[2]=3;
printf(“The number at 2nd position in array is %d “,no[1]);
}
For Loop :
for(i=0; i<5; i++)
{
//your code which u wanna repear 5 times
//For more details log on to www.sharebca.com
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int i=0;
for(i=0; i<5; i++)
{
printf(“This will be displayed 5 times\n”);
}
}
while Loop :
while(i<5)
{
//your code which u wanna repear 5 times
//For more details log on to www.sharebca.com
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int i=0;
while(i<5)
{
printf(“This will be displayed 5 times\n”);
i++;
}
}
Do while Loop :
do
{
//your code which u wanna repear 5 times
}
while(i<5);
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int i=0;
do
{
printf(“This will be displayed 5 times\n”);
i++;
}
while(i<5);
}
The Switch Case :
Switch(i)
{
case 1:
printf(“1\n”);
break;
case 2:
printf(“2\n”);
break;
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int i=1;
switch(i)
{
case 1:
printf("1\n");
break;
case 2:
printf("2\n");
break;
}
}
Pointer Variable :
int *no;
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int a=10;
int *no;
no=&a;
printf("This will print address of the variable where pointer variable is
pointing to that is a's address %d\n",no);
printf("This will print address of the pointer variable no %d\n",&no);
printf("This will print number stored on the variable which is been pointed
bye the pointer no, That is a's value %d\n",*no);
printf("We are simpli printing address of variable a %d\n",&a);
}
Defining Functions :
<Function return type> <function name> (<argument list>)
{
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void show(char c);
void main()
{
show('*');
//For more details log on to www.sharebca.com
}
void show(char c)
{
for(int i=0; i<=4; i++)
{
for(int j=0; j<=i; j++)
{
printf("%c",c);
}
printf("\n");
}
}
Defining Constants :
const int no=10;
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
const int no=10;
void main()
{
printf(“%d”,no);
}
The ?: Operator :
conditional expression ? expression 1 : expression 2
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
(1<0)?printf(“True”): printf(“False”);
}
Goto Startement syntax :
Goto label;
Label:
//statements to be executed
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
void main()
{
int x;
read:
scanf("%d",&x);
if(x<0)
{
goto read;
}
printf("%d",x);
}
Structure syntax :
struct
{
<data type> <variable name>;
<data type> <variable name>;
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
struct product
{
char p_name[50];
int p_date;
float p_price;
char p_supply[50];
}p1,p2,p3;
void main()
{
printf("Enter product 1 detail");
scanf("%s%d%f%s",p1.p_name,&p1.p_date,&p1.p_price,p1.p_supply);
}
Union syntax :
union
{
<data type> <variable name>;
<data type> <variable name>;
}
Eg : ( 100% working just copy and paste it will work)
#include<stdio.h>
union product
{
char p_name[50];
int p_date;
float p_price;
char p_supply[50];
}p1,p2,p3;
void main()
{
printf("Enter product 1 detail");
scanf("%f",&p1.p_price);
printf("%f",p1.p_price);
}
No comments:
Post a Comment
Comment