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

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 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 strings together. It takes the following form: strcat(string1, string2); where string1 and string2 are character arrays. When the function strcat is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. The string at string2 remains unchanged. a = Hello ; b = World ; strcat(a,b); Now, the string a becomes HelloWorld ; strcpy() Function: The strcpy function works almost like a string-assignment operator which copies one string over another. It takes the following form: strcpy(string1, string2); which assigns the contents of string2 to string1. The statement, strcpy(city, Delhi ); will assign the string Delhi to the string variable city. strncmp() Function: It is a variation of the function strcmp. This function has three parameters in the function call as shown below: strncmp(s1,s2,n); This compares the left-most n characters of s1 to s2 and returns a) 0, if they are equal b) Negative number, if s1 sub-string is less than s2 c) Positive number, otherwise strstr() Function: It is a two parameter function that can be used to locate a sub-string in a string. This takes the following form: strstr (s1, s2); strstr (s1, ABC ); The function strstr searches the string s1 to see whether the string s2 is contained in s1. If yes, the function returns the position of the first occurrence of the sub-string. Otherwise, it returns a NULL pointer.

2 2. Explain getchar() and gets() functions with suitable example. [4] getchar() Function: This function is used for taking a single character as input from standard input device. The general form of getchar() is: int getchar(void); It returns the unsigned char that they read. If end-of-file or an error is encountered getchar() functions return EOF. #include <stdio.h> void main () char c; printf("enter character: "); c = getchar(); printf("character entered: "); putchar(c); gets() Function: It is used to scan a line of text from a standard input device. The gets() function will be terminated by a newline character. The newline character won't be included as part of the string. The string may include white space characters. The general form of gets is: char *gets(char *s); The argument must be a data item representing a string. On successful completion, gets() shall return a pointer to string s. #include <stdio.h> char str[50]; printf("enter a string : "); gets(str); printf("you entered: %s", str); 3. Explain putchar() and puts() functions with suitable example [4] putchar() Function: putchar function displays a single character on the screen. The general form of putchar() is: int putchar(int c); #include <stdio.h> void main () char ch; for(ch = 'A' ; ch <= 'Z' ; ch++) putchar(ch);

3 puts() Function: It is used to display a string on a standard output device. The puts() function automatically inserts a newline character at the end of each string it displays, so each subsequent string displayed with puts() is on its own line. puts() returns non-negative on success, or EOF on failure. #include <stdio.h> char str[50]; printf("enter a string : "); gets(str); printf( The input string is: ); puts(str); 4. Write user defined functions for the following: (i) strlen() : to find length of string. [7] int find_length(char str[]) int len = 0; while( str[len]!= \0 ) len++; return(len); (ii) strcat() : to concate two strings char * concatenation(char a[], char b[]) int i, j; for( i=0; a[i]!= \0 ; i++) for(j=0; b[j]!= \0 ; j++) a[i++] = b[j]; a[i] = \0 ; return a;

4 5. Which are various categories of functions in C. [7] Depending on whether arguments are present or not and whether a value is returned or not, a function may belong to one of the following categories: Category 1: Functions with no arguments and no return values. Category 2: Functions with arguments and no return values. Category 3: Functions with arguments and one return values. Category 4: Functions with no arguments but return values. Category 5: Functions that return multiple values. No arguments and No return values When a function has no arguments, it does not receive any data from the calling function. Similarly when it does not return a value, the calling function does not receive any data from the called function. In effect, there is no data transfer between the calling function and the called function. void funct(); funct(); void funct() printf("hi, this is display from function funct()"); Arguments but No return values We could make the calling function to read data from the terminal and pass it on to the called function. In this type of functions there is a one-way data communication from the calling function to the called function. void funct(int, int); int a,b; printf("enter two numbers :"); scanf("%d %d",&a, &b); funct(a,b); funct(int a, int b) int c; c=a+b; printf("\n Result = %d",c);

5 Arguments with one return values We may not always wish to have the result of a function displayed. We may use it in the calling function for further processing. This category of functions receive values from calling function, does some calculations/ manipulations and sends one of the values back to the calling function. int funct(int, int); int a,b,result; printf("enter a value for a :"); scanf("%d",&a); printf("enter a value for b :"); scanf("%d",&b); result=funct(a,b); printf("\n Result = %d",result); int funct(int a1, int b1) int c; c=a1+b1; return(c); No arguments but return a values In this category of functions, no value is passed from the calling function to the called function but a value is send back to the calling function by the called function. int get_number(); int n=get_number(); printf( %d,n); int get_number() int number; scanf( %d,&number); return number; Functions with multiple return values We use this type of functions when we want to send back more information from the called function to the calling function. The mechanism of sending back information through arguments is achieved using address operator (&) and indirection operator (*).

