York University EECS 2031 Module 5 Assignment: C Programming Concepts

Verified

Added on  2021/03/18

|44
|2042
|197
Homework Assignment
AI Summary
This document presents a detailed overview of introductory C programming concepts, designed for an EECS 2031 module. The content covers fundamental aspects such as the differences between C and Java, basic I/O functions (scanf, printf, getchar, fgets), and line-based input/output methods. It explores variable names, C data types (char, int, float, double), and qualifiers (unsigned, long, short). The assignment also details integer, floating-point, character, and string constants, along with constant expressions and enumeration constants. Furthermore, it explains declarations, the const qualifier, and type conversion rules. It covers operators (arithmetic, relational, logical, bitwise), bit shifting, and control flow statements (if, else, switch, while, for, do-while, continue, break, and goto). String comparison using string.h functions is also included. This comprehensive resource is designed to assist students in understanding and applying core C programming principles.
Document Page
1
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level

EECS 2031

Software Tools

Module 5 – Introduction to C
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
2
Textbook

The C Programming Language (2nd edition)
by Brian Kernighan and Dennis Ritchie
Prentice Hall Software Series
Document Page
3
C vs. Java

Java-like (actually Java has a C-like
syntax), some differences

No garbage collection
No classes
No exceptions (try … catch)
No String type
Pointers
Document Page
4
First C Program (first.c)

#include <stdio.h>

main() {

printf(
hello, world \n);
}

Note: #include <filename.h>
replaces the line by the actual file before
compilation starts.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
5
Basic I/O

Every program has a standard input and a
standard output.

By default, keyboard and monitor,
respectively

Input functions
Output functions
scanf()
printf()
getchar()
putchar()
fgets()
fputs()
Document Page
6
Output is easy… (celsius.c)

Most of the time, use printf()
Very similar to Java
See Chapter 7 in the textbook
Returns the number of characters printed
Can also use putchar() for a single
character
Document Page
7
Input is more complicated

Several functions for input should never
be used because they are unsafe

They are still in the standard library
because a lot of code out there uses them

Avoid using gets() as well as scanf()
for strings

Recommended way to read input:
getchar() or fgets() + sscanf()
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
8
getchar()

To read one character at a time from the
standard input (the keyboard by default):

int getchar(void)

Returns the next input character each
time it is called

Returns EOF when it encounters end of
file.

EOF input: Ctrl-D (Unix) or Ctrl-Z
(Windows).
Document Page
9
getchar()

It buffers input characters until a new line
or EOF is entered.

That is, nothing happens until you hit Return
or EOF.

See getchar1.c and getchar2.c
Take a look at the man page
man –S 3 getchar
Document Page
10
scanf()

scanf() can be used for formatted input
To read an integer:
int num;

scanf("%d", &num);

&num is a pointer to num
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
11
scanf()

To read a char and a float:
char c; float f;

scanf("%c %f”, &c, &f);

scanf() stops when it exhausts its
format string, or when some input fails to
match
Document Page
12
scanf()

Returns the number of successfully
matched and assigned input items

Returns 0 if the input does not match the
specification in the format string (i.e., an
error).

On the end of file, EOF is returned.
chevron_up_icon
1 out of 44
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]