Tutorial 02
Tutorial 02
Tutorial 02
Tutorial 2
In C programming, you can write comments using two forward slashes (//) for single-line
comments or using /* and */ for multi-line comments. Comments are used to explain
parts of the code and make it easier to understand for other programmers. They don't
affect the program's execution.
The main() function is essential in a C program. It serves as the entry point for the
program and contains the code that will be executed when the program starts running.
Without the main() function, the program won't be able to execute any code. It's like
the heart of the program!
The purpose of `scanf` in C programming is to read input from the user. It allows you to
prompt the user for input and store that input into variables. You can use `scanf` to read
different types of input, such as integers, floating-point numbers, characters, and
strings. It's a handy function when you want your program to interact with the user and
take input dynamically. Let me know if you have any more questions!
Yes, "Standard C" is a case-sensitive language. This means that uppercase and lowercase
letters are treated as distinct and have different meanings in the language. For example,
1
"myVariable" and "myvariable" would be considered as two different variables in C. It's
important to be consistent with your capitalization when writing code in C to avoid any
potential errors. Let me know if there's anything else I can help with!
5. Determine which of the following are valid identifiers. If invalid, explain why.
(d) return
(a) record1: Valid identifier. It starts with a letter and can be followed by letters, digits, or
underscores.
(b) 1record: Invalid identifier. It starts with a digit, which is not allowed. Identifiers cannot start
with a number.
(c) file-3: Invalid identifier. It contains a hyphen, which is not allowed in identifiers. Only letters,
digits, and underscores are allowed.
(d) return: Valid identifier. Although "return" is a keyword in C, it can still be used as an
identifier.
(e) $tax: Valid identifier. It starts with a dollar sign, which is allowed in identifiers.
(g) name and address: Invalid identifier. It contains spaces, which are not allowed in identifiers.
Only letters, digits, and underscores are allowed.
2
(i) name_and_address: Valid identifier. It consists of letters, underscores, and digits, which is
allowed.
(j) 123-45-6789: Invalid identifier. It starts with a digit and contains hyphens, both of which are
not allowed in identifiers.
6. State whether each of the following is true or false. If false, explain why.
a) Function printf always begins printing at the beginning of a new line. (False)
b) Comments cause the computer to print the text enclosed between /* and */ on the
screen when the program is executed. (True)
c) The escape sequence \n when used in a printf format control string causes the cursor
to position to the beginning of the next line on the screen. (True)
d) All variables must be defined before they’re used. (True)
e) All variables must be given a type when they’re defined. (True)
f) C considers the variables, number and NuMbEr to be identical. (False)
g) A program that prints three lines of output must contain three printf statements.
(False)
*
**
***
****
*****
3
8. Identify and correct the errors in each of the following statements. (Note: There may be
more than one error per statement.)
a) scanf( "d", value );
b) printf( "The product of %d and %d is %d"\n, x, y );
c) Scanf( "%d", anInteger );
d) printf( "Remainder of %d divided by %d is\n", x, y, x % y );
e) print( "The sum is %d\n," x + y );
f) Printf( "The value you entered is: %d\n, &value );
4
Correction: The corrected statement should be `printf("Remainder of %d divided by %d is %d\
n", x, y, x % y);`.
9. What, if anything, prints when each of the following statements is performed? If nothing
prints, then answer “Nothing.” Assume x = 2 and y = 3 .
a) `printf("%d", x);`
Prints: 2
b) `printf("%d", x + x);`
Prints: 4
c) `printf("x=");`
Prints: x=
5
d) `printf("x=%d", x);`
Prints: x=2
f) `z = x + y;`
nothing
i) `printf("\n");`
Prints: (a new line)
10. State which of the following are true and which are false. If false, explain your answer.