BİL200 TUTORIAL-EXERCISES Objective:

Similar documents
Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Programming and Data Structure

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Introduction to C Language

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Lecture 2: C Programming Basic

Arithmetic Expressions in C

Fundamentals of Programming

Fundamental of Programming (C)

Full file at C How to Program, 6/e Multiple Choice Test Bank

Computers Programming Course 5. Iulian Năstac

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Advanced C Programming Topics


Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

CS16 Exam #1 7/17/ Minutes 100 Points total

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Introduction to Computing Lecture 03: Basic input / output operations

Binghamton University. CS-220 Spring Includes & Streams

Lesson 7. Reading and Writing a.k.a. Input and Output

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Preview from Notesale.co.uk Page 2 of 79

CS 108 Computing Fundamentals. October/November Array Bootcamp

Computer System and programming in C

Strings. Daily Puzzle

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Stream Model of I/O. Basic I/O in C

CSCI 171 Chapter Outlines

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

This is CS50. Harvard University Fall Quiz 0 Answer Key

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS201 - Introduction to Programming Glossary By

Introduction to C. Systems Programming Concepts

Unit 4 Preprocessor Directives

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Pointers, Arrays, and Strings. CS449 Spring 2016

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

UIC. C Programming Primer. Bharathidasan University

Object oriented programming C++

Solutions to Assessment

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

LESSON 4. The DATA TYPE char

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Intermediate Programming, Spring 2017*

These are reserved words of the C language. For example int, float, if, else, for, while etc.

BSM540 Basics of C Language

COSC2031 Software Tools Introduction to C

C introduction: part 1

OBJECTIVE QUESTIONS: Choose the correct alternative:

CSSE 332 Standard Library, Storage classes, and Make

Programming Language B

Programming in C Quick Start! Biostatistics 615 Lecture 4

Simple Output and Input. see chapter 7

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Computational Methods of Scientific Programming Fall 2007

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Fundamentals of Programming

C mini reference. 5 Binary numbers 12

Short Notes of CS201

1. The keyword main in C language is used for

Library Functions. General Questions

Fundamental of Programming (C)

WARM UP LESSONS BARE BASICS

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Chapter 8 C Characters and Strings

Array Initialization

Functions. Systems Programming Concepts

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CSCI 2132 Final Exam Solutions

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Programming for Engineers Introduction to C

Dynamic memory allocation

CS201 Some Important Definitions

ONE DIMENSIONAL ARRAYS

File I/O. Preprocessor Macros

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

ALGORITHM 2-1 Solution for Exercise 4

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CSE 5A Final Fall 2006

COMPUTER APPLICATION

Programming Language Basics

A Fast Review of C Essentials Part I

Technical Questions. Q 1) What are the key features in C programming language?

Transcription:

Objective: The purpose of this tutorial is learning the usage of -preprocessors -header files -printf(), scanf(), gets() functions -logic operators and conditional cases A preprocessor is a program that processes its input data to produce output to be used as an input for another program. The C preprocessor is the preprocessor for the C programming language. In many C implementations, C preprocessors are separate programs invoked by the C compiler as the first part of translation. The preprocessors handle directives for source file inclusion ( include), macro definitions ( define) and conditional inclusion ( if). Lines that begin with are called preprocessing directives. For example, if the lines #define XVal 100 #define YVal 200 occur in a file that is being compiled, the preprocessor changes the value of XVal to 100 and the value of YVal to 200. The identifiers XVal and YVal are called symbolic constants. An important property of define is; it can be written anywhere in the program but it should be noted that it only affects the lines written after it. The contents of the quoted strings are never changed by the preprocessors. The language of preprocessing directives is not strictly specific to the grammar of C, so the C preprocessor can also be used independently to process other types of files. #include "myheader1.h" is a preprocessing directive which exists at the head of the file and copies the contents of myheader1.h to the file when compilation occurs. An include file, which is also called as a header file, can contain define lines and other include files. The C system provides a number of standard header files. Some examples are stdio.h, string.h, math.h,.... These files contain the declarations of functions in the standard library, macros, structure templates, and other programming elements that are commonly used. The preprocessing directive #include "stdio.h" causes a copy of the standard header file stdio.h to be included in the code when compilation occurs. In ANSI C, whenever the function printf() or scanf() are used, the standard header file stdio.h should be included. This file contains the declarations, or more specifically, the function prototypes, of these functions.