6 void mathoperation (int, int, int *, int *); int x=20, y=10, s, d; mathoperation(x, y, &s, &d) printf( s=%d \n d=%d,s,d); void mathoperation(int a, int b, int *sum, int *diff) *sum = a+b; *diff = a-b; 6. Explain the difference between call by value and call by reference with suitable examples. [7] Sr. No. Call by Value Call by Reference 1. This is the usual method to call a function in which only the value of the variable is passed as an argument 2. Any alternation in the value of the argument passed is local to the function and is not accepted in the calling program 3. Memory location occupied by formal and actual arguments are different 4. Since a new location is created, this method is slow In this method, the address of the variable is passed as an argument Any alternation in the value of the argument passed is accepted in the calling program(since alternation is made indirectly in the memory location using the pointer) Memory location occupied by formal and actual arguments are same Since the existing memory location is used through its address, this method is fast 5. More memory space is used Less memory space is used 6. There is no possibility of wrong data manipulation since the arguments are directly used in an application 7. void funct(int); int n; There is a possibility of wrong data manipulation since the addresses are used in an expression. A good skill of programming is required here void funct(int *); int n;

7 printf("enter a value for n :"); scanf("%d",&n); funct(n); funct(int a) printf("n = %d",a); printf("enter a value for n :"); scanf("%d",&n); funct(&n); funct(int *a) printf("n = %d",*a); 7. What is recursive function? Explain with suitable example. [7] A function that calls itself is known as recursive function and the process of calling function itself is known as recursion in C programming. While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop. Recursive functions are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc. //Program for finding Factorial of a number using recursion #include<stdio.h> #include<conio.h> int factorial(int); int n,f; printf("enter a number to find factorial\n"); scanf("%d", &n); f = factorial(n); printf("%d! = %d", n, f); int factorial(int n) if (n == 1) return 1; else return(n * factorial(n-1));

8 8. Write a function which receives number as argument and return sum of digit of that number. [5] int sum_of_digits(int n) int sum=0, remainder; while( n > 0 ) remainder = n % 10; sum = sum + remainder; n = n / 10; return sum; 9. Write a program to generate Fibonacci series of n given numbers using function named fibo. i.e Or Write a program to generate Fibonacci series of n given numbers using recursion. [7] //Program for printing Fibonacci Series using Recursion #include<stdio.h> #include<conio.h> int fibo(int); int n, i; printf("enter the number of terms\n"); scanf("%d",&n); printf("first %d terms of Fibonacci series are :-\n",n); for ( i = 1 ; i <= n ; i++ ) printf("%d\n", fibo(i)); int fibo(int n) if ( n == 1 n == 2 ) return 1; else return ( fibo(n-1) + fibo(n-2) );

### 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,

### Strings. A special kind of array is an array of characters ending in the null character \0 called string arrays

Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters char s[10] char p[30] When declaring a string

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

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 0.. +127. char variables mostly used to store characters

### 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

### 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

### 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

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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### z = x + y * z / 4 % 2-1

1.Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1? A. rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D. Remainder cannot be obtain

### Arrays and Pointers (part 1)

Arrays and Pointers (part 1) EECS 2031 25 September 2016 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 1 Arrays: Example

### 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

### 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

### 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

### LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */

LESSON 7 POINTERS AND CALL BY REFERENCE When an expression is passed as an argument to a function, a copy of the value of the expression is made, and it is this copy that is passed to the function. Suppose

### 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

### Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C

Outline ompiling, interpreting, and running Administrative trivia Goals of the class Introduction to 1 2 The Programming Language Java vs. Systems programming language Originally used to write Unix and

### Chapter 10 Character Strings and String Functions

Chapter 10 Character Strings and String Functions 1. String Constants 2. String Variables 3. String Input 4. String Output 5. String Functions 6. The ctype.h Character Functions 7. String to Number Conversion

### 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..............................

### 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

### 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

### 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

### 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

