
5/24/2016 Resources • Kochen, Programming in C, Third Edition Review of Programming • Brian Kernighan and Dennis Ritchie, The C Programming C Language , 2 nd Edition, Prentice Hall – Is considered “THE” book on C : coauthor belongs to the creators of the C programming language – The book is not an introductory programming manual; it assumes some familiarity with basic programming concepts • C Programming Notes by Steve Summit http://www.eskimo.com/~scs/cclass/notes/top.html Based on the Original Slides from Politehnica International- Computer Engineering Lecture Slides Compilers The C Programming Language • Developed by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s Source Machine Compiler Code Code • Growth of C tightly coupled with growth of Unix: Unix was written mostly in C • Success of PCs: need of porting C on MS-DOS • Many providers of C compilers for many different platforms => Input Executable Output need for standardization of the C language data Program data • 1990: ANSI C (American National Standards Institute) • International Standard Organization: ISO/IEC 9899:1990 Compiler: analyzes program and • 1999: standard updated: C99, or ISO/IEC 9899:1999 translates it into machine language Executable program: can be run independently from compiler as many times => fast execution 1 5/24/2016 The first C program Compiling and running C programs uses standard library input and output functions Editor (printf) Source code #include <stdio.h> file.c the program int main (void) begin of program { Compiler printf ("Programming is fun.\n"); Object code statements return 0; file.obj end of program } Libraries main: a special name that indicates where the program must begin execution. It is Linker a special function . Executable code file.exe first statement: calls a routine named printf, with argument the string of characters IDE (Integrated “Programming is fun \n” Development last statement: finishes execution of main and return to the system a status value Environment) of 0 (conventional value for OK) C Compilers and IDE’s Debugging program errors • One can: – use a text editor to edit source code, and then use independent Editor command-line compilers and linkers Syntactic Source code Errors – use an IDE: everything together + facilities to debug, develop and file.c organize large projects • There are several C compilers and IDE’s that support various C compilers Compiler We will use Eclipse and the “GCC” compiler. Object code • file.obj gcc prog.c porg.out Libraries Linker Executable code file.exe Semantic Errors 2 5/24/2016 Displaying multiple values Using comments in a program /* This program adds two integer values and displays the results */ #include <stdio.h> int main (void) #include <stdio.h> { int main (void) int value1, value2, sum; { value1 = 50; // Declare variables value2 = 25; int value1, value2, sum; sum = value1 + value2; // Assign values and calculate their sum printf ("The sum of %i and %i is %i\n",value1, value2, sum); value1 = 50; return 0; value2 = 25; } sum = value1 + value2; // Display the result printf ("The sum of %i and %i is %i\n", The format string must contain as many placeholders as expressions to be printed value1, value2, sum); return 0; } Example: Using data types Basic Data Types - Summary Type Meaning Constants Ex. printf #include <stdio.h> int Integer value; guaranteed to contain at least 16 bits 12, -7, %i,%d, %x, int main (void) 0xFFE0, 0177 %o { short int Integer value of reduced precision; guaranteed to - %hi, %hx, int integerVar = 100; contain at least 16 bits %ho float floatingVar = 331.79; long int Integer value of extended precision; guaranteed to 12L, 23l, %li, %lx, 0xffffL %lo double doubleVar = 8.44e+11; contain at least 32 bits char charVar = 'W'; long long Integer value of extraextended precision; guaranteed 12LL, 23ll, %lli, 0xffffLL %llx, %llo printf ("integerVar = %d\n", integerVar); int to contain at least 64 bits printf ("floatingVar = %f\n", floatingVar); unsigned Positive integer value; can store positive values up 12u, 0XFFu %u, %x, %o printf ("doubleVar = %e\n", doubleVar); int to twice as large as an int; guaranteed to contain at printf ("doubleVar = %g\n", doubleVar); least 16 bits (all bits represent the value, no sign bit) printf ("charVar = %c\n", charVar); unsigned - %hu, %hx, return 0; short int %ho } unsigned 12UL, 100ul, %lu, %lx, long int 0xffeeUL %lo unsigned 12ull, %llu, long long 0xffeeULL %llx, %llo int 3 5/24/2016 Basic Data Types - Summary (contd.) Integer and Floating-Point Conversions Type Meaning Constants printf // Basic conversions in C #include <stdio.h> float Floating-point value; a value that can contain decimal 12.34f, 3.1e- %f, %e, places; guaranteed to contain at least six digits of 5f %g int main (void) precision . { double Extended accuracy floating-point value; guaranteed to 12.34, 3.1e-5, %f, %e, float f1 = 123.125, f2; contain at least 10 digits of precision . %g int i1, i2 = -150; char c = 'a'; long Extraextended accuracy floating-point value; 12.341, 3.1e- %Lf, i1 = f1; // floating to integer conversion double guaranteed to contain at least 10 digits of 5l %Le, %Lg printf ("%f assigned to an int produces %d\n", f1, i1); precision. f1 = i2; // integer to floating conversion char Single character value; on some systems, sign 'a', '\n' %c printf ("%d assigned to a float produces %f\n", i2, f1); extension might occur when used in an expression. f1 = i2 / 100; // integer divided by integer printf ("%d divided by 100 produces %f\n", i2, f1); unsigned Same as char, except ensures that sign extension - char does not occur as a result of integral promotion. f2 = i2 / 100.0; // integer divided by a float printf ("%d divided by 100.0 produces %f\n", i2, f2); signed Same as char, except ensures that sign extension - f2 = (float) i2 / 100; // type cast operator char does occur as a result of integral promotion. printf ("(float) %d divided by 100 produces %f\n", i2, f2); return 0; } The Type Cast Operator The assignment operators • f2 = (float) i2 / 100; // type cast operator • The C language permits you to join the arithmetic operators with the • The type cast operator has the effect of converting the value of the variable i2 to assignment operator using the following general format: op=, where op type float for purposes of evaluation of the expression. is an arithmetic operator, including +, –, ×, /, and %. • This operator does NOT permanently affect the value of the variable i2; • op can also be a logical or bit operator => later in this course • The type cast operator has a higher precedence than all the arithmetic operators • Example: except the unary minus and unary plus. count += 10; – Equivalent with: • Examples of the use of the type cast operator: count=count+10; • (int) 29.55 + (int) 21.99 results in 29 + 21 • Example: precedence of op=: (float) 6 / (float) 4 results in 1.5 • a /= b + c • (float) 6 / 4 results in 1.5 – Equivalent with: a = a / (b + c) – addition is performed first because the addition operator has higher precedence than the assignment operator 4 5/24/2016 Program input The while statement #include <stdio.h> It’s polite to while ( expression ) int main (void) display a { message before program statement int n, number, triangularNumber; printf ("What triangular number do you want? "); scanf ("%i", &number); triangularNumber = 0; Reads integer while ( number <= 0 ) { for ( n = 1; n <= number; ++n ) from keyboard printf (“The number must be >0“); triangularNumber += n; printf (“Give a new number: “); printf ("Triangular number %i is %i\n", number, scanf(“%i“, &number); triangularNumber); } return 0; } Scanf: similar to printf: first argument contains format characters, next arguments tell where to store the values entered at the keyboard More details -> in a later chapter ! Example – do while Statements break and continue • Programming style: don’t abuse break !!! // Program to reverse the digits of a number #include <stdio.h> ... int main () { while ( number != 0 ) { int number, right_digit; // Statements to do something in loop printf ("Enter your number.\n"); scanf ("%i", &number); printf("Stop, answer 1: "); do { right_digit = number % 10; scanf ("%i", &answer); printf ("%i", right_digit); if(answer == 1) number = number / 10; } break; // very bad idea to do this while ( number != 0 ); printf ("\n"); } return 0; } 5 5/24/2016 Statements break and continue Example: compound relational test Continue also not so good style!!! // Program to determine if a year is a leap year ... #include <stdio.h> int main (void) while ( number != 0 ) { { // Statements to do something in loop int year, rem_4, rem_100, rem_400; printf(“Skip next statements answer 1: "); printf ("Enter the year to be tested: "); scanf ("%i", &answer); scanf ("%i", &year); if(answer == 1) rem_4 = year % 4; continue; // not so good idea… rem_100 = year % 100; // Statements to do something in loop rem_400 = year % 400; if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 ) // If answer was 1 these statements are printf ("It's a leap year.\n"); // not executed. They are skipped. else // Go straight to the beginning of while printf (“It's not a leap year.\n"); } return 0; } Logical operators Boolean variables Operator Symbol Meaning // Program to generate a table of prime numbers AND && X && y is true if BOTH x and y are true #include <stdio.h> OR || X || y is true if at least one of x and y is true int main (void) { int p, d; A flag : assumes only NOT ! !x is true if x is false _Bool isPrime; one of two different for ( p = 2; p <= 50; ++p ) { values.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages22 Page
-
File Size-