Sscanf example #include int main () { char sentence []="Rudolph is 12 years old"; char str [20]; int i; sscanf (sentence,"%s %*s %d",str,&i); printf ("%s.

Slides:



Advertisements
Similar presentations
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Advertisements

תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב'
1 מבוא למדעי המחשב משתנים. 2  סוגי משתנים בשפת C  ההבדלים בין סוגי המשתנים השונים.
תכנות תרגול 2 שבוע : שבוע שעבר כתבו תוכנית המגדירה שלושה משתנים מאתחלת אותם ל 1 2 ו 3 ומדפיסה את המכפלה שלהם את ההפרש שלהם ואת הסכום שלהם.
1 ICS103 Programming in C Lecture 17: Array of Strings.
CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
מבוא כללי למדעי המחשב תרגול 3. לולאות while לולאות while while (condition) { loop body } במקרה של קיום התנאי מתבצע גוף הלולאה ברגע שהתנאי לא מתקיים נצא.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Outline Midterm results Static variables Memory model
C Programming Tutorial – Part I CS Introduction to Operating Systems.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Tutorial ICS431 – Introduction to C.
Computer Science 210 Computer Organization Arrays.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
1 C Programming Week 2 Variables, flow control and the Debugger.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
How to design and code functions Chapter 4 (ctd).
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Computer Programming for Engineers
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
1 נתבונן בפונקציה הבאה public static int min(int[] a,int n) { int min = a[0]; for (int i = 1; (i < n ) && (i < a.length) ; i++) if (min > a[i]) min = a[i];
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
 Recursion  Pointers and Arrays #include void print3(int n){ if (n==0)return; printf ("%d\n",n); print3(n-1); printf ("%d\n",n); } void main(){ print3(5);
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
C language--Introduction. History 1970: B by Ken Thompson at AT&T Bell Lab 1972: C by Dennis Ritchie and Ken Tompson at At&T Bell Lab for UNIX 1978: “The.
Lecture 3: Getting Started & Input / Output (I/O)
C Programming.
C Programming.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Formatted Input/Output
C Programming Tutorial – Part I
Chapter 2 Overview of C.
Module 2 Arrays and strings – example programs.
Formatted Input/Output
Arrays in C.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
OUTPUT STATEMENTS GC 201.
מבוא כללי למדעי המחשב תרגול 2
Computer Science 210 Computer Organization
Dynamic memory allocation and Intraprogram Communication
توابع ورودي-خروجي.
מ- C++ ל- C קרן כליף.
סוגי משתנים קרן כליף.
Counting Loops.
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
Functions, Part 2 of 3 Topics Functions That Return a Value
Engineering Programming A
A function with one argument
Introduction to Computer Organization & Systems
Introduction to Computer Organization & Systems
Strings #include <stdio.h>
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

sscanf example #include int main () { char sentence []="Rudolph is 12 years old"; char str [20]; int i; sscanf (sentence,"%s %*s %d",str,&i); printf ("%s -> %d\n",str,i); return 0; } Rudolph -> 12

int main( void ){ char tokenstring[] = " "; char s[81]; char c; int i; float fp; sscanf( tokenstring, "%80s", s ); // max 80 character string sscanf( tokenstring, "%c", &c ); sscanf( tokenstring, "%d", &i ); sscanf( tokenstring, "%f", &fp ); /* Output the data read */ printf( "String = %s\n", s ); printf( "Character = %c\n", c ); printf( "Integer: = %d\n", i ); printf( "Real: = %f\n", fp ); } String = 15 Character = 1 Integer: = 15 Real: =

int main(){ char my_string[100],str[30]; int int1, int2; int args_assigned = 0 ; while (args_assigned != 3) { puts ("Please enter two integers separated by whitespace and follo by your name."); gets(my_string); args_assigned = sscanf (my_string, "%d %d %s", &int1, &int2, str); if (args_assigned != 3) puts ("\nInput invalid!"); { printf ("\nThanks!\n%d\n%d\n%s\n", int1, int2, str); return 0; {

משתנים סטטיים – משתנים סטטים מאותחלים רק פעם אחת וערכם לא נהרס., אורך חיים : מרגע איתחולם ועד סיום ריצת התכנית. משתנים סטטים מאותחלים רק פעם אחת וערכם לא נהרס., אורך חיים : מרגע איתחולם ועד סיום ריצת התכנית. לעתים מעונינים להקנות למשתנים מקומיים אורך חיים למשך ביצוע התכנית כולה, אך לשמר את תכונת הפרטיות שלהם מבחינת טווח ההכרה. משתנה סטטי הוא משתנה שטווח הכרה שלו בתוך הפונקציה שבו הוגדר, ואורך החיים שלו כל זמן ריצת התכנית. נשתמש במשתנה סטטי כאשר ברצוננו לשמור על הערך שהתקבל בו בזמן ביצוע הפונקציה, עבור הקריאה הבאה לאותה פונקציה. טווח הכרה (scope): בתוך הבלוק בו הוגדרו. שימו לב להבדל בין " טווח הכרה " ו - " אורך חיים ".

void func() { static int x = 0; printf("%d\n", x); // outputs the value of x x = x + 1; } int main() { // x is initialized only once across three calls of func() func(); // prints 0 func(); // prints 1 func(); // prints 2 return 0; }

Program which sums integers, using static variables void sumIt() { static int sum = 0; int num; printf("\nEnter a number: "); scanf("%d", &num); sum+=num; printf("The current sum is: %d",sum); } int main() { int i =0; printf("Enter 5 numbers to be summed\n"); for(i = 0; i<5; ++i) sumIt(); return 0; }