ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters"

Transcription

1 The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers char variables mostly used to store characters encoded in ASCII. Only store characters in char variables in COMP1911. Even if a numeric variable is only use for the values 0..9, use the type int for the variable. ASCII ( American Standard Code for Information Interchange) specifies encoding for 128 characters to the integers The characters encoded include upper and lower case (English) letters, digits, common punctuation symbols (as you might find on the keyboard) plus a series of non-printable control characters (including newline and tab). Manipulating Characters Manipulating Characters Does this mean you will have to remember 100+ character codes!? Luckily no! We use character literals instead! For example: char a = a ; // ASCII code 97 char A = A ; // ASCII code 65 char space = ; // ASCII code 32 char p l u s = + ; // ASCII code 43 char n e w l i n e = \ n ; // ASCII code 10 Style Note Always use character literals in your code! Even if you are really proud of having memorised the ASCII Table! NOTE The ASCII codes for the digits (48 57), the upper case letters (65 90) and lower case letters (97 122) are in sequence. Knowing this allows us to do some neat things: i n t a = a ; i n t b = a + 1 ; // t h i s i s p o s s i b l e due to i n t c = a + 2 ; // the u n d e r l y i n g numeric type i n t B = b ( a A ) ;

2 Manipulating Characters Printing and Reading Characters We can also test various properties of characters: // check f o r l o w e r c a s e i f ( c >= a && c <= z ) { Problem: Convert a digit character to the integer it represents, e.g., 0 0, 7 7, etc. We use the fact that the digits codes are in order: // check i s a d i g i t i f ( c >= 0 && c <= 9 ) { v a l = c 0 ; // why does t h i s work? C provides library functions for reading and writing characters The getchar function This function reads and returns one input character. Note that, unlike scanf, it has no arguments; its return value is collected by assigning it to a variable. The putchar function This function takes a single int argument and prints it out Here is an example: i n t c ; p r i n t f ( P l e a s e e n t e r a c h a r a c t e r : ) ; c = g e t c h a r ( ) ; p u t c h a r ( c ) ; Reading Characters Reading Characters Consider the following code: i n t c1, c 2 ; p r i n t f ( P l e a s e e n t e r f i r s t c h a r a c t e r : \ n ) ; c1 = g e t c h a r ( ) ; p r i n t f ( P l e a s e e n t e r second c h a r a c t e r : \ n ) ; c2 = g e t c h a r ( ) ; p r i n t f ( F i r s t %c \ nsecond : %c \n, c1, c2 ) ; What is the output? Turns out that the newline input by pressing Enter after the first character is read by the second getchar. How can we fix the program? i n t c1, c2 ; p r i n t f ( P l e a s e e n t e r f i r s t c h a r a c t e r : \ n ) ; c1 = g e t c h a r ( ) ; g e t c h a r ( ) ; // r e a d s and d i s c a r d s a c h a r a c t e r p r i n t f ( P l e a s e e n t e r second c h a r a c t e r : \ n ) ; c2 = g e t c h a r ( ) ; p r i n t f ( F i r s t : %c \ nsecond : %c \n, c1, c2 ) ; Printing characters The conversion specifier for characters is %c. Using it we can supply variables of char type to printf for output:

