Upload
mayank-abhishek
View
1.282
Download
3
Tags:
Embed Size (px)
DESCRIPTION
CP01: Basics of C Programming Lecture
Citation preview
C Basics
Mayank Abhishek
Basically C
You need a text editor to write a C ProgramEg: Notepad, VI
You need a compiler to compile your program to produce an executable file.Eg: TurboC, GCC
The version of C used in our college is ANSI C Compilers for ANSI C are Microsoft Visual C++,
GCC etc.
A Sample C Program
#include <stdio.h> int main(void){ printf("Hello, World!\n"); return (0);}
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
Comments
Comments are beauty marks for a program
// This is a single line comment
/* This is a multiline comment */
Multiline Comments cannot be nested
Incorrect Usage of Comment
/* This is an incorrect usage of a comment
/* Hello This Comment Is Wrong */
*/
Queer Comments
/*
// Hello This Comment Is Queer
*/
// Hello //This //Comment //Is //Queer
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
A C Program01: // Program to calculate the product of two numbers. 02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: // Input the first number 11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: // Input the second number 15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: // Calculate and display the product 19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: // Function returns the product of its two arguments 26: int product(int x, int y)27: {28: return (x * y);29: }
A Wrong C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number *//*11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
A Wrong C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number *//*11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
Preprocessor Directives
The C preprocessor (cpp) is the preprocessor for the C programming language.
The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).
#include
#include <stdio.h>
#include ”myfile.h”
#define
#define PI 3.14159
#define SQR(x) ((x) * (x))
Be Careful with Macros!
#define SQR(x) (x * x)
Replacements:
SQR(5) : 5 * 5 = 25
SQR(A) : A * A = A2
SQR(5+5) : 5 + 5 * 5 + 5 = 35
Variables
int a; // a = 5
int a, b; // a = 5, b= 10
float a; // a = 12.18
double a; // a = 12.18
char a; // a = 'Z'
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
Suggest A Variable Type for:
Roll Number Grade obtained in a course Area of a circle Value of PI
Integer Airthmetic
5 + 5 = 10
10 / 5 = 2
9 / 5 = 1
23 / 7 = 3
Find the value of:
24 / 7 * 2 + 11 / 2
Find the value of:
24 / 7 * 2 + 11 / 23 * 2 + 5
6 + 511
Functions
26: int product(int x, int y)27: {28: return (x * y);29: }
Functions
26: int product(int x, int y)27: {28: return (x * y);29: }
A C Program01: /* Program to calculate the product of two numbers. */02: #include <stdio.h>03:04: int a,b,c;05:06: int product(int x, int y);07:08: int main()09: {10: /* Input the first number */11: printf("Enter a number between 1 and 100: ");12: scanf("%d", &a);13:14: /* Input the second number */15: printf("Enter another number between 1 and 100: ");16: scanf("%d", &b);17:18: /* Calculate and display the product */19: c = product(a, b);20: printf ("%d times %d = %d\n", a, b, c);21:22: return 0;23: }24:25: /* Function returns the product of its two arguments */26: int product(int x, int y)27: {28: return (x * y);29: }
printf
printf(format string, print list);
printf(format string);
Eg: printf(“The result is %lf ”, kms);
scanf
scanf (format string, input list);
Eg: scanf (“%c%d”,&letter, &age);
New C Program
01: /* Computes and Displays the area and circumference of a circle*/02: #include <stdio.h>03: #define PI 3.1415904: 05: int main ( void )06: {07: double radius, area, circum;08: printf (“Enter the radius of the circle : ”);09: scanf (“%lf”, &radius);10: /* Calculate the area */11: area = PI * radius * radius;12: /* Calculate the circumference*/13: circum = 2 * PI * radius;14: printf(“ \n Area is: %f \n Circumference: %f”, area, circum);15: return (0);16: }
Arithmetic Operators
5 + 5 = 10
3 2 = 1
10 / 5 = 2
9 * 5 = 45
23 % 7 = 2
++a or a++ : a = a + 1
a or a : a = a 1
Relational Operators
A > B
A < B
A == B
A != B
A >= B
A <= B
Boolean Operators
NOT : !A
OR : A || B
AND : A && B
Another New C Program
01: /* Finds if a person can vote or not */02: #include <stdio.h>03: #define VOTING_AGE 1804: 05: int main ( void )06: {07: int age;08: printf (“Enter the age : ”);09: scanf (“%d”, &age);10: if (age > VOTING_AGE)11: printf (”Person can vote.”);12: else13: printf (”Person cannot vote.”);14: 15: return (0);16: }
Another New C Program
01: /* Finds if a person can vote or not */02: #include <stdio.h>03: #define VOTING_AGE 1804: 05: int main ( void )06: {07: int age;08: printf (“Enter the age : ”);09: scanf (“%d”, &age);10: if (age >= VOTING_AGE)11: printf (”Person can vote.”);12: else13: printf (”Person cannot vote.”);14: 15: return (0);16: }
Another New C Program
01: /* Finds the largest of three numbers */02: #include <stdio.h>03: 04: 05: int main ( void )06: {07: int a, b , c;08: printf (“Enter the values of a, b, c : ”);09: scanf (“%d %d %d”, &a, &b, &c);10: if (a >= b && a >= c) printf(”%d is largest”, a);11: if (b >= a && b >= c) printf(”%d is largest”, b);12: if (c >= a && c >= b) printf(”%d is largest”, c);13: return (0);14: }
Another New C Program
01: /* Finds the largest of three numbers */02: #include <stdio.h>03: 04: 05: int main ( void )06: {07: int a, b , c;08: printf (“Enter the values of a, b, c : ”);09: scanf (“%d %d %d”, &a, &b, &c);10: if (a >= b)11: if (a >= c) printf(”%d is largest”, a);12: else printf(”%d is largest”, c);13: else if (b >= c) printf(”%d is largest”, b);14: else printf(”%d is largest”, c);15: return (0);16: }
Another New C Program
01: /* Finds the largest of three numbers */02: #include <stdio.h>03: 04: 05: int main ( void )06: {07: int a, b , c;08: printf (“Enter the values of a, b, c : ”);09: scanf (“%d %d %d”, &a, &b, &c);10: if (a >= b)11: {12: if (a >= c) printf(”%d is largest”, a);13: else printf(”%d is largest”, c); 14: }15: else16: {17: if (b >= c) printf(”%d is largest”, b);18: else printf(”%d is largest”, c);19: }20: return (0);21: }