C Programming (Important Questions Set 1)

Neso Academy
7 Mar 201813:42

Summary

TLDRThis video script discusses various C programming concepts, including the behavior of printf functions, character data types, and integer arithmetic. It explains how printf can print strings and return the number of characters printed, the impact of using %10s for formatting strings, and the arithmetic modulo operation with character overflow. Additionally, it covers the declaration of integers with modifiers like signed and unsigned, and how the internal representation of integers in computers affects the output of printf statements, emphasizing the importance of careful code analysis for exams like GATE.

Takeaways

  • 💻 The script discusses a C program fragment involving nested printf functions and explains their output.
  • 🔢 The outer printf function prints the number of characters printed by the inner printf function, which prints 'Hello World!'.
  • 📏 The script explains the use of format specifiers like %d for integers and %s for strings in printf functions.
  • 📑 It highlights the importance of including the null character when counting the total number of characters in a string.
  • 📐 The script demonstrates how printf can be used with padding (%10s) to control the width of the output.
  • 🔄 The script explores the concept of data types and arithmetic operations, particularly with character variables.
  • 🔒 It explains how character variables can only hold values up to 255 and uses modulo arithmetic to show how values exceeding this are handled.
  • 📝 The script discusses the declaration of integer variables in C, including signed and unsigned integers.
  • 🔄 It clarifies that omitting the data type for a variable defaults to int, which is implicitly assumed by the compiler.
  • 🔢 The script explains the internal representation of negative numbers in computers using 2's complement form.
  • 💡 It emphasizes the importance of carefully reading each line of code, especially for understanding questions in competitive exams like GATE.

Q & A

  • Question 1: What does the first printf function in the program do?

    -The first printf function prints 'Hello World!' on the screen, followed by the number of characters in the string, which is 12. Hence, the output is 'Hello World!12'.

  • Question 2: Why does the second printf function use '%10s' instead of '%s'?

    -The '%10s' format specifier ensures that the string is printed within 10 characters wide. Since 'Hello' has 5 characters, 5 white spaces are printed first, followed by 'Hello'. This is used to align the text in a wider field.

  • Question 3: How does the character variable behave when assigned a value exceeding its range?

    -When a character variable is assigned a value exceeding its range (which is 255 for 8 bits), it follows arithmetic modulo 256. For example, assigning 265 results in 265 mod 256, which equals 9. Thus, the value 9 is printed.

  • Question 4: How does a signed integer differ from an unsigned integer in the context of printf?

    -A signed integer allows negative values, while an unsigned integer only handles positive values. When using '%u', the printf function interprets the value as unsigned, potentially printing a large number instead of a negative value, as it interprets the binary form differently.

  • Question 5: Why does the program print '4294967293' when using '%u'?

    -The value -3 is represented in two's complement form, and when printed as an unsigned integer using '%u', the value is interpreted as 4294967293 due to how unsigned integers handle binary representation on a 4-byte machine.

  • Question 6: What is the significance of arithmetic modulo 2 raised to the power n in the context of character data types?

    -For a character variable, which is 8 bits long, the maximum value it can hold is 255 (2^8 - 1). If a value exceeds this range, it wraps around according to modulo arithmetic (value mod 256).

  • Question 7: Why is 'signed i;' considered a correct definition?

    -Even though 'int' is not explicitly mentioned, the compiler implicitly assumes the 'int' data type. Therefore, 'signed i;' is equivalent to 'signed int i;', making it a valid declaration.

  • Question 8: What is the internal representation of negative integers like -3 in computers?

    -Negative integers are represented using two's complement. To get the two's complement of a number, first find the one's complement (invert the bits) and then add 1 to the result. This allows negative numbers to be handled in binary form.

  • Question 9: What happens if you mix signed and unsigned integers in arithmetic operations?

    -When you mix signed and unsigned integers, the result may be unexpected because the unsigned integer takes precedence in most cases, leading to a potential misinterpretation of the result as a large positive number instead of a negative one.

  • Question 10: Why is it important to carefully read format specifiers like '%d' and '%u' in C programming?

    -The format specifier controls how values are printed. '%d' prints signed integers, while '%u' prints unsigned integers. Misusing these specifiers can lead to incorrect output, such as interpreting a negative value as a large positive number, which can lead to confusion.

