C language Syntax

14May10

C Language Syntax

  • Main Function Syntax

#include<stdio.h>

void main()

{

//Your Code goes here

//For more details log on to www.sharebca.com

}

  • 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”);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

  • Working with Arithmetic Operators & its  Syntax :

There are mainly 5 arithmetic operators. They are

  1. +
  2. /
  3. *
  4. %

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);

//For more details log on to www.sharebca.com

}

  • 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”);

}

//For more details log on to www.sharebca.com

}

  • 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 more details log on to www.sharebca.com

}

  • 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”);

}

//For more details log on to www.sharebca.com

}

  • 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++;

}

//For more details log on to www.sharebca.com

}

  • Do while Loop :

do

{

//your code which u wanna repear 5 times

//For more details log on to www.sharebca.com

}

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);

//For more details log on to www.sharebca.com

}

  • The Switch Case :

Switch(i)

{

case 1:

printf(“1\n”);

break;

case 2:

printf(“2\n”);

break;

//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=1;

switch(i)

{

case 1:

printf(“1\n”);

break;

case 2:

printf(“2\n”);

break;

//For more details log on to www.sharebca.com

}

}

  • 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);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

  • 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”);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

  • 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);

//For more details log on to www.sharebca.com

}

Advertisement


No Responses Yet to “C language Syntax”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.