#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.

Slides:



Advertisements
Similar presentations
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Computer Science 210 Computer Organization Introduction to C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int a=0; int main(void) { printf(“Hello World\n”); return.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
Introduction to Programming
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CSE1301 Computer Programming: Lecture 6 Input/Output.
Minimal standard C program int main(void) { return 0 ; }
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
C is a high level language (HLL)
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Lecture2.
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
ECE Application Programming
C Programming Tutorial – Part I
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
ICS103 Programming in C Lecture 3: Introduction to C (2)
Programming in C Input / Output.
Computer Science 210 Computer Organization
#include <stdio.h> int main(void) { printf("Hello, world!\n");
Lexical Elements, Operators, and the C Cystem
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lexical Elements, Operators, and the C Cystem
Introduction to C Topics Compilation Using the gcc Compiler
#include <stdio.h> int main(void) { printf("Hello, world!\n");
UMBC CMSC 104 – Section 01, Fall 2016
Outline Defining and using Pointers Operations on pointers
Lecture3.
Programming in C Input / Output.
Formatted Input/Output
Introduction to C Topics Compilation Using the gcc Compiler
An Overview of C.
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor indicates where to look for printf() function.h file is a header file belongs to stdio.h “Hello....” is a parameter to printf()

Marathon Distance Program convert the distance to kilometers –1 mile = km = 1760 yards –we know that the marathon length is 26 miles and 385 yards, then what is it in kilometers? the answer is

/* The distance of a marathon in kilometers. */ #include int main(void) { int miles, yards; float kilometers; miles = 26; yards = 385; kilometers = * (miles + yards / ); printf(“\nA marathon is %f kilometers.\n\n", kilometers); return 0; } comment declaration of variables assignment expression

Preprocessor performs before compilation # indicates that this line is a directive #define for symbolic constants #define PI #define YARDS_PER_MILE 1760 #include imports a header file from some where #include “file-name” from your directory

#include #define AREA 2337 #define SQ_MILES_PER_SQ_KILOMETER #define SQ_FEET_PER_SQ_MILE (5280 * 5280) #define SQ_INCHES_PER_SQ_FOOT144 #define ACRES_PER_SQ_MILE 640 /* Measuring the Pacific Sea. */ #include "pacific_sea.h" int main(void) { const int pacific_sea = AREA; /* in sq kilometers */ double acres, sq_miles, sq_feet, sq_inches; printf("\nThe Pacific Sea covers an area"); printf(" of %d square kilometers.\n", pacific_sea); sq_miles = SQ_MILES_PER_SQ_KILOMETER * pacific_sea; pacific_sea.h pacific_sea.c

I/O Using stdio.h printf(“any string or characters %d %f”, a, b); –“ “ indicates a format to be displayed –% is followed by a single character for a format c (char), d (decimal), e (exponential), f(floating), s (string) –escape with \ \n, \t, \”, \\ scanf(“%d”, &age); –takes something from the standard input, and interpret as a decimal

#include int main(void) { char c1, c2, c3; int i; float x; double y; printf("\n %s \n %s", "Input three characters," "an int, a float, and a double: "); scanf("%c %c %c %d %f %lf", &c1, &c2, &c3, &i, &x, &y); printf("\nHere is the data that you typed in:\n"); printf("%3c%3c%3c%5d%17e%17e\n\n", c1, c2, c3, i, x, y); return 0; }

Control Flow each statement is executed one by one sequentially special statements change the flow –if (expr) a single statement OR { statements } –while (expr) a single statement OR –for (expr1; expr2; expr3) a single statement OR expr1; while (expr2) { statement expr3; }

#include int main(void) { int i = 1, sum = 0; while (i <= 5) { sum += i; ++i; } printf("sum = %d\n", sum); return 0; }

Arrays deal with multiple same type data int xxx[3]; –declares 3 integers; xxx[0], xxx[1], xxx[2] int i; i = 2; xxx[i] = xxx[0] + 79; a string “abc” abc\0

Pointer address is a location in the imaginary space –an array name int age[100]; char *p; int *pq;

Functions Can you write a program of 10,000 lines in a single file? –divide your whole code into many small chunks –some chunks may look similar make them into a single one; how? this is a function main() is a special function called by....

#include float maximum(float x, float y); float minimum(float x, float y); void prn_info(void); int main(void) { int i, n; float max, min, x; prn_info(); printf("Input n: "); scanf("%d", &n); printf("\nInput %d numbers:", n); scanf("%f", &x); max = min = x; for (i = 2; i <= n; ++i) { scanf("%f", &x); max = maximum(max, x); min = minimum(min, x); } float maximum(float x, float y) { if (x > y) return x; else return y; } float minimum(float x, float y) { if (x < y) return x; else return y; } void prn_info(void) { printf("\n%s\n%s\n\n", "This program reads an integer value for n, and then", "processes n real numbers to find max and min values."); }

#include int main(void) { int a = 1; void try_to_change_it(int); printf("%d\n", a); /* 1 is printed */ try_to_change_it(a); printf("%d\n", a); /* 1 is printed again! */ return 0; } void try_to_change_it(int a) { a = 777; }

Files you need files, believe me. xfp = fopen(“file-name”, “r”); –checks if the file is available –prepares a pointer, xfp, to a location inside a file now read from (write to) the file using the pointer c = getc(xfp); n = fscanf, “%d %d %f %s”, i, j, x, name);

/* Count uppercase letters in a file. */ #include int main(int argc, char *argv[]) { int c, i, letter[26]; FILE *ifp, *ofp; ifp = fopen(argv[1], "r"); ofp = fopen(argv[2], "w"); for (i = 0; i < 26; ++i) /* initialize array to zero */ letter[i] = 0; while ((c = getc(ifp)) != EOF) if (c >= 'A' && c <= 'Z') /* find uppercase letters */ ++letter[c - 'A']; for (i = 0; i < 26; ++i) { /* print results */ if (i % 6 == 0) putc('\n', ofp); fprintf(ofp, "%c:%5d ", 'A' + i, letter[i]); } putc('\n', ofp); return 0; }