11
//My First C Program #include <stdio.h> int main() { printf("Talk To a Teacher\n"); return 0; } //My First C++ program #include <iostream> using namespace std; int main() { cout <<"Talk To a Teacher\n"; return 0; } #include <stdio.h> int main() { int a = 2; double const b = 4; float c = 1.5; char d = 'A'; printf("The value of a is %d\n",a); printf("The value of b is %lf\n",b); printf("The value of c is %.2f\n",c); printf("The value of d is %c\n",d); return 0; } #include <stdio.h> int main() { int a = 2; double const b = 4; float c = 1.5; char d = 'A'; printf("The value of a is %d\n",a); printf("The value of b is %lf\n",b); printf("The value of c is %.2f\n",c); printf("The value of d is %c\n",d); return 0; } #include <iostream> using namespace std; int main() { int a = 2; double const b = 4; float c = 1.5; char d = 'A'; cout<<"The value of a is "<<a<<"\n"; cout<<"The value of b is "<<b<<"\n"; cout<<"The value of c is "<<c<<"\n"; cout<<"The value of d is "<<d<<"\n"; return 0; } #include <stdio.h> int add(int a, int b) { int c = a + b; printf("Sum of a and b is %d\n",c); } int main() { int sum; sum = add(5,4); return 0; #include <iostream> using namespace std; int add(int a, int b) { int c = a + b; cout<<"Sum of a and b is "<<c<<"\n"; } int main() { int sum; sum = add(5,4); return 0;

Codes for C&CPP Tutorials for Linux

Embed Size (px)

DESCRIPTION

C language important codes

Citation preview

  • //My First C Program#include int main(){ printf("Talk To a Teacher\n"); return 0;}

    //My First C++ program#include using namespace std;int main(){ cout

  • } }

    #include int a=5;int b=2;void add(){int sum;sum = a + b;printf("Sum of a and b is %d\n",sum);}int main(){add();return 0;}

    #include using namespace std;int a=5;int b=2;void add(){int sum;sum = a + b;cout b; sum = a + b; cout

  • #include int main() {int x, y;printf("Enter a number between 0 to 39: ");scanf("%d",&y);

    if(y/10==0){printf("you have entered the number in the range of 0 to 9\n");}else if(y/10==1){printf("you have entered the number in the range of 10 to 19\n");}else if (y/10==2){printf("you have entered number in the range of 20-29\n");}else if (y/10==3){printf("you have entered number in the range of 30-39\n");}else {printf("The number not in range \n");}return 0;}

    #include using namespace std;int main() {int x, y;couty;

    if(y/10==0){cout

  • case 2:printf("you have entered number in the range of 20-29\n");break;case 3:printf("you have entered number in the range of 30-39\n");break;default:printf("number not in range \n");} return 0;}

    break;case 2:cout

  • %.2f\n",a,b,c); c = (float)a / b; printf("Real Division of %d and %d is %.2f\n",a,b,c); return 0;}

    c = a / b; cout

  • } return 0;}

    #includeint main(){int x=0;int y=0;do{y=y+x;printf("%d\n",y);x++;}while(x

  • int main(){int x=0;int y=0;while(x
  • printf("The 3X4 array num2 is\n"); for(i=0; i
  • j = strcmp(str2,"Cream"); printf("%d, %d\n",i,j);

    return 0;}

    printf("target string = %s\n",target);return 0;}

    //String Length

    #include#includeint main(){ char arr[] = "Ashwini"; int len1 = strlen(arr); printf("string = %s length = %d\n",arr, len1);return 0;}

    #include struct student { int eng; int maths; int science;};int main() { int total; struct student stud; stud.eng = 75; stud.maths = 70; stud.science = 65; total = stud.eng + stud.maths + stud.science; printf("Total is %d\n",total); return 0;}

    #include using namespace std;struct student { int eng; int maths; int science;};int main() { int total; struct student stud; stud.eng = 75; stud.maths = 70; stud.science = 65; total = stud.eng + stud.maths + stud.science; cout

  • printf("pointer's value: %p\n", ptr);

    printf("value pointed to: %ld\n", *ptr); }

    sizeof(ptr)

  • { FILE *fp; fp = fopen("/home/ttt/Desktop/sample.txt","w"); fprintf(fp,"Welcome to the spoken-tutorial

    \n"); fprintf(fp,"This is an test example\n"); fclose(fp); return 0;}

    { FILE *fp; char c; fp = fopen("/home/ttt/Desktop/sample.txt","r"); if (fp == NULL) printf("File doesn't exist\n"); else { while (c != EOF) { c = getc(fp); putchar(c); } fclose(fp); }return 0;}