Unexpected behaviour while taking input in C language - c

I am trying to run this code which seems fine to me but the end results are not much convincing. Here's the code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char nT,nF;
char extension[5];
puts("Enter No. of Testcases & Faults");
scanf("%d %d %s", &nT, &nF, extension);
printf("%d %d\n",nT,nF);
getch();
}
Below is the sample input:
Enter No. of Testcases & Faults
50 50 code
Below is the output:
0 50
Below is the Expected output:
50 50
Note: I am bounded to use unsigned char and cannot use unsigned int.
I am using DevCpp 4.9.9.2. Please help if anyone has got the solution or the reason for why this is happening.

Look into your scanf clause. You are passing two (pointers to) unsigned char variables to it, yet declare that you are reading two ints in your format specifier (%d specifier). Pass %hhu if you want to read unsigned char.
When you pass &extension to scanf there is no need to use ampersand. The name of the array gets converted to the pointer to its first element, so it's sufficient to pass just extension.
You are also using a very old IDE (Dev-C++), that is no longer developed. Probably the underlying compiler is dated as well. I advise to use newer tools, Code::Blocks is actively developed, quite newbie-friendly and similar to Dev-C++ to a certain extent.
However, on Windows platforms, the MSVCRT library does not support the %hhu format specifier, as has been noted in this question. MingGW compiler relies on this implementation, so it does not support %hhu on windows as well. Cygwin is a project that simulates Linux environment on Windows and is distributed with its own version of C library, that supports, among other things, %hhu. As a sidenote, you can configure Code::Blocks to work with Cygwin (as described here). I don't know if the same can be achieved with Dev-C++.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned int nT,nF;
char extension[5];
puts("Enter No. of Testcases & Faults");
scanf("%d %d %s", &nT, &nF, extension);
printf("%d %d\n",nT,nF);
getch();
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char nT,nF;
char extension[5];
puts("Enter No. of Testcases & Faults");
scanf("%hhu %hhu %s", &nT, &nF, extension);
printf("%hhu %hhu\n",nT,nF);
// getch();
}
You can see output on below image

Related

printf Function not giving output in C

I am an absolute newbie, and I am learning to code.
#include <stdio.h>
#include <stdlib.h>
int main(){
char characterName[] = "Khan";
int characterYear = 2022;
float characterScore = 8.8;
char characterGrade = 'A';
printf("%s passed out of college in %d and had a grade of %s i.e. a score of %f\n",characterName,characterYear,characterGrade,characterScore);
return 0;
}
Can someone help me with this — I was just trying to incorporate all simple data types in one line (for reference) and after I run the program there is no output whatsoever, it just exits! It didn't happen when I had fewer data types in the line.
P.S. This might be the dumbest ever question ever asked on Stack Overflow, but please forgive if I am dumb/silly :)
Your format is wrong:
characterGrade has type char and you want to print it using %s format which requires char * parameter. It has to be %c instead:
"%s passed out of college in %d and had a grade of %c i.e. a score of %f\n"

different output is generated every time I run the code

Here in this code, when I execute it, it sometimes display correct value of nT and nF, sometimes it display correct nF but incorrect nT(i.e 0).
Why is it so??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char nT,nF;
puts("Enter No. of Testcases & Faults");
scanf(" %hhu %hhu",&nT,&nF);
printf("\n %hhu %hhu",nT,nF);
}
You need to use %hhu as the format specifier for an unsigned char. (Obscure I know: one for the pub quiz.) Also, you might want to introduce some spaces between your formatters:
int read = scanf("%hhu %hhu %s", &nT, &nF, extension);
Currently the behaviour of your program is undefined.
Prior to C99 you're pretty much at the mercy of your compiler.
Further notes:
Always check the return value of scanf which gives you useful information about the number of inputs that are read successfully.
extension is only good for 4 characters plus the nul-terminator.

Passing variables to system function in C [duplicate]

