Follow
Publications: 90 | Followers: 1

CS1313 C Introduction Lesson

Publish on Category: Birds 0

C Introduction LessonCS1313 Fall 2019
1
C Introduction Lesson Outline
C Introduction Lesson Outlinehello_world.cC Character SetC is Case SensitiveCharacter String Literal ConstantString Literal Cannot Use Multiple LinesMulti-line String Literal ExampleNewlineNewline ExampleStatementsStatement TerminatorStandard Input & Standard Output
Block DelimitersWhat Is a Comment? #1What Is a Comment? #2Are Comments Necessary?hello_world.cwith Commentshello_world.cwithout CommentsFlowchart forhello_world.c
C Introduction LessonCS1313 Fall 2019
2
hello_world.c
/***************************************************** Program:hello_world****** Author: Henry Neeman (hneeman@ou.edu) ****** Course: CS 1313 010 Fall 2019 ****** Lab: Sec 014 Fridays 2:30pm ****** Description: Prints the sentence ****** "Hello, world!" to standard output. *****************************************************/#include <stdio.h>intmain (){ /* main *//************************************ Execution Section (body) ************************************* Print the sentence to standard output* (i.e., to the terminal screen).*/printf("Hello, world!\n");} /* main */
C Introduction LessonCS1313 Fall 2019
3
C Character Set
These are the characters that C recognizes.Letters(upper case and lower case)A B C D E F G H I J K L MN O P Q R S T U V W X Y Za b c d e f g h i j k l mn o p q r s t u v w x y zDigits0 1 2 3 4 5 6 7 8 9Special Characters(punctuation etc)space (also known asblank)’ " ( ) * + - / : =! & $ ; < > % ? , .ˆ # @ ˜ ‘ { } [ ] \ |
C Introduction LessonCS1313 Fall 2019
4
C is Case Sensitive
C iscase sensitive: it distinguishes betweenUPPERcase (CAPITAL) andlowercase (small) letters.Keywordsin C – for example, the keywordint–MUSTbe in lower case. For example:#include <stdio.h>intmain (){ /* main */intheight_in_cm;height_in_cm= 160;printf("My height is %d cm.\n",height_in_cm);} /* main */
C Introduction LessonCS1313 Fall 2019
5
Character String Literal Constant
Acharacter string literal constantis a sequence of charactersdelimitedby a double quote at the beginning and a double quote at the end.A character string literal constant is also known as acharacter string literalor astring literalfor short.For example, in thisprintfstatement:printf("This is aprintf.\n");the following is astring literal:"This is aprintf.\n"Theoutputof thisprintfstatement is:This is aprintf.followed by anewline, also known as acarriage return.
C Introduction LessonCS1313 Fall 2019
6
String Literal Cannot Use Multiple Lines
A character string literal constant can only use one line; that is, both of its delimitersMUSTbe on the same line of source code text.So, this isCORRECT:printf("This string literal takes one line");printf(" and so does this string literal.\n");And this isWRONGWRONGWRONG:printf("This string literal takesmore than one line so it's WRONG!\n");Some compilers will accept this but won’t be happy; other compilers will simply reject it.Regardless, if this appears in a program in CS1313,YOU WILL BE SEVERELY PENALIZED!
C Introduction LessonCS1313 Fall 2019
7
Multi-line String Literal Example
%catbad_string_literal.c#include <stdio.h>intmain (){ /* main */printf("This string literal takesmore than one line so it's WRONG!\n");} /* main */%gcc-obad_string_literalbad_string_literal.cgccbad_string_literal.cbad_string_literal.c: In function ‘main’:bad_string_literal.c:5: error: missing terminating " characterbad_string_literal.c:6: error: ‘more’ undeclared (first use in this function)bad_string_literal.c:6: error: (Each undeclared identifier is reported only oncebad_string_literal.c:6: error: for each function it appears in.)bad_string_literal.c:6: error: expected ‘)’ before ‘than’bad_string_literal.c:6: error: missing terminating ' characterbad_string_literal.c:7: error: expected ‘;’ before ‘}’ token
C Introduction LessonCS1313 Fall 2019
8
Newline
In C, you can place anewline, also known as acarriage return, inside a string literal using:\nIf a newline appears inside a string literal in thesource code, then when the string literal is output, the newline causes theoutputto move to a new line.
C Introduction LessonCS1313 Fall 2019
9
Newline Example
%catnewline.c#include <stdio.h>intmain (){ /* main */printf("Howdy do!\n");printf("This string literal contains a newline in the\nmiddle");printf("but this string literal contains a newline at the end.\n");printf("So there!\n");} /* main */%gcc-o newlinenewline.c%newlineHowdy do!This string literal contains a newline in themiddle but this string literal contains a newline at the end.So there!Note: In general, it’s better programming practice toput newlines only at the endof your string literals,not in the middle, because in the middle they can be difficult for programmers (for example, graders) to see.
C Introduction LessonCS1313 Fall 2019
10
Statements
Astatementin a program is like a sentence in a natural language: it’s the smallest possible collection of words and punctuation that can stand by itself and have meaning.For example:printf("Hello, world.\n");This statement tells the compiler to output to the terminal screen the string literalHello, world.followed by a newline.
C Introduction LessonCS1313 Fall 2019
11
Statement Terminator
In C,every statement ends with a semicolon, which is known as thestatement terminator.For example:intheight_in_cm;height_in_cm= 160;printf("My height is %d cm.\n",height_in_cm);Notice:A statement CAN take more than one line(but recall thata string literal CAN’T take more than one line).The way you find the end of a statement is by finding its statement terminator.
C Introduction LessonCS1313 Fall 2019
12
Standard Input & Standard Output
Standard inputis when a user types at the keyboard. It is sometimes shortened tostdin, pronounced “standard in.”Standard outputis when the computer outputs to the terminal screen. It is sometimes shortened tostdout, pronounced “standard out.”In C:ascanfstatement always inputs fromstdin, andaprintfstatement always outputs tostdout.More on this later.
C Introduction LessonCS1313 Fall 2019
13
Block Delimiters
The open curly brace, also known as the left brace,{acts as the start of ablockand is known as theblock open.The close curly brace, also known as the right brace,}acts as the end of ablockand is known as theblock close.The block open and block close are said todelimitthe block: they indicate where the block begins and where the block ends.Delimit: Indicate where something begins and ends.
C Introduction LessonCS1313 Fall 2019
14
What Is a Comment? #1
Acommentis a piece of text in a source file that:tells human beings(for example, programmers) something useful about the program,BUTisignored by the compiler, so it has absolutely no affect on how the program runs.In C, the start of a comment is indicated by/*and the end of a comment is indicated by*/All text appearing between thesecomment delimitersis part of the comment, and therefore is ignored by the compiler.Delimit: Indicate where something begins and ends.
C Introduction LessonCS1313 Fall 2019
15
What Is a Comment? #2
Acommentis a piece of text in a source file that:tells human beings(for example, programmers) something useful about the program,BUTisignored by the compiler, so it has absolutely no affect on how the program runs.In C, the start of a comment is indicated by/*and the end of a comment is indicated by*/A commentcan use multiple linesof text. The delimitersDON’Thave to be on the same line.
C Introduction LessonCS1313 Fall 2019
16
Are Comments Necessary?
Comments are ignored by the compiler, so strictly speaking they aren’t needed to compile and run.But, if you don’t put them into one of your CS1313 programming projects,YOU MAY LOSE A FULL LETTER GRADE OR MOREon that project.Why?Comments tell human beings useful things about your program.They helpprogrammers– including you, a month later when you’ve forgotten everything about your program – to understand your program.They also tellgradersthat you know what the heck you’re doing.
C Introduction LessonCS1313 Fall 2019
17
hello_world.cwith Comments
/***************************************************** Program:hello_world****** Author: Henry Neeman (hneeman@ou.edu) ****** Course: CS 1313 010 Fall 2019 ****** Lab: Sec 014 Fridays 2:30pm ****** Description: Prints the sentence ****** "Hello, world!" to standard output. *****************************************************/#include <stdio.h>intmain (){ /* main *//************************************ Execution Section (body) ************************************* Print the sentence to standard output* (i.e., to the terminal screen).*/printf("Hello, world!\n");} /* main */
C Introduction LessonCS1313 Fall 2019
18
hello_world.cwithout Comments
#include <stdio.h>int main (){printf("Hello, world!\n");}
C Introduction LessonCS1313 Fall 2019
19
Flowchart forhello_world.c
int main (){printf("Hello, world!\n");}
Anovaldenotes either the start or the end of the program, or a haltoperation within the program (which we’ll learn about later).Aparallelogramdenotes either an input operation or an output operation.Anarrowdenotes the flow of the program.
Start
End
Output “Hello, world!”
References:http://www.edrawsoft.com/flowchart-symbols.php

0

Embed

Share

Upload

Make amazing presentation for free
CS1313 C Introduction Lesson