C programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

C Programming PowerPoint PPT Presentation


  • 50 Views
  • Uploaded on
  • Presentation posted in: General

C Programming. General Information on C Data Types Arithmetic Operators Relational Operators if, if-else, for, while by Kulapan Waranyuwat. General Info on C.

Download Presentation

C Programming

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


C programming

C Programming

  • General Information on C

  • Data Types

  • Arithmetic Operators

  • Relational Operators

  • if, if-else, for, while

    by Kulapan Waranyuwat


General info on c

General Info on C

  • C is a much more powerful language than AWK. In fact, most of the programs for Unix and the Unix system itself is written in C. C is used to write large software systems, network programming, and it is very fast and efficient. However, it’s probably a more difficult language to learn and master and it takes more time to write C programs than AWK for example due to its complexity.


Data types

Data Types

  • C is a bit more picky about its data than AWK is. You’ll see what I mean soon.

  • There are 4 data types in C:integer - intfloating point - floatdouble - doublecharacter - char


Integer

Integer

  • These are whole numbers, both positive and negative.

  • int sum; /* declares an integer variable called sum. */int num = 5; /* declares an integer variable called num and sets it equal to 5.


Float

Float

  • These are numbers which contain fractional parts, both positive and negative.

  • float amount; /* declares a floating point called amount */float money = 5.15/* declares a floating point value of 5.15 to variable called money.


Double

Double

  • These are exponential numbers, both positive and negative.

  • double bignum;/* declars a double called bignum */double bignum = 31E+5; /* declares bignum as a double and initializes it to 31E+5 */


C programming

Char

  • These are single characters.

  • char word;/* declares a char called word */char letter = ‘B’;/* declares a char variable called letter and assigns B to it. */

  • Note: Use single quotes for single character!


Operators

Operators

  • operation: operator: expression: value before: value after.

  • Multiply : * : sum = sum * 2; : 4 : 8

  • Divide : / : sum = sum / 2; : 4 : 2

  • Addition : + : sum = sum + 2; : 4 : 6

  • Subtraction : - : sum = sum -2; : 4 : 2

  • Increment : ++ : ++sum; : 4 : 5

  • Decrement : -- : --sum; : 4 : 3

  • Modulus : % : sum = sum % 3; : 4 : 1


Keyboard input

Keyboard Input

  • Use scanf() function. See Example 3.


Relational operators

Relational Operators

  • == meaning “equal to”!= meaning “not equal to”< meaning “less than”<= meaning “less than or equal to”> meaning “greater than”>= meaning “greater than or equal to”


For loop

for loop

  • The logic of the C for-loop is the same as in AWK. Let’s look at some examples… The general format is:for( start condition; continue condition; re-evaulation ) program statement;

  • See Example 4.

  • Look at Example 5 for some exercises.


While

while

  • The general format is:while( condition ) program statement;

  • See Example 6.


If if else

if, if-else

  • The general format of if: if( expression ) program statement;

  • The general format of if-else:

  • if( condition 1 ) statement1;else if( condition 2 ) statement2;else if( condition 3 ) statement3;else statement4;

  • See Example 7.


Compound relationals

Compound Relationals

  • LOGICAL AND &&Logical AND requires all conditions to evaluate as TRUE.

  • LOGICAL OR ||Logical OR will be executed if any ONE of the conditions is TRUE.

  • LOGICAL NOT !Logical NOT negates a condition.

  • LOGICAL EOR ^Logical EOR will be executed if either condition is TRUE, but NOT if they are all true.

  • See Example 8.

  • Example 9 for more exercises.


Arrays

Arrays

  • An array is a type of data structure that is used to hold multiple variables of the same data type, as mentioned last week in the AWK lecture. It is the same in C.

  • A simple application:Suppose a company wants to keep track of all its employees by assigning the names of each respective employee with an identification number. If one were to do the following, it would be a very tedious task.int employee1 = id1int employee2 = id2and so on….

  • Arrays are a good solution to this problem. Arrays will create a “file cabinet” and everything in this cabinet will be of the same type. This makes storing data with arrays, in this specific problem, very practical. So you have a file cabinet with only integer variables all holding some integer value.


Array syntax

Array syntax

  • int employees[3]; employees[0] = 101; employees[1] = 232; employees[2] = 0;

  • To access an element in the array, use square brackets. name_of_array[i]so that name_of_array[5] refers to the sixth element in an array called name_of_array. In C, array elements start with 0.

  • Assigning values to an array is done by:x[10] = g; /* x = name of array, 10=index, g=value. */

  • and assigning array elements to a variable is done by g = x[10]; /* g=variable name, x=array name, 10=index. */


Arrays cont

Arrays (cont.)

  • See Example 10.


Multi dimensional arrays

Multi-dimensional arrays

  • Multi-dimensioned arrays have two or more index values which specify the element in the array.multi[i][j]

  • In the above example, the first index value i specifies a row index, whilst j specifies a column index.

  • Example:int m1[10][10]; static int m2[2][2] = { {0,1}, {2,3} }; sum = m1[i][j] + m2[k][l];

  • See Example 11 for exercises.


C programming

(cont.)

  • { 0, 1 },{ 2, 3 }

  • Remember that arrays are split up into row and columns. The first is the row, the second is the column. Looking at the values assigned to m2: m2[0][0] = 0m2[0][1] = 1m2[1][0] = 2m2[1][1] = 3


An aside on printf

An aside on printf()

  • %c single character%d signed decimal integer%f floating-point, decimal notation%s character string%p a pointer%% print a percent sign

  • The format for using printf() is:printf (Control-string, item1, item2, …);

  • See previous examples for more on printf().


The end

THE END


  • Login