### M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: IMPORTANT INSTRUCTIONS: 1. Question Paper in English and Hindi and Candidate can choose any one language. 2. In case of discrepancies in

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(

### The main features of First Generation are:

MODEL SOLUTION AS -4016 B.Te ch (First Semester) course A INTRODUCTION TO COMPUTER PROGRAMMING ANS 1 : i. Electrically Erasable Programmable Read Only Memory ii. Four Generation iii. c iv. 8 v. High Level

### 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

### Programming and Data Structures

Programming and Data Structures Tutorial sheet: 2 Topics: Decision Making, Looping and Branching Q 1. (a) What is wrong with the following loop while ( n

### 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

### Lecture: #6. More About Characters, Strings, and the string Class

Lecture: #6 More About Characters, Strings, and the string Class 1 C-Strings Topics 2 Library Functions for Working with C-Strings 3 Conversions Between Numbers and Strings 4 Character Testing 5 Character

### Week 4 Assessment. Q : Consider the following code segment. Assume n to be a positive integer.

Week 4 Assessment Question 1 : Q : Consider the following code segment. Assume n to be a positive integer. for(i=1; i

### 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

### 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,

### File Handling. CS10001: Programming & Data Structures

File Handling CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 What is a file? A named collection of data, stored

### 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,

### Engineering Problem Solving with C++, Etter

Engineering Problem Solving with C++, Etter Chapter 7 Strings 12-11-13 1 Strings Character Strings The string Class. 2 C style strings functions defined in cstring CHARACTER STRINGS 3 C Style Character

### 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

### Programming. MRS.ANURADHA BHATIA M.E. Computer Engineering MSBTE G-SCHEME

Software Engineering MSBTE 2. Project Scheduling G-SCHEME 2015-2016 Programming in C FIRST YEAR SUBJECT CODE 17212 COMPUTER TECHNOLOGY COMPUTER ENGINEERING INFORMATION TECHNOLOGY MRS.ANURADHA BHATIA M.E.

### 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

### 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)

### 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

### Senem Kumova Metin & Ilker Korkmaz 1

Senem Kumova Metin & Ilker Korkmaz 1 A loop is a block of code that can be performed repeatedly. A loop is controlled by a condition that is checked each time through the loop. C supports two categories

### 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

### 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,

### MGM s JNEC Question Bank Subject: Computer Engineering

MGM s JNEC Question Bank Subject: Computer Engineering 1. All of the following are examples of computer input units EXCEPT: a) Scanner b) Speaker c) Bar code reader d) Keyboard Answer: b 2. Which of the

### Sorting. Arranging records according to a specified sequence, such as alphabetically or numerically, from lowest to highest.

Sorting Arranging records according to a specified sequence, such as alphabetically or numerically, from lowest to highest. Array before sort [0] [] Array after sort [0] [] 2 [2] [2] [] [] [4] [4] [5]

### 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

### Week 12 Character Array (String)

CME111 Programming Languages I Week 12 Character Array (String) Assist. Prof. Dr. Caner ÖZCAN String Definition We learned multidimensional arrays and arrays. String is actually an array of what we call

### Chapter 6: Basic I/O. 6.1 printf

Chapter 6: Basic I/O So far, we've been using printf to do output, and we haven't had a way of doing any input. In this chapter, we'll learn a bit more about printf, and we'll begin learning about character-based

### Module 816. File Management in C. M. Campbell 1993 Deakin University

M. Campbell 1993 Deakin University Aim Learning objectives Content After working through this module you should be able to create C programs that create an use both text and binary files. After working

A Linked List Example http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_094.htm /* linked list example */ #include #include #include #include #include

### Tutorial No. 5 - Solution (Decision Making and Branching)

Tutorial No. 5 - Solution (Decision Making and Branching) 1. Explain if...if ladder with flowchart [7] The if ladder is a way of putting together ifs together when multipath decisions are involved. A multipath

### an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage.

Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction

### Pseudocode. Pseudocode. Guide for Pseudocode. Computers in Engineering Pseudocode and C Language Review. Example Pseudocode.

Computers in Engineering Pseudocode and C Language Review Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms Pseudocode is similar to everyday English; it is

### 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

11 Operator Overloading; String and Array Objects The whole difference between construction and creation is exactly this: that a thing constructed can only be loved after it is constructed; but a thing

### FORMAT MEANING VARIABLE TYPE

Printf and Scanf Both formatted I/O Both sent to standard I/O location Printf Converts values to character form according to the format string Scanf Converts characters according to the format string,

### LAB 8: CHARACTER AND STRING

LAB 8: CHARACTER AND STRING Exercise 1: Character manipulation functions The following program demonstrates the usage of (predefined) character manipulation functions. In order to use the following functions,

### (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

