c programming an introduction
Download
Skip this Video
Download Presentation
C programming an Introduction

Loading in 2 Seconds...

play fullscreen
1 / 24

C programming an Introduction - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' C programming an Introduction' - nate


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
types
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
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
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
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
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
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 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
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
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
Basic Arithmetic Operators
  • Addition +
  • Subtraction –
  • Multiplication *
  • Division /
  • Modulus %
  • Increment ++
  • Decrement --
basic program structure
Basic program structure
  • #Include <stdio.h>
  • main()
  • { declarations;
  • statements;
  • }
basic example
Basic example
  • #Include <stdio.h>
  • main()
  • { int a;
  • printf(“enter number \n);
  • scanf(“%d”,&a);
  • printf(“the number you entered is %d”,a);
  • }
basic example1
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
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 example2
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
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 example3
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
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
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
Boolean operators
  • The three Boolean operators are:
  • && and
  • || or
  • ! not (takes one operand; ``unary\'\')
if else
If else
  • if( expression ) statement<sub>1</sub>elsestatement<sub>2</sub>
  • if (a > b) printf(a);

Else printf(b);

if else if
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 example4
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);
  • }
ad