Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is value of EOF and '\0' in C

Tags:

c++

c

null

eof

I know that EOF and '\0' are of type integers, but if so shouldn't they have a fixed value?

I printed both and got -1 for EOF and 0 for '\0'. But are these values fixed?

I also had this

int a=-1;

printf("%d",a==EOF); //printed 1

Are the value for EOF and '\0' fixed integers?

like image 898
theReverseFlick Avatar asked Jan 16 '11 14:01

theReverseFlick


People also ask

What is value of EOF in C?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

Is EOF and \n same?

The answer is yes.

What is the use of '\ 0 in C programming?

'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.

What is the value returned by EOF?

An EOF return value indicates an error or an end-of-file condition. Use the feof() or the ferror() function to determine whether the EOF value indicates an error or the end of the file. The value of errno can be set to: Value.


3 Answers

EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.

'\0' is a char with value 0 in C++ and an int with the value 0 in C.

The reason why printf("%d",a==EOF); resulted in 1 was because you didn't assign the value EOF to a. Instead you checked if a was equal to EOF and since that was true (a == -1 == EOF) it printed 1.

like image 136
CB Bailey Avatar answered Oct 21 '22 17:10

CB Bailey


NULL and '\0' are guaranteed to evaluate to 0, so (with appropriate casts) they can be considered identical in value; notice however that they represent two very different things: NULL is a null (always invalid) pointer, while '\0' is the string terminator. EOF instead is a negative integer constant that indicates the end of a stream; often it's -1, but the standard doesn't say anything about its actual value.

C & C++ differ in the type of NULL and '\0':

  • in C++ '\0' is a char, while in C it's an int; this because in C all character literals are considered ints.
  • in C++ NULL is "just" an integral 0, while in C it can be defined as a 0 casted to void *; this cannot be done in C++ (and it's explicitly forbidden in a note) because, being C++ more strict in pointer conversions, a void * is not implicitly convertible to any other pointer type, so, if NULL was a void *, it would be necessary to cast it to the target pointer type on assignment:

    int * ptr = (void *) 0; /* valid C, invalid C++ */
    

Relevant standard quotations:

C++98/03

NULL

NULL is an integer type guaranteed to evaluate to 0:

4.10 Pointer conversions

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).

18.1 Types

[...] The macro NULL is an implementation-defined C++ null pointer constant in this international Standard (4.10). (Possible definitions include 0 and 0L, but not (void*)0).

'\0'

A 0-value char must exist:

2.2 Character sets

The basic execution character set and the basic execution wide-character set shall each contain [...] a null character (respectively, null wide character), whose representation has all zero bits.

'\0' is a char literal:

2.13.2 Character literals

A character literal is one or more characters enclosed in single quotes, as in 'x', optionally preceded by the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

and it's value is 0, since that escape sequence specifies its value:

The escape \ooo consists of the backslash followed by one, two, or three octal digits that are taken to specify the value of the desired character.

'\0' is used to terminate strings literals:

2.13.4 String literals

After any necessary concatenation, in translation phase 7 (2.1), '\0' is appended to every string literal so that programs that scan a string can find its end.

EOF

The definition of EOF is delegated to the C89 standard (as stated in §27.8.2 "C Library files"), where it is defined as an implementation specific negative integer.

C99

NULL

A null pointer is a 0 integer, optionally casted to void *; NULL is a null pointer.

6.3.2.3 Pointers

[...] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. (The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17.) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

7.17 Common definitions <stddef.h>

[...] The macros are

NULL

which expands to an implementation-defined null pointer constant; [...]

'\0'

'\0' is an integer with value 0, and is used to terminate strings:

5.2.1 Character sets

[...] A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

6.4.4.4 Character constants

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. [...]

The octal digits that follow the backslash in an octal escape sequence are taken to be part of the construction of a single character for an integer character constant or of a single wide character for a wide character constant. The numerical value of the octal integer so formed specifies the value of the desired character or wide character. [...]

An integer character constant has type int.

EOF

EOF is an implementation-defined negative integer

7.19 Input/output <stdio.h>

7.19.1 Introduction

EOF

which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream

like image 34
Matteo Italia Avatar answered Oct 21 '22 18:10

Matteo Italia


'\0' is always the null character, or 0. EOF depends on the compiler, but is usually -1, and always is a value that an unsigned char can't hold. Don't rely on the value of EOF being anything, because it CAN CHANGE. Always do x == EOF not x == -1. The value of '\0' is ALWAYS 0. You can count on that.

like image 6
Leo Izen Avatar answered Oct 21 '22 18:10

Leo Izen