473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how printf(100) works in C

38
The prototype of printf() is

Expand|Select|Wrap|Line Numbers
  1. printf(const char *__format, ...);
and i typed printf(100) and it run successfully printing a string. How it happened why it doesn't show a type mismatched error.
Jul 2 '14 #1

✓ answered by weaknessforcats

In C you so not need to have a function prototype. You don't need to include stdio.h.

If you do this, then printf will be considered an external function that returns an int. If your makefile includes the printf library, which is probably does, then the 100 is the char* argument and your build will be OK. When you run the program, you crash trying to access memory location 100 which is not in your process.

Stuff like this is why C++ was started in the first place.

7 1528
weaknessforcats
9,208 Expert Mod 8TB
printf has a char* as the first argument. If your compiler accepts 100 as valid, then I would get another compiler.

printf(100) does not compile without a cast using Visual Studio 2013.
Jul 2 '14 #2
aswal
38
I am using turbo C++ when i run it as a C program then it works but when i run it as a C++ program then it shows the error which should be shown.
Jul 2 '14 #3
donbock
2,426 Expert 2GB
Was there a prototype for printf in scope?
That is, did you include <stdio.h> before calling printf(100)?
Jul 2 '14 #4
aswal
38
No because in C we don't have to include header files. On including those it is showing the error. But I want to know why it works when we did not gave it's prototype and the output it gave.

Also, was the approach of function prototype was present in C or it came in C++
Jul 3 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
In C you so not need to have a function prototype. You don't need to include stdio.h.

If you do this, then printf will be considered an external function that returns an int. If your makefile includes the printf library, which is probably does, then the 100 is the char* argument and your build will be OK. When you run the program, you crash trying to access memory location 100 which is not in your process.

Stuff like this is why C++ was started in the first place.
Jul 3 '14 #6
Format is a pointer, pointer accepts integer value.
Jul 3 '14 #7
donbock
2,426 Expert 2GB
The original question was why didn't printf(100) provoke a compiler error. The answer is that without a prototype, the compiler had no reason to think there was anything wrong with printf(100).

The C Standard allows you to call a function without using a prototype. You can also hike into the desert without any water. The only thing stopping you from doing either of those things is a common-sense concern for the consequences.
Jul 3 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Puneet Jain | last post by:
I am tryin to use Microsoft Works Wordprocessor from Python After generating the .py module from the type library using makepy utility, I runt he following commands, >>> from win32com.client...
1
by: Gerd Güldenast | last post by:
Hi, I am having a problem with a replication over a Modem-Connection, which works fine over LAN. Has anyone experienced this problem before? Settings are: 2 SQL Servers 2000, SP3 on Windows...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
1
by: Moi | last post by:
I have tried repeatedly to import a MS Works database (simple address database) into Access, but I am having little luck. I have saved the Works file as DbaseIII DbaseIV and neither is seen by the...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
4
by: puja | last post by:
hi all, I have an asp.net website where am including .css file dynamically on page load event. For diff users, there is diff CSS file. So after user logs in, I am setting CSS href on page load....
0
by: patmg | last post by:
Hello, We use a SQL Server database to manage accounts in AD via a COM object using ADSI written in VB6. I have to move this database to a new server. The only real difference between the two...
3
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work (...
6
by: nild | last post by:
Hello i have a strange problem. I'm using LogonUser to impersonate the user under which my program must run. On Win XP or Server 2003 it works. But on 2000 it doesn't. So i found out, to set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.