Example 1: Usage of the Header File Header File (myheader1.h): #include "stdio.h" #include "math.h" #include "stdlib.h" #define A 5 #define Pi 3.14 C Source File (example1.c): #include "myheader.h" main() double c; c=pi*a; printf("new Pi value = %f",c); The printf() function is part of the standard C library and is used for output. The arguments of this function are control strings and other arguments, where control string is a string and may contain specifiers. The specifiers differ for different arguments. The prototype of printf function is below; printf("some words %d %f some words etc.",var1,var2,...); where var1 and var2 are arguments. The string may be consist of not only character literals but also format specifiers that control the output of other arguments. Commonly used conversion characters (Specifiers) are given below: The scanf() function is analogous to the function printf() but is used for input rather than output. Its first argument is control string and contains only specifiers. The other arguments are addresses. Its prototype is: scanf("%d %f possible specifiers",&variable1,&variable2,...);

scanf() reads from standard input and converts each argument as specified in the format string. The arguments after the format string must be addresses of variables such as (&var). The function gets() reads characters from standard stream for input (stdin) and stores the characters as a string. The storing process continues until a newline character ( \n )or the Endof-File is reached. The ending newline character ( \n ) is not included in the string. A null character ( \0 ) is automatically appended after the last character copied to str to signal the end of the C string. Notice that, gets() does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows. Example 2: In this example you are invited to assign a character, a string, an integer and a floating number. Then, your assignments are printed on the screen. #include <stdio.h> void main() int a1, a2; float b1,b2; char c1,c2; char mystring[21]; a1=754; b1=457.5; c1='s'; printf("assign a character and assign a string:"); scanf("%c",&c2); // Character Assignment gets(mystring); // String Assignment printf("assign an Integer:"); scanf("%d",&a2); // Integer Assignment printf("assign a Floating number:"); scanf("%f",&b2); // Float Assignment /* The character \t is used for placing a space */ printf("\n a1=%d \t Your integer assignment=%d\n",a1,a2); printf("\n b1=%2.3f \t Your floating number assignment=%f\n",b1,b2); printf("\n c1=%c \t Your character assignment=%c\n",c1,c2); printf("\n Your string assignment: %s\n",mystring); printf("\n\n"); printf("congratulations!\t"); printf("example 2 is completed\n\n");

Relational, equality and logic operators are used to express the conditions in C programming. These operators return binary bits which indicate whether the comparison is true or not. If the comparison is true, it returns 1, otherwise it returns 0. The and and/or or operators are used to check the multiple conditions. And (&&) operator combines two conditions, and results true if both conditions are true. If one of the condition is false, then the result is false. Or ( ) operator combines two conditions, and results true if at least one of the conditions is true. If both conditions are false, then the result is false. Some of the relational and equality operators are listed below: If statement is used in conditional cases. When the execution of a statement depends on a desired condition, if() structure is used. The statement which is desired to be executed is written after if (condition). In the case, the condition written in the if() is satisfied, the desired statement is executed. Otherwise, program skips to the next statement.

Exercises: i) Write a program that prints out the below structure ************************************* **My Name is ******* **My Surname is ******* **My ID # is ******* ************************************* ii) Write a program that asks the user below questions and wants their replies ******** ID Documentation *********** 1) What is your Name? ******* 2) What is your Surname? ******* 3) What is your Address? ******* **************************************** iii) Guess the outputs of the following program without writing the codes in the case that the integers you assigned are, a) 50 and 50 (i.e. a=50 and b=50). b) 100 and123 (i.e. a=100 and b=123). c) 130 and 20(i.e. a=130 and b=20). #include <stdio.h> int main() int a,b,c; printf("assign two integers seperating them with a space:"); scanf("%d %d",&a, &b); c=a+b; if (c>150) printf("\n Summation is greater than 150\n"); printf("\n Summation is %d\n", c); if (c<=150) printf("\n Summation is less than or equal to 150\n");