Introduction to C Programming 06/04/2019 Introduction to C Programming Dr. Nouf Aljaffan naljaffan@ksu.edu.sa Nouf Aljaffan (C) 2018
Objectives Write simple C programs. Use simple input and output statements. Use the fundamental data types. Learn computer memory concepts. Use arithmetic operators. Learn the precedence of arithmetic operators. Write simple decision making statements. Begin focusing on secure C programming practices Nouf Aljaffan (C) 2018
Structure of a .c file Add Comments Insert pre-processor definitions 06/04/2019 Structure of a .c file Add Comments Line comment: // my comment multi-line comments : /* my comments */ Insert pre-processor definitions #include < [file name & extension] > Add function prototypes and variable declarations Define main function int main( void ) { [function body] } Define other functions Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
Structure of a .c file An Output Statement 06/04/2019 Structure of a .c file An Output Statement printf( “this will be displayed on screen” ); The backslash (\) as used here is called an escape character. Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
Multiple printfs Nouf Aljaffan (C) 2018
Avoid Single-Argument printf With newline Without newline Nouf Aljaffan (C) 2018
Adding Two Integers Add Comments Insert pre-processor definitions 06/04/2019 Adding Two Integers Add Comments Insert pre-processor definitions Define main function Declare variables [DataType] [VariableName]; Variable names are case sensitive User scanf to read variables by a user at the keyboard scanf(“format control string,”, &variableName) computes the sum of two values Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
Input / Output More format specifiers printf (); //used to print to console(screen) example: printf(“%c”, ’a’); scanf (); //used to take an input from console(user). example: scanf(“%d”, &a); %c The character format specifier. %d The integer format specifier. %i The integer format specifier (same as %d). %f The floating-point format specifier. %o The unsigned octal format specifier. %s The string format specifier. %u The unsigned integer format specifier. %x The unsigned hexadecimal format specifier. %% Outputs a percent sign. C Course, Programming club, Fall 2008
Arithmetic in C Nouf Aljaffan (C) 2018
Rules of Operator Precedence Nouf Aljaffan (C) 2018
Decision Making: Equality and Relational Operators Nouf Aljaffan (C) 2018
06/04/2019 Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
06/04/2019 Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
06/04/2019 Nouf Aljaffan (C) 2018 Nouf Aljaffan (C) 2018
Nouf Aljaffan (C) 2018
are reserved words of the language Nouf Aljaffan (C) 2018
Conclusion Program structure printf & scanf Arithmatics Equality and rational operators Keywords Nouf Aljaffan (C) 2018
Any Question! Nouf Aljaffan (C) 2018
Exercises Nouf Aljaffan (C) 2018
Exercise 1 Identify and correct the errors in each of the following statements: printf( "The value is %d\n", &number ); scanf( "%d%d", &number1, number2 ); if ( c < 7 );{ printf( "C is less than 7\n" ); } if ( c => 7 ) { printf( "C is greater than or equal to 7\n" ); Nouf Aljaffan (C) 2018
Exercise 2 Convert the algorithm to C source code Nouf Aljaffan (C) 2018
Exercise 3 Convert the algorithm to C source code Nouf Aljaffan (C) 2018
References C how to program, 8\e, by P. and H. Deitels , Chapter 2 Nouf Aljaffan (C) 2018