### Arrays, strings, and functions

Arrays, strings, and functions Goals of this Lecture Helps you learn about: Arrays and strings Functions Recursive functions Some pointer concept, but we will defer the details to next lecture Subset of

### 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 (

### 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

### 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

### 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

### 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()

### Introduction to C. Memory Model. Instructor: Yin Lou 02/04/2011. Introduction to C CS 2022, Spring 2011, Lecture 6

Introduction to C Memory Model Instructor: Yin Lou 02/04/2011 Recap: Pointers int *ptr; Pointers are variables that store memory address of other variables Type of variable pointed to depends on type of

### 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,

### 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'

### Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

### 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

### 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

### Testing! Jennifer Rexford! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Testing! Jennifer Rexford! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6! 1 Quotations on Program Testing! On two occasions I have been

### COMPUTER PROGRAMMING THROUGH C LAB MANUAL

LAB MANUAL Name Roll No. Branch Section INDEX S. No Contents 1 Objectives of the lab 2 Requirements 3 Lab Syllabus Programs (JNTU) 4 Introduction About Lab 5 Solutions for Programs 6 Topics beyond the

### Solutions to Assessment: Basic Programming Constructs

Solutions to Assessment: Basic Programming Constructs Question 1: Specify the minimum number of comparisons required to find the largest number among a set of 3 integers X, Y and Z. We already know how

### C for Java Programmers

C for Java Programmers CS 414 / CS 415 Niranjan Nagarajan Department of Computer Science Cornell University niranjan@cs.cornell.edu Original Slides: Alin Dobra Why use C instead of Java Intermediate-level

### Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

2013 Computer Science 2nd Year Solved Exercise Chapter # 3 Programs Chapter # 4 Chapter # 5 Chapter # 6 Chapter # 7 Important Work Sheets Aumir Shabbir Pakistan School & College Muscat 3/4/2013 Chap #

### C File Input and Output (I/O) CSE303 Todd Schiller November 9, 2009

C File Input and Output (I/O) CSE303 Todd Schiller November 9, 2009 Lecture goal Build a practical toolkit for working with files 11/09/09 2 Files in C #include FILE object contains file stream

### 1. What does the following program print? #include <stdio.h> int main(void) { int a = 99; int b = 0; int c = 74;

1. What does the following program print? int a = 99; int b = 0; int c = 74; if( a b ) printf("first\n"); else printf("second\n"); if( a && c ) printf("third\n"); else printf("fourth\n"); if(!a ) printf("fifth\n");

### 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,

### 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

### Introduction to C Programming

Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

### C Programming 1. C Programming

C Programming 1 1 C Programming 1. Who developed the C language? Dennis M.Ritchie in 1972 2. What type of language is C? Semi-high level language 3. What is main()? The main() is a special function used

### 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

### TOPICS IN C PROGRAMMING

TOPICS IN C PROGRAMMING By Bob Hain (ME Net) Introduction This document is not intended to be a text on C programming. Because many of you may not have had the opportunity to use or practice C programming,

### Basic Common Unix commands: Change to directory d

Basic Common Unix commands: cd d Change to directory d mkdir d rmdir d mv f1 [f2...] d mv d1 d2 ls [d] [f...] ls -1 [f...] vi [f] emacs [f] more f cp f1 f2 mv f1 f2 rm f gcc [-o f1] f2 gnuplot Create new

### The Queue Data Structure in C++ By Eric Suh

The Queue Data Structure in C++ By Eric Suh http://www.cprogramming.com/tutorial/computersciencetheory/queue.html Queues are data structures that, like the stack, have restrictions on where you can add

### INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

### 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

### INTI COLLEGE MALAYSIA

CSC112 (F) / Page 1 of 5 INTI COLLEGE MALAYSIA CERTIFICATE IN COMPUTING AND INFORMATION TECHNOLOGY PROGRAMME CSC 112 : FUNDAMENTALS OF PROGRAMMING FINAL EXAMINATION : DECEMBER 2002 SESSION This paper consists

### 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,

### Lecture P1: Introduction to C

Learning to Program Lecture P1: Introduction to C Programming is learned with practice and patience. Don t expect to learn solely from these lectures. Do exercises. Experiment and write lots of code. printf("this

### Thinking in C. Darin Brezeale. March 25, 2010

Thinking in C Darin Brezeale March 25, 2010 NOTE: This is definitely a work in progress. 1 Introduction One of the most difficult parts of learning to program is knowing how to deconstruct a problem in