Outlines

00:00

💻 C Program Output Analysis

The paragraph discusses the behavior of C program fragments involving the printf function. It starts with an introduction to the lesson, emphasizing that the content is a review of previous topics. The instructor invites viewers to pause the video and try to understand the code written on the screen before the explanation. The code in question involves a printf function within another printf function, which is unusual. The inner printf uses the format specifier %s to print the string 'Hello World!'. The instructor explains that printf returns the number of characters printed, including the space, totaling 12 characters. The expected output is 'Hello World!12'. The next part of the lesson involves another printf function with the format specifier %10s, which prints the string 'Hello' followed by spaces to fill a field of 10 characters wide. The final part of the paragraph discusses what happens when a character variable is assigned a value beyond its capacity, such as 255, and then incremented. It explains that character variables can hold values up to 255 due to their 8-bit size, and exceeding this results in a modulo operation with 2^8 (256), leading to a remainder of 9, which is what gets printed.

05:02

🔢 Understanding Data Types in C

This section delves into the correct definitions of integers in C programming. It starts with a multiple-choice question about which statements correctly define an integer. The instructor explains that the first statement, defining a signed integer, is correct. It clarifies that omitting the 'int' keyword and just using 'signed' is also valid due to compiler assumptions. The paragraph also covers the definitions of 'unsigned', 'long', and 'long long' integers, confirming that all the given options are correct. The discussion then shifts to a program that prints the result of adding an unsigned integer to a negative integer. The instructor explains that the output is not -3 as one might expect, but rather depends on the machine due to the use of the %u format specifier, which interprets the result as an unsigned integer. The maximum value for an unsigned integer is 4294967295 for a 4-byte integer, and the actual output is one less than this maximum value due to the initial value of -4 being added to 1.

10:03

📚 Recap and Importance of Careful Reading

The final paragraph serves as a recap of the lesson and emphasizes the importance of careful reading and understanding of each line of code, especially for exams like GATE. It highlights that such questions test the ability to comprehend every detail within a code snippet. The instructor reminds viewers to pay close attention to every line and concludes the lecture with a farewell, indicating the next lecture will continue the discussion.

Mindmap

Keywords

💡printf function

The printf function is a standard library function in C programming used to send formatted output to the standard output stream. It is a versatile function that can output various data types, including integers, characters, and strings. In the video, the instructor uses printf to demonstrate how to print strings and integers, as well as to return the number of characters printed. For example, the script shows a printf function calling another printf within its arguments, which is used to print 'Hello World!' followed by the number of characters printed.

💡format specifier

A format specifier in C programming is a code that tells the printf function the expected data type of the argument to be printed. The script mentions '%d' for integers and '%s' for strings. The instructor explains that '%d' is used to print an integer, while '%s' is for printing a string of characters, such as 'Hello World!'.

💡character

In the context of the video, a character refers to a data type in C that can hold a single letter or digit. The instructor discusses how a character variable can be assigned an integer value and how it behaves when the value exceeds the maximum limit for a character (255). The example of incrementing a character variable from 255 to 265 and printing it demonstrates the concept of overflow and modulo arithmetic.

💡arithmetic modulo

Arithmetic modulo is a mathematical operation that finds the remainder after division of one number by another. In the video, the instructor explains how character data types, being 8 bits long, use modulo 2^8 arithmetic to handle values that exceed their storage capacity. The example of assigning 265 to a character variable and printing it results in 9, demonstrating modulo arithmetic in practice.

💡integer

An integer is a whole number that can be positive, negative, or zero. In C programming, integers are a fundamental data type used to store whole numbers. The script discusses various ways to declare integer variables, such as 'int', 'signed int', and 'unsigned int', and the implications of using unsigned integers when performing arithmetic operations.

💡unsigned integer

An unsigned integer is a data type that can only represent non-negative integers. The video explains that when an arithmetic operation results in a negative value with unsigned integers, the result wraps around to a large positive value. This is demonstrated when adding -4 to an unsigned integer 1, which results in a large number depending on the machine's integer size.

