Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views6 pages

Tutorial 02

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Programming with C Language

Tutorial 2

1. How do you write comments in a c program? What is the purpose of comments in a


program?

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.

2. Which is the function that is essential in a C program?

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!

3. What is the purpose of ‘scanf’ ?

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!

4. Is ‘standard c’ a case sensitive language?

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.

(a) record1 (e) $tax (h) name-and-address

(b) 1record (f) name (i) name_and_address

(c) file-3 (g) name and address (j) 123 - 45 - 6789

(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.

(f) name: Valid identifier. It consists of letters only, which is allowed.

(g) name and address: Invalid identifier. It contains spaces, which are not allowed in identifiers.
Only letters, digits, and underscores are allowed.

(h) name-and-address: Invalid identifier. It contains a hyphen, which is not allowed in


identifiers.

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)

7. What does the following code print?


printf( "*\n**\n***\n****\n*****\n" );

*
**
***
****
*****
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 );

a) `scanf( "d", value );`


Error: The format specifier is missing the `%` symbol before the `d`.
Correction: The corrected statement should be `scanf("%d", &value);`.

b) `printf( "The product of %d and %d is %d"\n, x, y );`


Error: The closing double quote is placed before the newline escape sequence (`\n`), causing a
syntax error.
Correction: The corrected statement should be `printf("The product of %d and %d is %d\n", x, y,
x * y);`.

c) `Scanf( "%d", anInteger );`


Error: The `scanf` function is written with a capital 'S' instead of a lowercase 's'.
Correction: The corrected statement should be `scanf("%d", &anInteger);`.

d) `printf( "Remainder of %d divided by %d is\n", x, y, x % y );`


Error: The format specifier `%d` is not followed by a corresponding argument (x % y).

4
Correction: The corrected statement should be `printf("Remainder of %d divided by %d is %d\
n", x, y, x % y);`.

e) `print( "The sum is %d\n," x + y );`


Error: The function name should be `printf` instead of `print`. Also, there is a missing comma
after the string format specifier.
Correction: The corrected statement should be `printf("The sum is %d\n", x + y);`.

f) `Printf( "The value you entered is: %d\n, &value );`


Error: The `printf` function is written with a capital 'P' instead of a lowercase 'p'. Also, there is a
missing closing double quote after the format specifier.
Correction: The corrected statement should be `printf("The value you entered is: %d\n",
value);`.

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

e) `printf("%d = %d", x + y, y + x);`


Prints: 5 = 5

f) `z = x + y;`
nothing

g) `scanf("%d%d", &x, &y);`


nothing
h) `/* printf("x + y = %d", 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.

a) C operators are evaluated from left to right.(True)


b) The following are all valid variable names: _under_bar_ , m928134 , t5 , j7 ,
her_sales , his_account_total , a , b , c , z , z2 .(True)
c) The statement printf("a = 5;"); is a typical example of an assignment statement.(False)
All of the variable names you listed are valid in C. Variable names can contain letters,
numbers, and underscores (_), but they cannot start with a number.
d) A valid arithmetic expression containing no parentheses is evaluated from left to
right.(True)
e) The following are all invalid variable names: 3g , 87 , 67h2 , h22 , 2h (True)

You might also like