240 likes | 371 Views
C programming an Introduction. 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
E N D
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
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;
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).
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.
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 %
C’s backslash codes • \b backspace • \f formfeed • \n newline • \r carriage return • \t horizontal tab • \” double quote • \0 null • \\ backslash
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
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
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
Basic Arithmetic Operators • Addition + • Subtraction – • Multiplication * • Division / • Modulus % • Increment ++ • Decrement --
Basic program structure • #Include <stdio.h> • main() • { declarations; • statements; • }
Basic example • #Include <stdio.h> • main() • { int a; • printf(“enter number \n); • scanf(“%d”,&a); • printf(“the number you entered is %d”,a); • }
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); • }
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
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); • }
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> }
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); • } • }
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
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.
Boolean operators • The three Boolean operators are: • && and • || or • ! not (takes one operand; ``unary'')
If else • if( expression ) statement<sub>1</sub>elsestatement<sub>2</sub> • if (a > b) printf(a); Else printf(b);
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");
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); • }