3 End of Input An input funcion can such as scanf or getchar can fail because there is no input is available. This can occur, for example, if input is coming from a file and the end of the file is reached. On UNIX-like systems such Linux & OSX typing Ctrl + D on a terminal signals to the operating system there is no more input from the terminal. Windows has no equivalent, but some windows program interprert Ctrl + Z similarly. getchar returns a special non-ascii value to indicate there is no input was available. This non-ascii value is #defined as EOF in stdio.h. On most systems EOF == -1. Note -1, isn t an ASCII value (0..127) There is no end-of-file character on Linux or other modern operating systems. Reading Characters to End of Input Programming pattern for reading characters to the end of input: i n t ch ; ch = g e t c h a r ( ) ; w h i l e ( ch!= EOF) { p r i n t f ( you e n t e r e d the c h a r a c t e r : %c which ha ch = g e t c h a r ( ) ; For comparison the programming pattern for reading integers to end of input: i n t num ; // s c a n f r e t u r n s the number o f i t e m s read w h i l e ( s c a n f ( %d, &num) == 1) { p r i n t f ( you e n t e r e d the number : %d\n, num ) ; Strings A string is a sequence of characters. C uses a special ASCII character \0 to mark the end of strings. This is convenient because programs don t have to track the lnegth of the string. Definition A C string is a null-terminated character array. Consider the following: // t h i s i s i n c o r r e c t, \ 0 w i l l be d i s c a r d e d c h a r h e l l o [ 5 ] = { h, e, l, l, o, \ 0 ; // t h i s i s OK c h a r h e l l o [ 6 ] = { h, e, l, l, o, \ 0 ; // t h i s i s b e t t e r c h a r h e l l o [ ] = { h, e, l, l, o, \ 0 ; Useful C Library Functions for Characters The C library includes some useful functions which operate on characters. Several of the more useful listed below. Note the you need to #include <ctype.h> to use them. #i n c l u d e <c t y p e. h> i n t t o u p p e r ( i n t c ) ; // c o n v e r t c to upper c a s e i n t t o l o w e r ( i n t c ) ; // c o n v e r t c to l o w e r c a s e i n t i s a l p h a ( i n t c ) ; // t e s t i f c i s a l e t t e r i n t i s d i g i t ( i n t c ) ; // t e s t i f c i s a d i g i t i n t i s l o w e r ( i n t c ) ; // t e s t i f c i s a l o w e r c a s e l e t t e r i n t i s u p p e r ( i n t c ) ; // t e s t i f c i s a upper c a s e l e t t e r String Length The null character must be accounted for when sizing strings, although, somewhat confusingly, the library function strlen returns the length of a string not including the null character.