💡2's complement

2's complement is a method for representing signed integers in binary form used in computer systems. The video script explains how -3 is represented in 2's complement, involving taking the 1's complement of the binary representation of 3 and then adding 1. This concept is crucial for understanding how negative numbers are stored and manipulated in computers.

💡ASCII table

The ASCII (American Standard Code for Information Interchange) table is a character encoding standard for electronic communication. The video mentions the ASCII table in the context of character variables and how they can represent characters according to their ASCII values. This is relevant when discussing the output of printing character variables.

💡GATE examination

The GATE (Graduate Aptitude Test in Engineering) examination is a standardized test in India for postgraduate admissions in engineering and the sciences. The script mentions that understanding each line of code is critical for GATE exams, where questions often test the candidate's ability to comprehend and predict the behavior of code snippets.

💡data type

A data type in programming defines the type of value a variable can hold. The video script covers various data types in C, such as int, signed int, unsigned int, long, and long long int. The instructor explains that the compiler implicitly assumes certain data types if not explicitly stated, which is important for understanding variable declarations and their defaults.

Highlights

The lesson reviews previous topics and does not introduce new material.

Students are encouraged to pause the video and try to understand the written C program fragment.

A printf function within another printf function is explained, which is unusual.

The inner printf function returns the number of characters printed.

The number of characters in a string includes spaces and should be counted accurately.

The output of the nested printf functions is expected to be 'Hello World !12'.

The concept of format specifiers in printf is explained with %10s.

The %10s format specifier prints a string with leading spaces to fill up to 10 characters wide.

The output of the modified printf with %10s is 'Hello' followed by spaces.

A character variable can hold integer values, and exceeding the range results in modulo arithmetic.

The value 265 assigned to a character variable results in a modulo operation with 256.

The result of 265 mod 256 is 9, which is printed on the screen.

The definition of integers is discussed, including signed and unsigned modifiers.

Compiler assumptions about data types for variables like 'signed i;' are explained.

All the given statements about integer definitions are correct.

The output of a program with unsigned and signed integers is machine-dependent.

The internal representation of -3 in computer using 2's complement form is explained.

The difference between printing with %d and %u in printf changes the output significantly.

Careful reading of each line is emphasized for understanding and answering questions correctly.

The importance of such questions in GATE examinations is highlighted.

Transcripts

play00:00

Today we are going to consider some

play00:02

questions on the topics

play00:04

that we had already studied

play00:05

in our previous lessons.

play00:07

These questions are related to

play00:08

the previous topics that we had

play00:10

already studied.

play00:11

Therefore there is nothing new,

play00:12

if something is new, I will talk about that

play00:15

in this lesson.

play00:16

1. What is the output of the following

play00:18

C program fragment:

play00:19

Before I will explain the program,

play00:21

I would recommend to you to please

play00:23

pause the video for a while,

play00:25

and try to understand whatever

play00:27

I have written over here.

play00:29

Then come back again

play00:30

and see what I explained

play00:32

in this particular session.

play00:33

OK,

play00:34

Here you see,

play00:35

I have written a printf function

play00:37

and inside this printf function

play00:39

I have written two arguments.

play00:41

One is %d,

play00:43

as we all know, this is a format specifier

play00:46

used to print an integer

play00:48

and another argument is

play00:50

printf function.

play00:51

Now this is something weird.

play00:52

In this argument we usually write

play00:54

a variable or some integer value

play00:57

but here, instead of that

play00:58

I have written printf function.

play01:00

Now what does this printf function do

play01:02

Inside this printf function,

play01:04

I have provided the argument as

play01:06

%s.

play01:07

Now this is something new to you.

play01:09

%s means, we need to print

play01:12

string of characters on to the screen.

play01:14

%s is used to print

play01:17

"string of characters."

play01:19

Here I have provided,

play01:21

a string of characters as

play01:23

"Hello World !"

play01:24

If you want to print this "Hello World !"

play01:26

on the screen,

play01:27

then we have to use %s.

play01:30

OK, what this printf function will do

play01:33

is that it prints "Hello World !" on screen.

