CP01: Basics of C Programming

37
C Basics Mayank Abhishek

description

CP01: Basics of C Programming Lecture

Transcript of CP01: Basics of C Programming

Page 1: CP01: Basics of C Programming

C Basics

Mayank Abhishek

Page 2: CP01: Basics of C Programming

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.

Page 3: CP01: Basics of C Programming

A Sample C Program

#include <stdio.h> int main(void){    printf("Hello, World!\n");    return (0);}

Page 4: CP01: Basics of C Programming

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: }

Page 5: CP01: Basics of C Programming

Comments

Comments are beauty marks for a program

// This is a single line comment

/* This   is   a   multiline   comment */

Multiline Comments cannot be nested

Page 6: CP01: Basics of C Programming

Incorrect Usage of Comment

/* This is an incorrect usage of a comment

/* Hello   This   Comment   Is Wrong */

*/

Page 7: CP01: Basics of C Programming

Queer Comments

/* 

// Hello This Comment Is Queer

*/

// Hello //This //Comment //Is //Queer

Page 8: CP01: Basics of C Programming

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: }

Page 9: CP01: Basics of C Programming

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: }

Page 10: CP01: Basics of C Programming

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: }

Page 11: CP01: Basics of C Programming

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: }

Page 12: CP01: Basics of C Programming

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: }

Page 13: CP01: Basics of C Programming

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

Page 14: CP01: Basics of C Programming

#include

#include <stdio.h>

#include ”myfile.h”

Page 15: CP01: Basics of C Programming

#define

#define PI 3.14159

#define SQR(x) ((x) * (x))

Page 16: CP01: Basics of C Programming

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

Page 17: CP01: Basics of C Programming

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'

Page 18: CP01: Basics of C Programming

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: }

Page 19: CP01: Basics of C Programming

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: }

Page 20: CP01: Basics of C Programming

Suggest A Variable Type for:

Roll Number Grade obtained in a course Area of a circle Value of PI

Page 21: CP01: Basics of C Programming

Integer Airthmetic

5  +  5   = 10

10 /  5   =  2

9  /  5   =  1

23 /  7   =  3

Page 22: CP01: Basics of C Programming

Find the value of:

24 / 7 * 2 + 11 / 2

Page 23: CP01: Basics of C Programming

Find the value of:

24 / 7 * 2 + 11 / 23 * 2 + 5

6 + 511

Page 24: CP01: Basics of C Programming

Functions

26: int product(int x, int y)27: {28:     return (x * y);29: }

Page 25: CP01: Basics of C Programming

Functions

26: int product(int x, int y)27: {28:     return (x * y);29: }

Page 26: CP01: Basics of C Programming

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: }

Page 27: CP01: Basics of C Programming

printf

printf(format string, print list);

printf(format string);

Eg: printf(“The result is %lf ”, kms);

Page 28: CP01: Basics of C Programming

scanf

scanf (format string, input list);

Eg: scanf (“%c%d”,&letter, &age);

Page 29: CP01: Basics of C Programming

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: }

Page 30: CP01: Basics of C Programming

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

Page 31: CP01: Basics of C Programming

Relational Operators

A > B

A < B

A == B

A != B

A >= B

A <= B

Page 32: CP01: Basics of C Programming

Boolean Operators

NOT : !A

OR  : A || B

AND : A && B

Page 33: CP01: Basics of C Programming

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: }

Page 34: CP01: Basics of C Programming

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: }

Page 35: CP01: Basics of C Programming

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: }

Page 36: CP01: Basics of C Programming

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: }

Page 37: CP01: Basics of C Programming

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: }