num
will always contain an integer because it’s an int
. The real problem with your code is that you don’t check the scanf
return value. scanf
returns the number of successfully read items, so in this case it must return 1 for valid values. If not, an invalid integer value was entered and the num
variable did probably not get changed (i.e. still has an arbitrary value because you didn’t initialize it).
As of your comment, you only want to allow the user to enter an integer followed by the enter key. Unfortunately, this can’t be simply achieved by scanf("%d\n")
, but here’s a trick to do it:
int num; char term; if(scanf("%d%c", &num, &term) != 2 || term != '\n') printf("failure\n"); else printf("valid integer followed by enter key\n");
Related Posts:
- How to convert an int to string in C?
- How to convert an int to string in C?
- What is EOF in the C programming language?
- How to convert an int to string in C?
- warning: return makes pointer from integer without a cast but returns integer as desired
- Implementing Taylor Series for sine and cosine in C
- warning: implicit declaration of function
- pthread_join() and pthread_exit()
- Why should we typedef a struct so often in C?
- What is the difference between ++i and i++?
- Floating point exception (core dumped)
- Undefined reference to pthread_create in Linux
- Stack smashing detected
- Correct format specifier for double in printf
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- dereferencing pointer to incomplete type
- Openssl : error “self signed certificate in certificate chain”
- How to convert a string to integer in C?
- Get a substring of a char* [duplicate]
- How do I create an array of strings in C?
- How do function pointers in C work?
- Expression must be a modifiable L-value
- “Expected expression before ‘ { ‘ token”
- How to generate a random int in C?
- How do I use valgrind to find memory leaks?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- What does “1e” mean?
- how to use uint64_t in C [duplicate]
- munmap_chunk(): invalid pointer
- How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?
- Warning comparison between pointer and integer
- What is the LD_PRELOAD trick?
- What is the argument for printf that formats a long?
- Cannot assign requested address – possible causes?
- How do I properly compare strings in C?
- Connect: Socket operation on non-socket
- Using %s in C correctly – very basic level
- How to correctly use the extern keyword in C
- How do you make an array of structs in C?
- need help understanding the movzbl call in this function
- How do we check if a pointer is NULL pointer?
- how to use wait in C
- How do I calculate MB/s & MiB/s?
- How to create my own header file in c++?
- In C programming, what is `undefined reference`error, when compiling?
- double free or corruption (fasttop)
- Returning string from C function
- error: expected declaration or statement at end of input in c
- Why do I get clang: error: linker command failed with exit code 1?
- Difference between int32, int, int32_t, int8 and int8_t
- Char Comparison in C
- How to convert integer to char in C?
- Program received signal SIGPIPE, Broken pipe
- How to read from stdin with fgets()?
- Why am I getting “array initializer must be an initializer list or string literal”?
- what is the unsigned datatype?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Reading a string with scanf
- getopt_long() — proper way to use it?
- C read file line by line
- C read file line by line
- Copying a part of a string (substring) in C
- Incompatible implicit declaration of built-in function ‘malloc’
- Need more information about Aborted (core dumped)
- Does C have a “foreach” loop construct?
- What is a bus error? Is it different from a segmentation fault?
- What is the cause of flexible array member not at end of struct error?
- Reversing a string in C
- When a number is written as 0x00… what does the x mean
- How to remove the character at a given index from a string in C?
- How can I get argv[] as int?
- waitpid, wnohang, wuntraced. How do I use these
- Why I do get “Cannot find bound of current function” when I overwrite the ret address of a vulnerable program?
- valgrind – Address —- is 0 bytes after a block of size 8 alloc’d
- Writing binary number system in C code
- Error: initializer element is not computable at load time
- %i or %d to print integer in C using printf()?
- Convert char array to string use C
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- Return a `struct` from a function in C
- Scanning Multiple inputs from one line using scanf
- xorl %eax – Instruction set architecture in IA-32
- Which of sprintf/snprintf is more secure?
- Allocating char array using malloc
- Implementation of strtok() function
- switch case: error: case label does not reduce to an integer constant
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- Use of flag in c?
- What primitive data type is time_t? [duplicate]
- Usage of \b and \r in C
- Parsing command-line arguments in C
- Compiler warning – suggest parentheses around assignment used as truth value
- lvalue required as increment operand