CSC 1107: Structured Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1107 http://muele.mak.ac.ug block A, level 3, department of computer science institute of open, distance, and elearning Introduction Kizito (Makerere University) CSC 1107 August, 2018 1 / 12
Overview 1 Introduction 2 Types 3 Variables 4 Operators 5 Conversions and Casting 6 Arrays Kizito (Makerere University) CSC 1107 August, 2018 2 / 12
Introduction Introduction The C Programming Language A computer program (or just a program) is a sequence of instructions, writtem to perform a specific task with a computer A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer Programming languages can be used to create programs The only way to learn a new programming language is by writing programs in it First C Program #include <stdio.h> main() { printf("hello, world\n"); } Output: Hello, world Kizito (Makerere University) CSC 1107 August, 2018 3 / 12
Types Types Data Types and Sizes Basic Data Types Qualifiers char: A single byte (8 bits), capable of holding one character in the local character set int (either 16 or 32 bits no longer than long): An integer, typically reflecting the natural size of integers on the host machine float: Single-precision floating point double: Double-precision floating point short: Often 16 bits long no longer than int long: At least 32 bits when applied to int unsigned: Always positive or zero, and obey the laws of arithmetic modulo 2 n, where n is the number of bits in the type signed: Default Kizito (Makerere University) CSC 1107 August, 2018 4 / 12
Types Types Constants Variables and constants are the basic data objects manipulated in a program Type Example Notes int 1234 037 Leading zero means integer in octal (31 10 ) 0x1F Leading 0x or 0X means integer in hexadecimal long int 123456789L Written with a terminal l (ell) or L unsigned Written with a terminal u or U unsigned long Written with the suffix ul or UL 0xFUL 15 in decimal double 123.4 Contain a decimal point or an exponent or 1e-2 both float 123.4F Written with the suffix f or F long double 123.4L Written with the suffix l or L char A One character within single quotes (65 10 ) Kizito (Makerere University) CSC 1107 August, 2018 5 / 12
Types Constants Escape Sequences Recall the \n in printf("hello, world\n"); of our first program... \a alert (bell) character \\ backslash \b backspace \? question mark \f formfeed \ single quote \n newline \ double quote \r carriage return \ooo octal number \t horizontal tab \xhh hexadecimal number \v vertical tab Kizito (Makerere University) CSC 1107 August, 2018 6 / 12
Variables Variables Variable Declarations Variable names are made up of letters and digits The first character must be a letter The underscore counts as a letter Library routines often begin variable names with underscore All variables must be declared before use Examples: 1 int lower, upper = MAXLINE+1, step; 2 char c, line[1000]; 3 char esc = \\ ; 4 float eps = 1.0e-5; 5 const double e = 2.71828182845905; 6 const char msg[] = "warning: "; Kizito (Makerere University) CSC 1107 August, 2018 7 / 12
Operators Operators An operator takes one or more operands and produces a result An operator is just a function, but with a different syntax Most of C s operators can be divided into: Arithmetic: +,, *, /, % Relational and Logical: >, >=, <, <=, &&,,!, ==,!= Increment and Decrement: ++, Bitwise: &,, ˆ, <<, >>, Assignment: =, op= Ternary:?: Example (get n bits of x from position p). Does it work for n > p + 1? int b = (x >> (p+1-n)) & ( 0 << n); Kizito (Makerere University) CSC 1107 August, 2018 8 / 12
Operators Operators Precedence and Associativity Highest Lowest Operators Associativity () [] >. left to right! ++ + (type) sizeof right to left / % left to right + left to right << >> left to right < <= > >= left to right ==! = left to right & left to right ˆ left to right left to right && left to right left to right? : right to left = + = = = / = % = & = ˆ= = <<= >>= right to left, left to right Kizito (Makerere University) CSC 1107 August, 2018 9 / 12
Conversions and Casting Example Type Conversions Type Conversion and Casting Operands of different types in the same expression are converted to a common type Narrowing conversions may draw a warning, but they are not illegal Recall: a char is a small integer Conversions take place across assignments Explicit type conversions can be forced with the operator called a cast (type-name) expression int i, n = 0, m; char s[]; n = 10 * n + (s[i] - 0 ); m = (unsigned int)(n*1234567) % 32; Kizito (Makerere University) CSC 1107 August, 2018 10 / 12
Arrays Arrays An array is a group of like-type variables that are referred to by a common name Arrays can be of any type Arrays may have one or more dimensions The simplest kind is a linear array also called one-dimensional array A specific element in an array is accessed by its key or index Kizito (Makerere University) CSC 1107 August, 2018 11 / 12
Arrays Arrays Declaring and Indexing Arrays General form: type var-name[size]; Examples 1 int month days[12]; 2 int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 3 int x; char daytab[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31} {0,31,29,31,30,31,30,31,31,30,31,30,31} }; x = daytab[0][2]; // x equals 28 Indexing the array 1 month days[1] = 28; 2 printf("%d", month days[3]); 3 printf("april has %d days", mdays[3]); Strings as arrays of characters (terminated by the character \0 ) Kizito (Makerere University) CSC 1107 August, 2018 12 / 12