Anda di halaman 1dari 5

c pgmg -----------------source code is saved with extension c need to b compiled; object code will b generated object code can

be executed header files / library files - extension .h - contain utilities for the ease of programmer - stdio.h - conio.h string.h math.h pgmr can use these files in the pgm by giving: #include<stdio.h> - in the beginnning of the pgm pgm can contain comments / remarks - non-executable statements - serve as documents - 2 types: single line: begin with: // multi line: enclosed within: /* pgm should contain void main() -pgm execution will begin here -all statements are given within { } - most of the statements will end with semicolon ; - c pgm is case -sensitive o/p: printf(" ");

*/

printf("Welcome ...\n"); printf("good luck\ntc\tbye");

Welcome ... good luck tc bye escape characters --------------------------\n - new line \t - tab built-in / primitive datatypes: 1. int - whole no 2. float - real nos 3. double - real nos 4. char - to store only one char: useful for gender, marital status etc. variables: - named locations in memory. - can store value; it can change in the pgm - can be of different data types - need to be declared in the beginning of the pgm. - general syntax to declare: datatype variablename; -eg: int a;

int age,salary; float height, weight, mark1, mark2, average; char gender; - varaibles after declaration will have unpredictable / garbage value - variables can be initialized at the time of declaration -eg: int a=899; int age=28,salary=87879; float height=5.61, weight=78.553, mark1=64.4, mark2=69.549, average=78.7 67; char gender='f'; --character constant is enclosed within ' ' i/p: scanf statement - used to take input from keyboard at run time and store the value into a variab le scanf("%d",&age); - takes an int value and stores into variable age %d - int %f - float / double %c - char - while running the program to inform the user as what has to be entered we can give printf before scanf eg: printf("Enter age and salary"); scanf("%d%d",&age,&salary); arithmetic operators: ================ + * / % - remainder /* pgm to understand use of arithmetic operators */ #include<stdio.h> #include<conio.h> void main() { int n1, n2, result; clrscr(); printf("Enter 2 numbers "); scanf("%d%d",&n1, &n2); //adding the nos and storing sum in result variable result=n1+n2; printf("sum=%d",result); //subtracing the nos and storing difference in result variable result=n1-n2; printf("\ndifference=%d",result); //multiplying the nos and storing product in result variable result=n1*n2; printf("\nproduct=%d",result);

//dividing the nos and storing quotient in result variable result=n1/n2; printf("\nquotient=%d",result); //finding the modulous of nos and storing reminder in result variable result=n1%n2; printf("\nreminder=%d",result); getch(); } relational / comparison operators: == equals to != not equals to >greater than < less than >= greater than or equal <= less than or equal - helps to frame condition - work with 2 operands; so they r known as binary operators -- return non-0 if condition is true / satisfied other wise returns 0 conditional branching: -------------------------------------if statement: if(condition) statement; else statement; //pgm to understand if statement: ----------------------------------------------------#include<stdio.h> #include<conio.h> void main() { int n1; clrscr(); printf("Enter a number "); scanf("%d",&n1); if(n1>890) printf("u entered above 890"); else printf("u entered not above 890"); getch(); } //pgm to understand if statement: ----------------------------------------------------#include<stdio.h> #include<conio.h> void main() { int n1; clrscr(); printf("Enter a number "); scanf("%d",&n1); if(n1>890) {

printf("u entered above 890"); printf("\nGood"); } else printf("u entered not above 890"); getch(); } //to accept no check if its even or odd #include<stdio.h> #include<conio.h> void main() { int n1; clrscr(); printf("Enter a number "); scanf("%d",&n1); if(n1 % 2 == 0) printf("%d is even no", n1); else printf("%d is odd no", n1); getch(); } //to accept no check if its even or odd #include<stdio.h> #include<conio.h> void main() { int n1; clrscr(); printf("Enter a number "); scanf("%d",&n1); if(n1 % 2 != 0) printf("%d is odd no", n1); else printf("%d is even no", n1); getch(); } //to accept 2 nos and display biggest #include<stdio.h> #include<conio.h> void main() { int n1, n2, result; clrscr(); printf("Enter 2 numbers "); scanf("%d%d",&n1, &n2); if(n1>n2) printf(" biggest is %d",n1); else printf(" biggest is %d",n2); getch(); } logical operators: ==================== && and or - binary operators - used to combine 2 conditions

n=a>10 && b>10; /*pgm to accept marks in 3 subjects if marks in all subjects is above 50 display pass otherwise display fail */ # # void main() { int a,b,c; clrscr(); printf("enter 3 marks"); scanf("%d%d%d",&a,&b,&c); if(a>50 && b>50 && c>50) printf("Congrats....Passed!"); else printf("Sorry.... Failed! Better Try..."); getch(); } /*pgm to accept marks in 3 subjects if marks in any one subjects is above 50 display pass; if none of the subjects has above 50 display fail */ # # void main() { int a,b,c; clrscr(); printf("enter 3 marks"); scanf("%d%d%d",&a,&b,&c); if(a>50 b>50 c>50) printf("Congrats....Passed!"); else printf("Sorry.... Failed! Better Try..."); getch(); } ======================================================

Anda mungkin juga menyukai