play01:36

This is OK.

play01:37

But what does this printf function do?

play01:39

What does it print?

play01:41

printf not only prints

play01:43

the content on the screen.

play01:44

It also returns the

play01:46

number of characters that it

play01:48

successfully prints on the screen.

play01:50

Now this is quite important for us to know

play01:53

this printf function will not

play01:55

only print "Hello World !" on the screen,

play01:58

but it also returns the value

play02:01

which is equivalent to

play02:02

the number of characters

play02:04

it successfully prints on the screen.

play02:07

Here you can see, the number of characters

play02:09

in this string is

play02:10

1 2 3 4 5 6

play02:15

You have to include that blank character as well.

play02:18

Don't forget to include the blank character.

play02:21

OK,

play02:22

up till now there are total 6 characters.

play02:24

After this, 7 8 9 10 11 and then 12.

play02:31

There are total 12 character that we

play02:33

need to print on the screen.

play02:34

Right?

play02:35

So this printf function will also

play02:37

return 12 to this

play02:39

particular printf function

play02:41

as second argument.

play02:43

So this printf function will print

play02:45

12 and this printf function prints

play02:48

"Hello World !" on the screen.

play02:49

Therefore the output is

play02:51

"Hello World !12"

play02:54

That is what is expected.

play02:56

Let's consider the next question.

play02:58

What is the output of the following

play03:00

C program fragment:

play03:01

Kindly pause the video for a while

play03:03

and try to understand

play03:05

what this printf is going to print

play03:08

on the screen.

play03:09

OK here,

play03:11

I have provided the first argument

play03:13

as %10s instead of %s.

play03:18

And second argument is

play03:20

"Hello"

play03:20

Now what would be the output

play03:22

provided by this printf function

play03:24

Let's try to understand

play03:26

by executing it on Code blocks.

play03:28

Here I changed the code a little bit

play03:31

to actually differentiate between

play03:33

these two statements.

play03:35

Let's execute the code.

play03:37

First printf prints Hello

play03:39

simply on the screen.

play03:41

while

play03:42

the second printf prints Hello

play03:44

but after some white spaces.

play03:47

This is because of this 10.

play03:50

This 10 means print the characters

play03:53

up to 10 characters wide.

play03:56

This means,

play03:57

here in our example

play03:59

we just have 5 characters to print.

play04:01

Now because of the shortage of characters,

play04:04

it will print first white spaces

play04:07

and then the rest of the 5 characters.

play04:10

5 white spaces printed

play04:12

and after that 5 characters

play04:14

gets printed.

play04:15

That is the reason why

play04:17

%10s is used.

play04:19

If you want to print

play04:21

string up to 10 characters wide,

play04:23

then we use %10s.

play04:26

If we want 15 characters wide,

play04:28

then we write 15 instead of 10.

play04:31

That is the difference between

play04:33

this one and this one.

play04:34

Let's consider the next question.

play04:36

What is the output of the

play04:38

following C program fragment:

play04:41

Here is this program,

play04:43

let's try to understand what does

play04:45

it really do.

play04:46

Here I have provided a character

play04:48

variable c and assigned it a value

play04:51

255. You can assign integer values

play04:54

to a character variable.

play04:56

As I already told you,

play04:57

in the lesson when I taught you

play04:59

about the character variable.

play05:01

Here you can see, I have provided an integer

play05:03

value to this character variable

play05:06

and after that

play05:07

I increment that value to 10

play05:09

and store it again in this variable.

play05:12

Here after the increment

play05:14

this will be 265.

play05:15

and I simply print it on to the screen.

play05:17

Can you guess what would be the output

play05:20

of this printf statement?

play05:21

Is it 265

play05:23

Is it some character according to ASCII table

play05:26

Is it 7

play05:27

or is it 9.

play05:28

As we know this is 255,

play05:31

character is capable of holding

play05:33

the information up to 1 byte only

play05:35

which is equal to 8 bits.

play05:37

8 bits means the maximum value

play05:40

that it can hold is 255.

play05:42

And cannot be more than that.

play05:44

If we try to exceed the range,

play05:46

like in this case