4 Strings Reading an Entire Input Line Because working with strings is so common, C provides some convenient syntax. Instead of writing: char h e l l o [ ] = { h, e, l, l, o, \ 0 ; You can write char h e l l o [ ] = h e l l o ; Note hello will have 6 elements The compiler automatically appends \0 when strings are initialised with string literals. Again, remember to allow space for it if sizing the array manually. The function fgets reads an entire line: #d e f i n e MAX LINE LENGTH 1024 char l i n e [ MAX LINE LENGTH ] ; f g e t s ( l i n e, MAX LINE LENGTH, s t d i n ) ; f p u t s ( l i n e, s t d o u t ) ; Reading an Entire Input Line Reading Lines to End of Input You might use fgets as follows: #d e f i n e MAX LINE LENGTH 1024 char l i n e [ MAX LINE LENGTH ] ; f g e t s ( l i n e, MAX LINE LENGTH, s t d i n ) ; f p u t s ( l i n e, s t d o u t ) ; // e q u i v a l e n t to p r i n t f ( % s, l i n e ) Programming pattern for reading lines to end of input: // f g e t s r e t u r n s NULL i f i t can t r e a d any c h a r a c t e r s w h i l e ( f g e t s ( l i n e, MAX LINE, s t d i n )!= NULL) { p r i n t f ( you e n t e r e d t h e l i n e : %s, l i n e ) ;

5 String Manipulation String Manipulation The header file string.h provides some useful string functions: // s t r i n g l e n g t h ( not i n c l u d i n g \ 0 ) s i z e t s t r l e n ( c o n s t c h a r s ) ; // s t r i n g copy c h a r s t r c p y ( c h a r dest, c o n s t c h a r s r c ) ; c h a r s t r n c p y ( c h a r dest, c o n s t c h a r s r c, s i z e t n ) ; // s t r i n g c o n c a t e n a t i o n / append c h a r s t r c a t ( c h a r dest, c o n s t c h a r s r c ) ; c h a r s t r n c a t ( c h a r dest, c o n s t c h a r s r c, s i z e t n ) ; // s t r i n g compare i n t strcmp ( c o n s t c h a r s1, c o n s t c h a r s2 ) ; i n t strncmp ( c o n s t c h a r s1, c o n s t c h a r s2, s i z e t n ) ; i n t s t r c a s e c m p ( c o n s t c h a r s1, c o n s t c h a r s2 ) ; i n t s t r n c a s e c m p ( c o n s t c h a r s1, c o n s t c h a r s2, s i z e t n ) ; // c h a r a c t e r s e a r c h c h a r s t r c h r ( c o n s t c h a r s, i n t c ) ; c h a r s t r r c h r ( c o n s t c h a r s, i n t c ) ; #i n c l u d e < s t r i n g. h> c h a r s t r 1 [ ] = H e l l o World! ; c h a r s t r 2 [ ] ; s t r n c p y ( s t r 2, s t r 1, ) ; // copy s t r 1 to s t r 2 i f ( strcmp ( s t r 1, s t r 2 ) == 0) { // case s e n s i t i v e compare p r i n t f ( S t r i n g s match! \ n ) ; // append s t r 1 to s t r 2 s t r n c a t ( s t r 2, s t r 1, 100 ( s t r l e n ( s t r 2 ) + 1 ) ) ; i f ( s t r c a s e c m p ( s t r 1, s t r 2 ) ) { // case i n s e n s i t i v e compare p r i n t f ( S t r i n g s do not match! \ n ) ; p r i n t f ( %d\n, s t r l e n ( s t r 2 ) ) ; // s t r i n g l e n g t h Note that strlen does not count the null character! String Manipulation Strings Remember You can find out about what else is available in string.h by running man string. When working with strings we use the null character as a guard: c h a r s t r 1 [ ] = H e l l o World! ; c h a r s t r 2 [ ] ; i n t i ; // the f o l l o w i n g code c o p i e s s t r 1 i n t o s t r 2 i = 0 ; // s t a r t at i n d e x 0 w h i l e ( s t r 1 [ i ]!= \ 0 ) { // s t o p on n u l l s t r 2 [ i ] = s t r 1 [ i ] ; // copy i n d i v i d u a l c h a r a c t e r s i = i + 1 ; // i n c r e m e n t i n d e x s t r 2 [ i ] = \ 0 ; // MUST s e t t h i s f o r s t r 2! In summary strings: are null-terminated character arrays can be initialised with string literals can be manipulated by scanf/printf, use %s benefit from the string manipulation functions in string.h since they are arrays they cannot be copied via assignment (=) Careful The main error encountered when working with strings is mishandling the terminating null character, e.g., forgetting to set it! Check this first if your strings are behaving strangely.

6 Arrays of Strings Command-line Arguments Sometimes, instead of manipulating each individual cell, as for matrices, we need to manipulate whole arrays. This is generally the case when working with arrays of strings! Consider: c h a r names [ 3 ] [ 2 0 ] = { Mark, Luke, John ; // why does t h i s work? p r i n t f ( % s %s %s \n, names [ 0 ], names [ 1 ], names [ 2 ] ) ; Array of arrays If we take this view (array of arrays!) of 2D arrays, it makes sense why using only the first index gives us a whole array! What are command-line arguments? Arguments that are supplied to a program when it is run. We have seen them already, for example: % diff -i file1.txt file2.txt % gedit prog.c Here, -i, file1.txt and file2.txt are command-line arguments to diff and prog.c is a command-line argument to gedit. Command-line arguments are automatically supplied to our C programs, by the operating system, via the arguments of the main function (argc and argv)! i n t main ( i n t argc, char argv [ ] ) { Command-line Arguments Command-line Arguments Arguments to main argc stores the number of command-line arguments argv stores the command-line arguments as strings i n t main ( i n t argc, c h a r a r g v [ ] ) { i n t i ; f o r ( i = 0 ; i < a r g c ; i = i + 1) { // p r i n t arguments p r i n t f ( Argument %d i s : %s \n, i + 1, a r g v [ i ] ) ; NB The first argument is always the program name! This means that argc is always at least 1 and argv contains at least one value. By default, command-line arguments are space delimited. We can use quotes if arguments include spaces. %./prog1 nospace one space %./prog2 nospace "one space" In the above, prog1 sees three command-line arguments, while prog2 sees only two. What about? %./prog3 *.c %./prog4 10 < in > out Sometimes argv is typed as: char **argv.

strsep exercises Introduction C strings Arrays of char

strsep exercises Introduction C strings Arrays of char strsep exercises Introduction The standard library function strsep enables a C programmer to parse or decompose a string into substrings, each terminated by a specified character. The goals of this document

More information

Characters and Strings. Constants

Characters and Strings. Constants Characters and Strings Constants Characters are the fundamental building blocks of source programs Character constants One character surrounded by single quotes A or? Actually an int value represented

More information

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

More information

So far we have considered only numeric processing, i.e. processing of numeric data represented

So far we have considered only numeric processing, i.e. processing of numeric data represented Chapter 4 Processing Character Data So far we have considered only numeric processing, i.e. processing of numeric data represented as integer and oating point types. Humans also use computers to manipulate

More information

C Programming Strings. Mrs. Hajah T. Sueno, MSIT instructor

C Programming Strings. Mrs. Hajah T. Sueno, MSIT instructor C Programming Strings Mrs. Hajah T. Sueno, MSIT instructor String Basics 3 Characters Characters are small integers (0-255) Character constants are integers that represent corresponding characters. 0 48

More information

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())!

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())! Character and String Processing CSE 130: Introduction to C Programming Spring 2005 The char Data Type A char value can be thought of as either a character or a small integer printf( %d, a ); /* prints

More information

1) String initialization can be carried out in the following ways, similar to that of an array :

1) String initialization can be carried out in the following ways, similar to that of an array : Strings in C : Overview : String data type is not supported in C Programming. String is essentially a collection of characters to form particular word. String is useful whenever we accept name of the person,

More information

Programming in C. Characters and Strings

Programming in C. Characters and Strings Programming in C Characters and Strings ASCII The American Standard Code for Information Interchange (ASCII) character set, has 128 characters designed to encode the Roman alphabet used in English and

More information

Reading and Writing String

Reading and Writing String Reading and writing strings Reading and Writing String Reading Format conversion %s can be used in scanf for reading strings not containing white spaces: scanf("%s", str) & not required before str as it

More information

Strings in C++ and Java. Questions:

Strings in C++ and Java. Questions: Strings in C++ and Java Questions: 1 1. What kind of access control is achieved by the access control modifier protected? 2 2. There is a slight difference between how protected works in C++ and how it

More information

C Primer. Fall Introduction C vs. Java... 1

C Primer. Fall Introduction C vs. Java... 1 CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

More information

The switch Statement. Multiple Selection. Multiple Selection (cont.)

The switch Statement. Multiple Selection. Multiple Selection (cont.) The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7, 4.12 Multiple Selection So far, we have only seen binary selection. if (

More information

Introduction to C Programming S Y STEMS

Introduction to C Programming S Y STEMS Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

More information

Your first C program. Which one is best?

Your first C program. Which one is best? Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

More information

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Chapter 3 Basic Input & Output

Chapter 3 Basic Input & Output Chapter 3 Basic Input & Output 3-1 Basic Output in C++ cout is a predefined variable in C++ that indicates you are going to output a stream of characters to an output device. cout uses an operator called

More information

Member Functions of the istream Class

Member Functions of the istream Class Member Functions of the istream Class The extraction operator is of limited use because it always uses whitespace to delimit its reads of the input stream. It cannot be used to read those whitespace characters,

More information

Tutorial No. 8 - Solution (Strings and User Defined Functions)

Tutorial No. 8 - Solution (Strings and User Defined Functions) Tutorial No. 8 - Solution (Strings and User Defined Functions) 1. Explain strcat( ), strcpy( ),strncmp( ) and strstr( ) string manipulation function. [6] strcat() Function: The strcat function joins two

More information

12 INPUT AND OUTPUT OF DATA

12 INPUT AND OUTPUT OF DATA 12 INPUT AND OUTPUT OF DATA 12.1 INTRODUCTION In C language input and output of data is done by a collection of library functions like getchar, putchar, scanf, printf, gets and puts. These functions permit

More information

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E.

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E. SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK Subject: Computer Programming and Utilization (2110003) [F.Y.B.E.: ALL BRANCHES] Unit 1

More information

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

CSC 270 Survey of Programming Languages. C-String Values

CSC 270 Survey of Programming Languages. C-String Values CSC 270 Survey of Programming Languages C Lecture 4 Strings C-String Values The most basic way to represent a string of characters in C is using an array of characters that ends with a null byte. Example

More information

5 Arrays and Pointers

5 Arrays and Pointers 5 Arrays and Pointers 5.1 One-dimensional arrays Arrays offer a convenient way to store and access blocks of data. Think of arrays as a sequential list that offers indexed access. For example, a list of

More information

UNIT III: 1. What is an array? How to declare and initialize arrays? Explain with examples

UNIT III: 1. What is an array? How to declare and initialize arrays? Explain with examples UNIT III: Arrays: Introduction, One-dimensional arrays, Declaring and Initializing arrays, Multidimensional arrays. Strings: Introduction to Strings, String operations with and without using String handling

More information

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor)

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor) GTU Questions Computer Programming & Utilization Chief Course Coordinator Prof. Mitul K.Patel (Head of Department & Assistant Professor) Course Coordinator Prof. Vrutti D. Shah (Assistant Professor) Lab

More information

Python to C/C++ Fall 2011

Python to C/C++ Fall 2011 Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

More information

(a) Arrays. Definition

(a) Arrays. Definition Part 3: Aggregate Data Types (a) Arrays Definition An array is a sequence of objects of a given type Therefore it is not a type of its own It is rather an organizational concept Array elements can be accessed

More information

Going from Python to C

Going from Python to C Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

More information

Basics of I/O Streams and File I/O

Basics of I/O Streams and File I/O Basics of This is like a cheat sheet for file I/O in C++. It summarizes the steps you must take to do basic I/O to and from files, with only a tiny bit of explanation. It is not a replacement for reading

More information

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments Reading C Programming Language Types, operators, expressions Control flow, functions Basic IO K&R Chapter 2 Types, Operators, and Expressions K&R Chapter 3 Control Flow K&R Chapter 7 Basic I/O NEWS Assignment

More information

File Handling. What is a file?

File Handling. What is a file? File Handling 1 What is a file? A named collection of data, stored in secondary storage (typically). Typical operations on files: Open Read Write Close How is a file stored? Stored as sequence of bytes,

More information

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements: 1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression)

More information

Standard C Input/Output. Output: printf() Table of Contents

Standard C Input/Output. Output: printf() Table of Contents Standard C Input/Output 1 Output: printf() 2 Table of Contents Output: printf( ) - syntax & sematics Output: printf( ) - examples Output: printf( ) - format control Screen / Printer Control Input: scanf(

More information

STRINGS. If you follow the rule of array initialization then you can write the above statement as follows:

STRINGS. If you follow the rule of array initialization then you can write the above statement as follows: STRINGS The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise

More information

Format String Vulnerability. printf ( user input );

Format String Vulnerability. printf ( user input ); Lecture Notes (Syracuse University) Format String Vulnerability: 1 Format String Vulnerability printf ( user input ); The above statement is quite common in C programs. In the lecture, we will find out

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

More information

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006 Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

More information

Data Structure with C

Data Structure with C Subject: Data Structure with C Topic: Strings In this chapter we are emphasizing the reading and storing string and also manipulation concepts through the following millstones. The discussion starts with

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

More information

Overview of a C Program

Overview of a C Program Overview of a C Program Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University Programming with C (CSCI 112) Spring 2015 2 / 42 C Language Components Preprocessor Directives

More information

Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type

Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type Program Control Essentials of repetition Loop control variable Counts the number of repetitions in the counter-controlled loop Controls the execution of loop Sentinel value Indicates the end of data when

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

CMPE-013/L. Introduction to C Programming

CMPE-013/L. Introduction to C Programming CMPE-013/L Introduction to C Programming Gabriel Hugh Elkaim Winter 2015 Text I/O 1 Text I/O Within : Formatted text: scanf()/printf() Characters: getchar()/ putchar() Strings/Lines: fgets()/puts()

More information

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad

STRINGS. PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad STRINGS PREPAED BY T.DEVI IT DEPARTMENT Vidya Jyothi Institute of Technology. Hyderabad Definition Array of character are called strings. A string is terminated by null character /0. For example: "Vjit

More information

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

More information

MPI and C-Language Seminars 2010

MPI and C-Language Seminars 2010 MPI and C-Language Seminars 2010 Seminar Plan (1/3) Aim: Introduce the C Programming Language. Plan to cover: Basic C, and programming techniques needed for HPC coursework. C-bindings for the Message Passing

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

String Processing in C

String Processing in C String Processing in C C Programming and Software Tools N.C. State Department of Computer Science Standard Library: Many functions for checking whether a character is a digit, is upper case,

More information

C Programming under Linux

C Programming under Linux C Programming under Linux P2T Course, Semester 1, 2006 7 C Lecture 8 Dr Ralf Kaiser Room 514, Department of Physics and Astronomy University of Glasgow r.kaiser@physics.gla.ac.uk P2T: C Lecture 8 p.1/23

More information

J a v a Quiz (Unit 3, Test 0 Practice)

J a v a Quiz (Unit 3, Test 0 Practice) Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points

More information

C Programming Dr. Hasan Demirel

C Programming Dr. Hasan Demirel C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

More information

6.087 Lecture 1 January 11, 2010

6.087 Lecture 1 January 11, 2010 6.087 Lecture 1 January 11, 2010 Introduction to C Writing C Programs Our First C Program 1 What is C? Dennis Ritchie AT&T Bell Laboratories 1972 16-bit DEC PDP-11 computer (right) Widely used today extends

More information

As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Encoding of alphanumeric and special characters As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric

More information

String Representation in C

String Representation in C String Representation in C 1 There is no special type for (character) strings in C; rather, char arrays are used. char Word[7] = "foobar"; Word[6] Word[5] Word[4] Word[3] Word[2] Word[1] Word[0] 'f' 'o'

More information

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging? Freshers Club Important 100 C Interview Questions & Answers 1) How do you construct an increment statement or decrement statement in C? There are actually two ways you can do this. One is to use the increment

More information

System Calls and Standard I/O

System Calls and Standard I/O System Calls and Standard I/O Professor Jennifer Rexford http://www.cs.princeton.edu/~jrex 1 Goals of Today s Class System calls o How a user process contacts the Operating System o For advanced services

More information

6.087 Lecture 5 January 15, 2010

6.087 Lecture 5 January 15, 2010 6.087 Lecture 5 January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String

More information

1 Problem Description

1 Problem Description CSCE2014 Programming Foundations II Homework One By Wing Ning Li 1 Problem Description Implement the standard library function strtok using C++/C basic language constructs. In concept, this assignment

More information

Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

More information

COMP 321: Introduction to Computer Systems

COMP 321: Introduction to Computer Systems Assigned: 1/21/16, Due: 2/4/16, 11:55 PM Important: This project must be done individually. Be sure to carefully read the course policies for assignments (including the honor code policy) on the assignments

More information

1 Problem Description

1 Problem Description CSCE2014 Programming Foundations II Lab Two (week 9-9 to 9-13) By Wing Ning Li 1 Problem Description We have introduced and studied the concept of pointer variable and learned that the name of an array

More information

3/13/2012. Esc101: Strings. String input and output. Array of characters: String

3/13/2012. Esc101: Strings. String input and output. Array of characters: String Esc101: Strings Instructor: Krithika Venkataramani Semester 2, 2011-2012 The contents of most of these slides are from the lecture slides of Prof. Arnab Bhattacharya 1 2 Array of characters: String String

More information

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT-

More information

MIT Aurangabad FE Computer Engineering

MIT Aurangabad FE Computer Engineering MIT Aurangabad FE Computer Engineering Unit 1: Introduction to C 1. The symbol # is called a. Header file c. include b. Preprocessor d. semicolon 2. The size of integer number is limited to a. -32768 to

More information

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started CIS 190: C/C++ Programming Lecture 1 Introduction and Getting Started This course will teach you the basics of C and C++ give you more programming experience be appropriate for majors and non-majors not

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Common to: CSE 2 nd sem IT 2 nd sem Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart

More information

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C Basic Java Constructs and Data Types Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C 1 Contents Hello World Program Statements Explained Java Program Structure in

More information

C Programming Language CIS 218

C Programming Language CIS 218 C Programming Language CIS 218 Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language constructs that map efficiently to machine instructions,

More information

Chapter 13 - The Preprocessor

Chapter 13 - The Preprocessor Chapter 13 - The Preprocessor Outline 13.1 Introduction 13.2 The#include Preprocessor Directive 13.3 The#define Preprocessor Directive: Symbolic Constants 13.4 The#define Preprocessor Directive: Macros

More information

Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements

Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements Programming Language: Syntax Introduction to C Language Overview, variables, Operators, Statements Based on slides McGraw-Hill Additional material 2004/2005 Lewis/Martin Modified by Diana Palsetia Syntax

More information

Common C Errors. Compiled by: Leela Kamalesh Yadlapalli

Common C Errors. Compiled by: Leela Kamalesh Yadlapalli Common C Errors Compiled by: Leela Kamalesh Yadlapalli This document shows some of the common errors and warnings that you may encounter during this class. Always remember to use the Wall option if you

More information

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

More information

High-level programming (Ch 11-14)

High-level programming (Ch 11-14) High-level programming (Ch 11-14) hardware software H/w s/w interface Problems Algorithms Prog. Lang & Interfaces Instruction Set Architecture Microarchitecture (Organization) Circuits Devices (Transistors)

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class: L&L, 2.1-2.3, App C 1 Primitive Data There are eight

More information

Lecture 5: Java Fundamentals III

Lecture 5: Java Fundamentals III Lecture 5: Java Fundamentals III School of Science and Technology The University of New England Trimester 2 2015 Lecture 5: Java Fundamentals III - Operators Reading: Finish reading Chapter 2 of the 2nd

More information

Programming for MSc Part I

Programming for MSc Part I Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

More information

Control structures: Conditionals

Control structures: Conditionals Control structures: Conditionals Leo Ferres Department of Computer Science Universidad de Concepción leo@inf.udec.cl April 5, 2011 1a 1 Controlling the flow of a program: if... then... else In order to

More information

The Basics of C Programming. Marshall Brain

The Basics of C Programming. Marshall Brain The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types In this lecture Computer Languages Assembly Language The compiler Operating system Data and program instructions Bits, Bytes and Data Types ASCII table Data Types

More information

60-141 Introduction to Programming II Winter, 2014 Assignment 2

60-141 Introduction to Programming II Winter, 2014 Assignment 2 60-141 Introduction to Programming II Winter, 2014 Assignment 2 Array In this assignment you will implement an encryption and a corresponding decryption algorithm which involves only random shuffling of

More information

Outline. Strings. String Literals. Strings in C. Duplicate String Literals. Referring to String Literals

Outline. Strings. String Literals. Strings in C. Duplicate String Literals. Referring to String Literals Strings A string is a sequence of characters treated as a group We have already used some string literals: filename output string Strings are important in many programming contexts: names other objects

More information

C Syntax and Semantics

C Syntax and Semantics C Syntax and Semantics 1 C Program Structure C Language Elements Preprocessor directives Function Header, and Function body Executable statements Reserved word, Standard identifiers, user defined identifiers

More information

C programming. Intro to syntax & basic operations

C programming. Intro to syntax & basic operations C programming Intro to syntax & basic operations Example 1: simple calculation with I/O Program, line by line Line 1: preprocessor directive; used to incorporate code from existing library not actually

More information

Advanced Bash Scripting. Joshua Malone (jmalone@ubergeeks.com)

Advanced Bash Scripting. Joshua Malone (jmalone@ubergeeks.com) Advanced Bash Scripting Joshua Malone (jmalone@ubergeeks.com) Why script in bash? You re probably already using it Great at managing external programs Powerful scripting language Portable and version-stable

More information

C Examples! Jennifer Rexford!

C Examples! Jennifer Rexford! C Examples! Jennifer Rexford! 1 Goals of this Lecture! Help you learn about:! The fundamentals of C! Deterministic finite state automata (DFA)! Expectations for programming assignments! Why?! The fundamentals

More information

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal. Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be

More information

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

More information

Common Errors in C. David Chisnall. February 15, 2011

Common Errors in C. David Chisnall. February 15, 2011 Common Errors in C David Chisnall February 15, 2011 The C Preprocessor Runs before parsing Allows some metaprogramming Preprocessor Macros Are Not Functions The preprocessor performs token substitution

More information

C A short introduction

C A short introduction About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

More information

Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays. Arrays, Argument Passing, Promotion, Demotion Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

More information

Passing 1D arrays to functions.

Passing 1D arrays to functions. Passing 1D arrays to functions. In C++ arrays can only be reference parameters. It is not possible to pass an array by value. Therefore, the ampersand (&) is omitted. What is actually passed to the function,

More information

C Control Statements : Branching and jumps

C Control Statements : Branching and jumps C Control Statements : Branching and jumps Using Bloodshed Dev-C++ Heejin Park Hanyang University 2 Introduction The if Statement Adding else to the if Statement Let's Get Logical A Word-Count Program

More information

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++ Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

More information

C AND C++ PROGRAMMING

C AND C++ PROGRAMMING C AND C++ PROGRAMMING Bharathidasan University A Courseware prepared by University Informatics Centre Part I - Programming in C Getting Started This courseware is intended to be an introduction to C programming

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

Supplement V.F: C-Strings. For Introduction to C++ Programming By Y. Daniel Liang

Supplement V.F: C-Strings. For Introduction to C++ Programming By Y. Daniel Liang Supplement V.F: C-Strings For Introduction to C++ Programming By Y. Daniel Liang NOTE 7.8 gave a brief introduction on C-string from the array s perspectives. This supplement provides a detailed coverage

More information

Introduction to Programming Block Tutorial C/C++

Introduction to Programming Block Tutorial C/C++ Michael Bader Master s Program Computational Science and Engineering C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers

More information

Comp151. Definitions & Declarations

Comp151. Definitions & Declarations Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const

More information

Lecture 4 Notes: Arrays and Strings

Lecture 4 Notes: Arrays and Strings 6.096 Introduction to C++ January 10, 2011 Massachusetts Institute of Technology John Marrero Lecture 4 Notes: Arrays and Strings 1 Arrays So far we have used variables to store values in memory for later

More information