C programming an Introduction
description
Transcript of C programming an Introduction
![Page 1: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/1.jpg)
C programming an Introduction
![Page 2: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/2.jpg)
Types
• There are only a few basic data types in C. • char a character • int an integer, in the range -32,767 to 32,767 • long int a larger integer (up to +-2,147,483,647) • float a floating-point number • double a floating-point number, with more
precision and perhaps greater range than float
![Page 3: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/3.jpg)
Declarations
• A declaration tells the compiler the name and type of a variable you'll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon:
• EXAMPLES• char c; • int i; • float f;
![Page 4: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/4.jpg)
Variable names• Within limits, you can give your variables and functions
any names you want • Must be alphanumeric • Can use underscores etc• Case in C is significant: the variable names variable,
Variable, and VARIABLE (as well as silly combinations like variAble) are all distinct.
• A final restriction on names is that you may not use keywords (the words such as int and for which are part of the syntax of the language) as the names of variables or functions (or as identifiers of any kind).
![Page 5: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/5.jpg)
simple output
• Printf• Examples• printf("Hello, world!\n"); • \n moves output to a newline
• printf("i is %d\n", i); • In that case, whenever printf ``printed'' the string "i
is %d", it did not print it verbatim; it replaced the two characters %d with the value of the variable i.
![Page 6: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/6.jpg)
Format specifiers for printf.• There are quite a number of format specifiers for printf. Here are
the basic ones :
• %d print an int argument in decimal • %ld print a long int argument in decimal • %c print a character • %s print a string • %f print a float or double argument • %e same as %f, but use exponential notation • %g use %e or %f, whichever is better • %o print an int argument in octal (base 8) • %x print an int argument in hexadecimal (base 16) • %% print a single %
![Page 7: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/7.jpg)
C’s backslash codes
• \b backspace• \f formfeed• \n newline• \r carriage return• \t horizontal tab• \” double quote• \0 null• \\ backslash
![Page 8: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/8.jpg)
Scanf input operator
• Scanf(formatspecifier,&variablename)• Examples• scanf(“%d”,&a) where a is an integer• scanf(“%c”,&c) where c is a character • scanf(“%f”,&h) where h is a real
![Page 9: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/9.jpg)
Assignment
• The assignment operator = assigns a value to a variable. For example,
• x = 1 sets x to 1, and • a = b sets a to whatever b's value is.• The right hand side of an assignment
operation may be an expression such as • a = a+1
![Page 10: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/10.jpg)
Expression Statements
• Most of the statements in a C program are expression statements. An expression statement is simply an expression followed by a semicolon. The lines
• i = 0; i = i + 1; and printf("Hello, world!\n"); are all expression statements
![Page 11: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/11.jpg)
Basic Arithmetic Operators
• Addition +• Subtraction –• Multiplication *• Division /• Modulus %• Increment ++• Decrement --
![Page 12: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/12.jpg)
Basic program structure
• #Include <stdio.h>• main()• { declarations;• statements;• }
![Page 13: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/13.jpg)
Basic example
• #Include <stdio.h>• main()• { int a;• printf(“enter number \n);• scanf(“%d”,&a);• printf(“the number you entered is %d”,a);• }
![Page 14: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/14.jpg)
Basic example• #Include <stdio.h>• main()• { int a,b,c;• printf(“enter first number \n”);• scanf(“%d”,&a);
• printf(“enter second number \n”);• scanf(“%d”,&b );• c= a*b;• printf(“the product of the numbers you entered is %d”,c);• }
![Page 15: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/15.jpg)
If statements
• The simplest way to modify the control flow of a program is with an if statement, which in its simplest form looks like this:
• if(x > max) max = x; • The syntax of an if statement is: If ( expression ) statement where expression is any expression and
statement is any statement
![Page 16: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/16.jpg)
Basic example• #Include <stdio.h>• main()• { int a;• printf(“enter number \n”);• scanf(“%d”,&a);
• if(a > 10) printf(“the number you entered %d is more than ten”,c);
• }
![Page 17: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/17.jpg)
A series of statements after an if statement
• What if you have a series of statements, all of which should be executed together or not at all depending on whether some condition is true? The answer is that you enclose them in braces:
• if( expression ) { statement<sub>1</sub> statement<sub>2</sub>
statement<sub>3</sub> }
![Page 18: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/18.jpg)
Basic example• #Include <stdio.h>• main()• { int a,b,c;• printf(“enter first number \n”);• scanf(“%d”,&a);
• printf(“enter second number \n”);• scanf(“%d”,&b );• c= a*b;• If (c > 100) • {printf(“ok \n”);• printf(“the product of the numbers you entered is %d”,c);• }• }
![Page 19: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/19.jpg)
Expressions in If statements
• The expression in an if statement is a Boolean expression typically made up of
• Relational and Boolean operators • The statements in an if stamenst are any
legal statements
![Page 20: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/20.jpg)
Relational Operators• The relational operators such as <, <=, >, and >= are in fact
operators, just like +, -, *, and /.• The relational operators take two values, test them, and ``return'' a
value of 1 or 0 depending on whether the tested relation was true or false.
• The complete set of relational operators in C is:
< less than • <= less than or equal• > greater than• >= greater than or equal• == equal • != not equal
For example, 1 < 2 is 1, 3 > 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.
![Page 21: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/21.jpg)
Boolean operators
• The three Boolean operators are:
• && and • || or • ! not (takes one operand; ``unary'')
![Page 22: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/22.jpg)
If else
• if( expression ) statement<sub>1</sub> else statement<sub>2</sub>
• if (a > b) printf(a);Else printf(b);
![Page 23: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/23.jpg)
If else if• We can also have multiple specific alternatives
using if … else if…..• Suppose we have a variable grade containing a
student's numeric grade, and we want to print out the corresponding letter grade. Here is code that would do the job:
• if(grade >= 90) printf("A"); else if(grade >= 80) printf("B"); else if(grade >= 70) printf("C"); else if(grade >= 60) printf("D"); else printf("F");
![Page 24: C programming an Introduction](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815e79550346895dccfb2f/html5/thumbnails/24.jpg)
Basic example• #Include <stdio.h>• main()• { int a,b,c;• printf(“enter first number \n”);• scanf(“%d”,&a);
• printf(“enter second number \n”);• scanf(“%d”,&b );• If (a > b !! a > 5)• printf(“%d is greater than %d or 5”,a,b);• Else• printf(“%d is not greater than %d and not greater than 5”,a,b);• }