play05:48

we are exceeding the range

play05:49

because we are assigning to this c variable

play05:52

265.

play05:53

If we try to exceed the range,

play05:55

then what would happen is that,

play05:57

as we know this thing

play05:59

that all the data types like

play06:01

character, integer, float,

play06:03

long, double

play06:05

they all follow

play06:06

or you can say, they obey the laws of

play06:08

arithmetic modulo 2 raised to the power n

play06:12

where n is equal to

play06:14

8 in this case.

play06:16

As character data type is of 8 bits long,

play06:19

therefore n is equal to 8.

play06:22

Here in this case, the value is 265

play06:25

therefore we will calculate

play06:27

265 mod 2 raised to the power 8.

play06:31

2 raised to power 8 is equal to 256.

play06:33

Therefore it is 265 mod 256

play06:36

which is equal to 9.

play06:38

When we divide 265 by 256

play06:42

we get the remainder as 9,

play06:44

that is why, 9 is printed on the screen.

play06:48

And therefore the answer is option d.

play06:50

Let's consider this question.

play06:52

Which of the following statement/ statements

play06:55

is/ are correct corresponding to the

play06:57

definition of the integer:

play06:59

As we know how to define or declare

play07:01

an integer.

play07:02

Here are some statements written.

play07:04

We have to determine that

play07:06

which of the following statements or statement

play07:09

is correct corresponding to the

play07:11

definition of integer.

play07:12

Is it only I and V ?

play07:14

Is it just I ?

play07:16

Are all correct?

play07:17

Or just IV V and VI are correct?

play07:20

Take you time and try to interpret this

play07:22

one by one.

play07:24

OK. Let's understand them one by one.

play07:28

The first interpretation is correct.

play07:30

This is the actual definition of

play07:32

a signed integer.

play07:33

You can also write int i;

play07:35

But instead of int i

play07:37

you can also write signed int i;.

play07:39

Both are one and the same thing.

play07:41

Here writing signed int i;

play07:44

int is a data type

play07:46

and signed is our modifier

play07:47

for integer.

play07:48

Therefore this is a correct definition.

play07:50

Apart from that,

play07:52

in this second statement,

play07:53

I have written signed i;

play07:55

instead of signed int i;

play07:57

Now this is something weird.

play07:59

I have not written anything

play08:02

like int data type

play08:04

or some character data type

play08:05

or any other data type here in this case.

play08:07

Therefore it seems like a incorrect

play08:10

definition but I would like to tell you

play08:12

that this is not an incorrect definition.

play08:14

It is correct.

play08:16

Because compiler implicitly assume

play08:19

integer data type.

play08:21

If you have not written any data type

play08:23

over here, then compiler

play08:25

will assume this data type

play08:26

is integer over here.

play08:28

Therefore signed i;

play08:30

is equivalent to

play08:32

signed int i;

play08:33

Similarly unsigned i;

play08:35

is also correct.

play08:36

Because compiler implicitly assumes

play08:38

as integer therefore it automatically puts

play08:41

int in front of this variable.

play08:43

And unsigned i; is also a

play08:45

correct definition.

play08:47

On the other hand long i;

play08:49

is also a correct definition,

play08:51

that is equivalent to long int i;

play08:53

and long long i; is

play08:55

equivalent to long long int i;

play08:57

Therefore we can say,

play08:59

option c is the correct answer.

play09:01

Because all of them are

play09:03

actually correct. Therefore C is the correct answer.

play09:06

Now let us see what this program prints.

play09:09

Does it print garbage value?

play09:11

Does it print -3?

play09:13

Or does it print integer value that

play09:15

depends from machine to machine?

play09:17

Or is it none of the above?

play09:18

Ok,

play09:19

in the first statement itself,

play09:21

I have written unsigned i equal to 1;

play09:25

that is equivalent to unsigned int i;

play09:27

Automatically compiler puts

play09:29

int in front of this variable.

play09:31

Therefore there is no problem.

play09:32

unsigned int i is equal to 1

play09:35

means that this integer variable

play09:37

is going to have a value equal to 1.

play09:40

in the next statement,

play09:41

I have declared a variable j