This question already has answers here:
system function in c is not working for me
(3 answers)
Closed 7 years ago.
I have this C code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
printf("Please enter a number:\n");
scanf("%d",&a);
printf("Your number is: %d\n",a);
system("echo %d",a);
}
I'm interested in the last command, the system() function and why I can't print my variable like i printed it with printf(). I want to be able to ask the user some input , let's say a string and then pass it to the system function.
Practical example:
Ask user for folder name
system("mkdir %s", FolderName);
Thank you in advance! :)
Use snprintf
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
char buf[BUFSIZ];
printf("Please enter a number:\n");
scanf("%d",&a);
printf("Your number is: %d\n",a);
snprintf(buf, sizeof(buf), "echo %d",a);
system(buf);
}
system, unlike printf does not accept multiple parameters, it only accepts one parameter, a const char *command. So you need to build your complete command string first in memory and then pass it to system.
An example would be:
char buf[32];
sprintf(buf, "echo %d", a);
system(buf);
You need to take care not to write more chars into buf than buf has space for. You might want to read the man page for snprintf to rewrite the code in a safer way.
Also: if your code really compiled, then please compile with a higher warning level. At least that would have given you warnings that you are calling system with more parameters than you should.
The System function doesnt have the formatting options like printf, instead the system function takes a C string as a parameter.
Check out the following site for more info.
http://www.tutorialspoint.com/c_standard_library/c_function_system.htm

Adding two numbers in char variables read with scanf

I was teaching the C programming language to a friend and we came up with something I could not explain. This is the code we wrote:
#include <stdio.h>
int main(void)
{
char num1;
char num2;
printf("%s", "Enter the first number: ");
scanf("%d", &num1);
printf("%s%d\n", "The number entered is:", num1);
printf("%s", "Enter the second number: ");
scanf("%d", &num2);
printf("%s%d\n", "The number entered is:", num2);
printf("%s%d\n", "The first number entered was:", num1); /* This was done for testing */
printf("%s%d\n", "The sum is:", num1+num2);
return 0;
}
The weird thing is that we tried to do 5 + 6 and we expected to get 11 but instead got 6, I added a line to see what's going on with the first number and it becomes 0 after the second number is read.
I am aware that the variables should be an int (in fact the original code was like that and worked) but my understanding is that a char is a small integer so I thought it would be 'safe' to use if we were adding small numbers.
The code was tested and compiled on a Linux machine with cc and on a Windows machine with cl. The output was the same. On the Windows machine the program throw an error after the addition.
I would like an explanation on why this code is not working as I expected. Thanks beforehand.
You cannot pass a pointer to a different datatype to scanf. scanf will write to memory assuming you gave it a pointer to what it expected (e.g. int for %d), and will exhibit wonderful undefined behaviour if you give it a pointer to a different datatype.
Here, what is most likely happening is that scanf is overwriting e.g. 4 bytes on your stack when your chars only take up 1 byte, so scanf will just be happily writing right over some other variable on your stack.
a char is a small integer so I thought it would be 'safe' to use it if we were adding small numbers.
That is correct, char is a small integral type , and it's OK to use it in integer arithmetic(although char may be signed or unsigned which may causes the result unexpected).
But the problem is, a pointer to char can NOT be used in a place where a pointer to int is expected. And this is the case for scanf("%d", &num1);, the second parameter is expected to a of type int *.

scanf and printf format modifiers

I need help on this exercise from C Primer Plus.
Write a program that requests your first name and does the following with it:
Prints it in a field three characters wider than the name
#include<stdio.h>
#include<string.h>
int main()
{
char a[40];
int p,v=0;
printf("Enter your first name: \n");
scanf("%s",a);
p= strlen(a);
v==p+3;
printf("%s",a);
}
I cant figure out how what to use as a modifier for the width
what should I add in between % and s?
the goal of this excercise is to read and grok the manual page for printf(). The reading part could only be done by you and there is no shortcut. The format specifier is the most complex chapter in C-Programing (other would say 'pointer'), and it is very wise to know where look things up (man-page) in need of remembering.
When you are done reading, you should have a little (or big) understanding of the format-specifier %s with all its possibilities.
EDITH: when you are done reading, and there is still a question whether to use "%*.*s" or "%s" or "%-.*s" etc., please come back with an updated question.
Here is one way to do it:
#include<stdio.h>
#include<string.h>
int main()
{
char a[40];
printf("Enter your first name: \n");
scanf("%s",a);
printf("[%-*s]", (int)(3 + strlen(a)), a);
return(0);
}
The printf() function can do it all.
From the question code, it is clear that "%s" as a format string is understood.
A format string of "%*s" allows the caller to place the (space-padded) width to be specified. For example:
printf("%*s", 10, "Hello");
The above will print "Hello" (as expected), in a 10-character frame. Hence, the command above actually prints: " Hello".
To put the spaces on the other side, tell printf() to left-justify the string using:
printf("%-*s", 10, "Hello");
This results in printing: "Hello "

Resources