play09:43

and assigned it a value -4.

play09:45

Inside this printf function

play09:48

I have written i+j.

play09:51

This is an arithmetic expression,

play09:53

and the result is getting printed.

play09:55

using this printf function.

play09:57

i+j is equal to 1-4

play10:00

which is equal to -3,

play10:02

and that is getting printed.

play10:04

You might think the answer is -3

play10:06

but it is not.

play10:08

Let's try to understand what is the reason.

play10:10

But before that,

play10:12

we must have to know

play10:13

what would be the internal representation

play10:15

of -3 in computer.

play10:18

Internally -3 is represented

play10:20

in 2's complement form.

play10:22

Here I have written certain steps,

play10:24

to know how we can represent

play10:27

-3 internally in computer.

play10:30

You first have to take 1's complement of 3.

play10:33

That is,

play10:34

This is the binary representation of 3

play10:37

as this value is set to 1

play10:38

and this is also set to 1

play10:40

which is (2 raised to power 1) + (2 raised to power 0)

play10:43

that is equal to 3.

play10:45

And then we perform 1's complement of 3.

play10:48

which is nothing but changing the

play10:50

0's to 1's and 1's to 0's.

play10:53

Here in this case I change

play10:55

all the 0's to 1's and 1's to 0's.

play10:57

And this gives me the answer

play10:59

which is 1's complement of 3.

play11:01

Then in order to produce the

play11:03

2's complement of 3,

play11:05

we need to add 1 to the

play11:07

result of this 1's complement,

play11:09

which gives us this answer.

play11:11

Now, this would be the binary

play11:13

representation of -3.

play11:15

You can say internally that is

play11:17

represented in our modern computers.

play11:20

OK, now if I have written

play11:22

%d instead of %u

play11:25

this will print the answer -3.

play11:27

Now because I have written %u

play11:30

this will be interpreted as

play11:32

an unsigned integer.

play11:34

And we know this thing that

play11:36

unsigned integers' maximum value

play11:38

is 4294967295

play11:42

in the case when integer is of

play11:44

4 bytes long.

play11:46

We know this thing.

play11:47

Here you can say not all the bits

play11:49

are equal to 1.

play11:50

This bit is not equal to 1.

play11:52

If this is 1, then this would be

play11:55

4294967295.

play11:57

Because this is not set

play11:59

and its place value is

play12:01

2 raised to the power 1 which is equal to 2.

play12:03

Therefore we need to subtract,

play12:05

2 from the maximum value

play12:08

4294967295.

play12:11

This gives us the answer as

play12:13

4294967293.

play12:17

This is on my computer.

play12:19

Basically in my computer,

play12:20

because the size of integer is

play12:22

of 4 bytes,

play12:23

Therefore the maximum value is

play12:25

4294967295 and subtracting 2 from it

play12:29

will give me the answer 4294967293.

play12:33

May be this is possible in your computer

play12:36

size of integer is of 2 bytes,

play12:39

then maximum value would be different

play12:41

and subtracting 2 from it would give

play12:43

something else.

play12:44

Note down this thing.

play12:46

Because of %u,

play12:48

the output printed is,

play12:49

integer value that depends from machine to machine.

play12:52

And therefore the answer is c.

play12:54

But if it is %d,

play12:56

the output would be -3.

play12:58

And that is the major difference between these things

play13:01

From this example we have

play13:03

to know this thing,

play13:04

and we have to understand this thing,

play13:06

that we need to actually read

play13:08

each and every line very carefully.

play13:11

when we are answering the questions like this

play13:13

In GATE examination,

play13:15

such things are very critical

play13:17

and very frequent.

play13:18

They are checking your ability

play13:20

of understanding each and every line.

play13:22

Therefore such kinds of questions

play13:24

are frequent in GATE examinations.

play13:26

OK friends, this is it for now.

play13:29

See you in the next lecture. Bye.

Rate This

5.0 / 5 (0 votes)

関連タグ
C ProgrammingCode AnalysisIntegersModulo ArithmeticPrintf FunctionData Types2's ComplementCompilerGATE ExamEducational
英語で要約が必要ですか?